Thursday, 12 December 2013

Query String AND Hidden Fields

1. What is Query String?
  • Client side state management
  • Query strings are data that is appended to the end of a page URL.
  • For passing variables content between pages ASP.NET gives us several choices. One choice is using QueryString property of Request Object.
2. I have one page which contains one textbox and button control I need to send textbox value to another page when we click on button control for that we need to write the code like this

protected void btnSend_Click(object sender, EventArgs e)
{
       Response.Redirect("Default2.aspx?UserId="+txtUserId.Text);
}

Or

In case if we need to send multiple parameters to another page we need to write code like this

protected void btnSend_Click(object sender, EventArgs e)
{
   Response.Redirect("Default2.aspx?UserId="+txtUserId.Text+"&UserName="+txtUserName.Text);
}

Now we need to get these values in another page (here I mentioned Default2.aspx) by using QueryString Parameter values with variable names or index values that would be like this


protected void Page_Load(object sender, EventArgs e)
{
     if(!IsPostBack)
     {
          lblUserId.Text = Request.QueryString["UserId"];
          lblUserName.Text = Request.QueryString["UserName"];
     }
}

Or

protected void Page_Load(object sender, EventArgs e)
{
      if(!IsPostBack)
     {
          lblUserId.Text = Request.QueryString[0];
          lblUserName.Text = Request.QueryString[1];
      }
}

3. Advantages of this approach
  • It is very easy.
  • No server resources are required. The query string containing in the HTTP requests for a specific URL.
  • All browsers support query strings.
4. Disadvantages of this approach
  • QueryString have a max length, If you have to send a lot information this approach does not work.
  • QueryString is visible in your address part of your browser so you should not use it with sensitive information.
  • QueryString can not be used to send & and space characters.
5.What are Hidden Fields?
  • Hidden fields are Client side state management technique is widely used in ASP.NET programing.
  • Hidden fields are html input control with hidden type that store hidden data in the html.
  • <input type="hidden" name="HTMLHiddenField" id="hdnName" type="hidden"/>
 6. Difference Hidden Field and View State
  • Hidden Fields does not support for storing structured values while View State support for structured values.
  • Hidden Fields must be explicitly added it to the page and Each control on a Web Forms page, including the page itself, has a ViewState property.
  • HiddenFeilds are used to store small amount of data during the post backs. Hidden field stores a single variable in its value property . And View State can store complex data.

No comments:

Post a Comment