Monday, August 29, 2011

Passing variables between pages using QueryString

Posted by Rahul Kharde at 10:55 AM

Query string is used to pass the values or information form one page to another page. It is part of State Management

You want to see how to use the QueryString collection in the ASP.NET web development framework. Almost all examples show one way of using this NameValueCollection, but there are faster and more logical ways. This short information sheet has some examples and tips with QueryString, using the C# programming language.

The query string is composed of a series of field – value pair. Below is the pattern of query string

field1=value1&field2=value2&field3=value3

An URL that contains query string can be depicted as follow:

http://www.domainname.com?field1=value1&field2=value2&field3=value3

Retrieving Query Strings in ASP.NET
Retrieving query strings in ASP.NET is quite simple, what we need is just the following line of code:
Page.Request.QueryString[queryStringField];
or
Page.Request.QueryString[indexOfQueryString];

Just do this inside of your ASP.NET code: Page.Request.QueryString["FName"] or Page.Request.QueryString[0]

Let’s look at how it work in simple source of code

Often you need to pass variable content between one aspx page to other aspx web page. For example in first page you collect information about your First And Last Name (Default.aspx), use this information in your second page(Default2.aspx).

HTML

Default.aspx

<html xmlns="http://www.w3.org/1999/xhtml">

<head runat="server">

<title>QueryString Exampletitle>

</head>

<body>

<form id="form1" runat="server">

<div>

<div style="float: left; width: 100%">

<div style="float: left; width: 100%">

<div style="float: left; width: 20%">

First Name :

</div>

<div style="float: right; width: 78%">

<asp:TextBox ID="TextBox1" runat="server">asp:TextBox>

</div>

</div>

<div style="float: left; width: 100%">

<div style="float: left; width: 20s%">

Last Name :

</div>

<div style="float: right; width: 78%">

<asp:TextBox ID="TextBox2" runat="server">asp:TextBox>

</div>

</div>

<div style="float: left; width: 100%">

<asp:Button ID="Button1" runat="server" Text="Submit" onclick="Button1_Click" />

</div>

</div>

</div>

</form>

</body>

</html>

Code Behind Default.aspx

protected void Button1_Click(object sender, EventArgs e)

{

Server.Transfer("Default2.aspx?FName="+TextBox1.Text+"&LName="+TextBox2.Text);

}

Default2.aspx

<html xmlns="http://www.w3.org/1999/xhtml">

<head runat="server">

<title>Untitled Page</title>

</head>

<body>

<form id="form1" runat="server">

<div>

<asp:Label ID="Label1" runat="server" Text="Label">asp:Label>

</div>

</form>

</body>

</html>

Default2.aspx Code Behind

protected void Page_Load(object sender, EventArgs e)

{

if (!IsPostBack)

{

Label1.Text = "First Name : " + Request.QueryString["FName"].ToString()

+ "Last Name : " + Request.QueryString["LName"].ToString();

}

}



If you enjoyed this post and wish to be informed whenever a new post is published, then make sure you subscribe to my regular Email Updates. Subscribe Now!


Kindly Bookmark and Share it:

YOUR ADSENSE CODE GOES HERE

0 comments:

Have any question? Feel Free To Post Below:

 

Popular Posts

Recent Comments

© 2011. All Rights Reserved | Help to understand .Net | Template by Blogger Widgets

Home | About | Top