How to assign a method’s output to a textbox value without code behind

How do I assign a method’s output to a textbox value without code behind?

<%@ Page Language="VB" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
    Public TextFromString As String = "test text test text"
    Public TextFromMethod As String = RepeatChar("S", 50) 'SubSonic.Sugar.Web.GenerateLoremIpsum(400, "w")

    Public Function RepeatChar(ByVal Input As String, ByVal Count As Integer)
        Return New String(Input, Count)
    End Function
</script>
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    <title>Test Page</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <%=TextFromString%>
        <br />
        <asp:TextBox ID="TextBox1" runat="server" Text="<%# TextFromString %>"></asp:TextBox>
        <br />
        <%=TextFromMethod%>
        <br />
        <asp:TextBox ID="TextBox2" runat="server" Text="<%# TextFromMethod %>"></asp:TextBox>        
    </div>   
    </form>
</body>
</html>

it was mostly so the designer guys could use it in the aspx page. Seems like a simple thing to push a variable value into a textbox to me.

It’s also confusing to me why

<asp:Label runat="server" ID="label1"><%=TextFromString%></asp:Label>

and

<asp:TextBox ID="TextBox3" runat="server">Hello</asp:TextBox>

works but

<asp:TextBox ID="TextBox4" runat="server"><%=TextFromString%></asp:TextBox>

causes a compilation error.

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

There’s a couple of different expression types in .ASPX files. There’s:

<%= TextFromMethod %>

which simply reserves a literal control, and outputs the text at render time.

and then there’s:

<%# TextFromMethod %>

which is a databinding expression, evaluated when the control is DataBound(). There’s also expression builders, like:

<%$ ConnectionStrings:Database %>

but that’s not really important here….

So, the <%= %> method won’t work because it would try to insert a Literal into the .Text property…obviously, not what you want.

The <%# %> method doesn’t work because the TextBox isn’t DataBound, nor are any of it’s parents. If your TextBox was in a Repeater or GridView, then this method would work.

So – what to do? Just call TextBox.DataBind() at some point. Or, if you have more than 1 control, just call Page.DataBind() in your Page_Load.

Private Function Page_Load(sender as Object, e as EventArgs)
   If Not IsPostback Then
      Me.DataBind()
   End If
End Function

Method 2

Have you tried using an HTML control instead of the server control? Does it also cause a compilation error?

<input type="text" id="TextBox4" runat="server" value="<%=TextFromString%>" />


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x