I am trying to pass variables declared in C# to html. The variables have all been declared as public in the code-behind.
This is the HTML code I am using:
<asp:TextBox ID="TextBoxChildID" Text='<%= Child_ID %>' runat="server" Enabled="false"></asp:TextBox>
The problem is that when the page loads, the text '<%= Child_ID %>' appears in the textbox instead of the value in the variable.
What is wrong please?
All of this is assuming that this is just a textbox somewhere on your page, rather than in a DataBound control. If the textbox is part of an itemTemplate in a repeater, and Child_ID is something that differes by data row, then all of this is incorrect.
Do this instead:
<asp:TextBox ID="TextBoxChildID" runat="server" Enabled="false"><%= Child_ID %></asp:TextBox>
In short, you're making the same mistake I was making when I asked this question: Why <%= %> works in one situation but not in another
Alternatively, in code-behind, you can have this in your ASPX:
<asp:TextBox ID="TextBoxChildID" runat="server" Enabled="false"></asp:TextBox>
and this in your Code-Behind:
TextBoxChildID.Text = Child_ID;
The variable must be public first. And:
'<%# Child_ID %>'
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With