ASP.NET - C # variable passing in HTML

Possible duplicate:
Why does <% =%> work in one situation, but not in another

I am trying to pass variables declared in C # to html. All variables were declared public in the code.

This is the HTML I use:

<asp:TextBox ID="TextBoxChildID" Text='<%= Child_ID %>' runat="server" Enabled="false"></asp:TextBox> 

The problem is that when the page loads in the text field, the text '<% = Child_ID%>' appears instead of the value in the variable.

What's wrong please?

+6
source share
4 answers

All of this assumes that this is just a text box somewhere on your page, and not in a DataBound control. If the text field is part of the itemTemplate element in the repeater, and Child_ID is what differs in the data row, then all this is wrong.

Do this instead:

 <asp:TextBox ID="TextBoxChildID" runat="server" Enabled="false"><%= Child_ID %></asp:TextBox> 

In short, you are making the same mistake I made when I asked this question: Why does <% =%> work in one situation, but not in another


Alternatively, in code, you can have this in your ASPX:

 <asp:TextBox ID="TextBoxChildID" runat="server" Enabled="false"></asp:TextBox> 

and this is in your code behind:

 TextBoxChildID.Text = Child_ID; 
+7
source

The variable must be publicly available. A:

 '<%# Child_ID %>' 
+1
source
 <script type="text/javascript"> function abc() { var id = document.getElementById('txtTextBox'); id.value=<%=MyProperty %>; alert(id.value); } </script> 

 protected int MyProperty { get { return 1; } } 

 Page.RegisterStartupScript(Guid.NewGuid().ToString(), "<script language = 'javascript'>abc();</script>"); 
+1
source

In HTML:

 <asp:HiddenField ID="HiddenField1" runat="server" /> 

In Codebehind:

 protected void Page_Load(object sender, EventArgs e) { HiddenField1.Value = Child_ID; } 

That would be the best way, it creates a hidden input with a value.

0
source

Source: https://habr.com/ru/post/911434/


All Articles