2

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?

Dismissile
  • 32,564
  • 38
  • 174
  • 263
  • 2
    The better approach is really to use code-behind assignment of the value. Any reason you can't do that here? txtName.Text = Name; – Raelshark Jun 01 '11 at 15:02
  • I have to set 100 different things. I don't feel that code-behind is the better approach. WHat I'm really using this for is to get a unique validation group name for a User Control that has a validation group by using Client ID. – Dismissile Jun 01 '11 at 15:09
  • Gotcha. In that case yeah - Town had the answer. – Raelshark Jun 01 '11 at 23:28

2 Answers2

4

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

Town
  • 14,706
  • 3
  • 48
  • 72
0

Because the control is rendered differently than a literal. Use the codebehind to set the Text property.

Daniel A. White
  • 187,200
  • 47
  • 362
  • 445