Postback problem: Variable lost

J

Joseph Luner

I am having problem postback, (code shown below) the variable "my_str" is
lost after clicking the "submit" button. Isn't it suppose to display
"changed" after posting back? Is there anyway I can maintain the state of
a variable?

Imagine the variable is a Class, not String. After clicking the submit
button, I updated some properties in the instance of the class. How do I use
the updated class instance after submitting the form? Help...


<%@ Page %>
<script language="c#" runat="server">
String my_str;
void Page_Load () {
if (!Page.IsPostBack)
my_str = "hello";
else
debug.Text = "debug=" + my_str;
}
public void submit_me (object sender, EventArgs e) {
my_str = "changed";
}
</script>
<form runat="server">
<asp:button runat="server" onClick="submit_me" text="submit"/>
<asp:literal runat="server" id="debug"></asp:literal>
</form>
 
T

Teemu Keiski

It is because postback events are raised after Page_Load. E.g on postback
the Page_load happens first (displaying old value) and after that ithe
variable is actually changed. Refer straight to the Label on postback event
and you should be fine (or do processing after postback events say in
PreRender)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top