Im trying to set a value in a method as shown below but when i run btnSaveValue there is no value to retrieve. Ive even tried creating a private int val in the defaul class and assigning the value to that but still a blank value comes through – can anyone help me out?
thanks
public partial class _Default : System.Web.UI.Page
{
valueAllocation valAlloc = new valueAllocation();
public void declaringValue()
{
valAlloc.setValue(5);
int testAlloc = valAlloc.getValue();
lblResult.Text="Value set here is:"+testAlloc; //THIS WORKS!!!
}
protected void btnSaveValue_Click(object sender, ImageClickEventArgs e)
{
lblResult.Text = "Value now is:" + valAlloc.getValue(); //DOESNT WORK??????!!!!!
}
}
public class valueAllocation
{
private int val;
public void setValue(int value)
{
val = value;
}
public string getValue()
{
return val;
}
}
This is because you need to save the value in each post using for example the
ViewState.This is a basic problem related with the ASP.Net page life-cycle.
Basically, every time you request your page, a new instance of the page is created on every post, and destroyed when the response returns to the client
If you want to keep the state across post backs, you need to manually save each value in the
ViewStatestoring the whole type in the ViewState
I think this is your best bet
Custom Type
Code behind