Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.NET - Passing a C# variable to HTML [duplicate]

Tags:

c#

asp.net

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?

like image 1000
Matthew Avatar asked Mar 22 '12 16:03

Matthew


2 Answers

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;
like image 131
David Avatar answered Sep 21 '22 10:09

David


The variable must be public first. And:

'<%# Child_ID %>' 
like image 38
Rafael Carvalho Avatar answered Sep 21 '22 10:09

Rafael Carvalho