Click here to Skip to main content
15,893,266 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
Hi

I created a page with some controls within it.When i put "<% label.Text = "changed";%>" after definition of Label ,label text value do not render after execute page.why?

ASP.NET
<div>
   <tagprefix:MD ID="CC"  runat="server" />
        <asp:TextBox runat="server"  ID="TextBox1"/>
        <asp:Button Text="button" ID="Button"  runat="server" onclick="Button_Click" />
        <%= "Hello..." %>

        <% label.Text = "changed"; %>                          //working fine
        <asp:Label ID="label" Text="text" runat="server" />
        <%--<% label.Text = "changed"; %>--%>                  // do not work

</div>
Posted
Updated 7-Dec-11 4:28am
v2

1 solution

It is because the embedded code block is executed during rendering. See http://msdn.microsoft.com/en-us/library/ms178135.aspx. Therefore if you put the code behind the label, the label is already rendered when the code is executed. If you put the embedded code block before the label it is executed before the rendering of the label.
 
Share this answer
 
v2
Comments
[no name] 7-Dec-11 10:50am    
i know,my question is why this code should not work?
K Herms 8-Dec-11 3:38am    
It is because the embedded code block is executed during rendering. See http://msdn.microsoft.com/en-us/library/ms178135.aspx. Therefore if you put the code behind the label, the label is already rendered when the code is executed. If you put the embedded code block before the label, it works.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900