quey string and view state

Posted by Suneel161 under Regular Expressions on 3/21/2010 | Views : 8939 | Status : [Member] | Replies : 2
hii

how to use query string and view state. example




Responses

Posted by: Raja on: 3/21/2010 [Member] Starter

Up
0
Down
Querysring Example

http://www.dotnetfunda.com/post/PostAnswer.aspx?qid=1671&com=added


You can see in the above code that qid and com is the querystring. Querystring is appended at the last of URL of the web page and first querystring starts with ? and rest is appended with &

To get any of the querystring value use below code snippet

If (Request.QueryString["qid"] != null) // this will ensure that below line will only fire if querystring exists in the url

{
string qid = Request.QueryString["qid"];
}


ViewState Example:

ViewState is used to store the data in the form of hidden variable in the source code of the page.

<input type="hidden" name="hiddenId" id="hiddenID" value="25" /> 


To store the value in the ViewState you can use below code snippet
ViewState["hiddenId"] = "25";


To get the value of the ViewState, use below code
if (ViewState["hiddenId"] != null) // to ensure that the viewstate exists

{
string id = ViewState["hiddenid"].ToString();
}


Hope this will help.

Thank you

Regards,
Raja, USA

Suneel161, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Sagarp on: 3/24/2010 [Member] Bronze

Up
0
Down
ViewState:
Viewstate is used to maintaine the state (data and event) of page
during postback. ViewState is maintained using a hidden field that hold all information in encrypted format.

ViewState is also an object that can be used to hold data that is page specific.

Querystring:
Querystring is used to send the inforamtion to other page. Information is sent using the url and same can be read in other page.

1)QueryString
=============

protected void Page_Load(object sender, EventArgs e)
{
string v = Request.QueryString["value"];
if (v != null)
{
Response.Write("valueis ");
Response.Write(v);
}
}



2)Viewstate
===========
protected void Page_Load(object sender, EventArgs e)

{
if(ViewState["NameOfUser"] != null)
NameLabel.Text = ViewState["NameOfUser"].ToString();
else
NameLabel.Text = "Not set yet...";
}

protected void btnSubmit_Click(object sender, EventArgs e)
{
ViewState["NameOfUser"] = NameField.Text;
NameLabel.Text = NameField.Text;
}


Thanks
SagarP
http://www.emanonsolutions.net
http://emanonsolutions.blogspot.com/

Suneel161, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response