Suppose I have the following asp.net page:
public partial class SamplePage : System.Web.UI.Page
{
private int UserID;
protected void Page_Load(object sender, EventArgs e)
{
}
protected void btnSearchUser_Click(object sender, EventArgs e)
{
UserID = 5;
}
protected void btnSubmit_Click(object sender, EventArgs e)
{
Response.Write(UserID.ToString());
}
}
but UserID is always 0. How to achieve it to be 5 always?
Because a Button’s click causes a postback and data is always lost during postbacks as HTTP protocol is stateless.. So you’ll have to store that UserID into a viewstate to maintain its value…and that’s why we have State Mangement’s concept in Asp.net.
However you can achieve it like this…