asp.net - How to keep the variable value after post back -


i trying keep variable value after post back. tried both session variable , viewstate failed keep value of random number same. every time after button press (after page refresh) getting new random value want keep same value.

//in code behind public static int randnumber{ get; set; }  protected void page_load(object sender, eventargs e) {     //by using session      session["rand"] = rnd.next(0, 10);     randnumber = int32.parse(session["rand"].tostring());      //by view state     int rand = rnd.next(0, 10);     viewstate["key"] = rand;     randnumber = int32.parse(viewstate["key"].tostring()); } 

for post in form:

<asp:button id="button1" runat="server" text="button" onclick="button1_click" /> 

and tried access in page below:

<p>random no: <%= randnumber %></p> 

only set new random number if it's not post checking ispostback

    public int randnumber{ get; set; }      protected void page_load(object sender, eventargs e)     {         //by using session          if(!ispostback){             session["rand"] = rnd.next(0, 10);         }         randnumber = int32.parse(session["rand"].tostring());     } 

Comments

Popular posts from this blog

java - SSE Emitter : Manage timeouts and complete() -

jquery - uncaught exception: DataTables Editor - remote hosting of code not allowed -

java - How to resolve error - package com.squareup.okhttp3 doesn't exist? -