Search code examples
c#asp.netwebformsresponse.writeshorthand

ASP.net shorthand in TextBox


I am trying to do the following:

<asp:TextBox ID="txtName" runat="server" Text="<%= Name %>" />

When I execute my page it gets output as <%= Name %> instead of actually doing a response.write.

I tried modifying it to use the <% Response.Write(Name) %> instead but it did the same thing, putting the text there instead.

I can do this just fine:

<input type="text" value="<%= Name %>" />

That will actually work. Why doesn't this work when I use the TextBox control? Is there another way I'm supposed to do this?


Solution

  • Either use code behind:

    txtName.Text = Name;

    Or, add Page.DataBind() in your code behind and change the syntax of your control to:

    <asp:TextBox ID="txtName" runat="server" Text="<%# Name %>" />

    Note the # rather than the =. # represents a data-binding expression