user1151923
user1151923

Reputation: 1872

textbox's value not correct when displayed in browser

I have a simple textbox on my page with an embedded code block where I set its value. But in the browser it still displays "oldvalue". Can't figure out why..

<asp:TextBox id="textBox" runat="server" Text="oldvalue">
</asp:TextBox>     

<%
    var box = FindControl("textBox") as TextBox;
    box.Text = "newvalue";
%>  

Upvotes: 1

Views: 135

Answers (1)

R.C
R.C

Reputation: 10565

This will not work as the inline expressions <% %> are executed after prerender event in the asp.net page life cycle.

And the last changes you can make to the contents of the page or its controls is upto PreRender event, So that any changes in the view state of the server control can be saved during this event. MSDN reference here.

So, rather than using inline expressions, use any event upto PreRender events of Page life cycle in your code to change the TextBox.Text property.

Upvotes: 2

Related Questions