Search code examples
c#postbackviewstaterequest.form

How to use ViewState after Request.Form?


Hi I'm trying to save the values from the request.form after post back. I've tried to use viewstate on the input string but the page_load keeps regenerating the request.form

string TID = null;
Protected void Page_Load(object sender, EventArgs e)
{
     if (TID == null)
     {  
            TID= Request.Form ["totalID"];
      }
      Label1.Text = TID;
}

protected void Button2_Click (object sender, EventArgs e)
{
     Label1.Text = TID;
}

Solution

  • Most asp.net controls have a text property, so if yours is a TextBox and you named it textBoxTotalID, then you can just get the value entered using this.textBoxTotalID.Text.

    Instead of totalID, use the name of the element not the Id. The name is used for posting information to the server. And also make sure you have specified a name in your view for the element whose value you need on post.

    In addition to that, ask yourself this question: Do you need to do that even when the user requests the page or only when the form is submitted? I am sure your answer is only when form is submitted. In that case, do it only when IsPostBack is true.