Different Approaches to Pass data as the Query String (Client Side and Server Side in Dotnet)


In this article, step by step we will learn, How to pass the data from one page to another page by using Query String in both client and server side. I will be using the ASP.Net with C# as the code. I have provided the code snippets wherever required.

Abstract


This article is related to the state management of the ASP.Net and we will see here how to perform the query string client side state management technique to pass the data from one page to other page.

Introduction



This article will be helpful for those members who are looking to pass the data from one page to another using the state management techniques. We will see, how to pass the data between pages using the client side state management techniques i.e. Query String.


Pre-requisite


First if all, to implement the solution and to feel the taste of code, we must have:

• Visual Studio 2010 or later

Step1:- Why do we need to pass data using the state management technique i.e. Query string



If we talk about the Windows Application, where we can just assign the data using global variables and then we can retrieve the same in to other forms. But as the Web is stateless, so it can't maintain and store the data which is set in one page. When we assign the data in to a page and do the post back, all the data will get flushed and we get nothing. To get the data in to another page, we need to maintain the state of the page. So there are various techniques to maintain the state of the [page i.e.

• Client Side state management techniques
• Server Side State Management techniques

Again there is various Client side state management techniques like:
- View State
- Hidden Controls
- Query String
-
Server Side State management techniques are:
- Session
- Application
- Profile
Each of these state management techniques depends on the requirements. The Quest string is the best way to pass the data from one page to next page and is very efficient in this way.

How to assign the values for View State, Session,Hidden Field in c# server side

1.ViewState




ViewState["Empno"] = Request.QueryString["EmpName"].ToString();


2. Session




Session["Empno"] = Request.QueryString["EmpName"].ToString();
Session.Add("EmpNo", Request.QueryString["EmpName"].ToString());


3. Hidden Field




HiddenField reg = new HiddenField();
reg.Value = Request.QueryString["EmpName"].ToString();


4. Application




//Application Mostly assign in our project Global.asax file.
Application.Add("EmpName", Request.QueryString["EmpName"].ToString());


Step2:- Pass the data via Query string using Client Side Code use this code(C#)




<asp:GridView ID="Grd1" runat="server">
<Columns>
<asp:BoundField DataField="Id" HeaderText="" />
<asp:BoundField DataField="EmpNo" HeaderText="" />
<asp:BoundField DataField="EmpName" HeaderText="" />
<asp:BoundField DataField="Amount" HeaderText="" />
<asp:TemplateField>
<ItemTemplate>
<asp:HyperLink ID="HyperLink1" runat="server" NavigateUrl='<%# string.Format("WebForm7.aspx?Id={0}&EmpNo={1}&EmpName={2}",
HttpUtility.UrlEncode(Eval("Id").ToString()), HttpUtility.UrlEncode(Eval("EmpNo").ToString()), HttpUtility.UrlEncode(Eval("EmpName").ToString())) %>'
Text="View Details" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>


Step3: Pass the data via Query string using Server Side Code (C#)





DataTable dt = new DataTable();
dt.Columns.AddRange(new DataColumn[4] {new DataColumn("Id"), new DataColumn("EmpNo"),new DataColumn("EmpName"),new DataColumn("Amount") });
dt.Rows.Add(1, 1500,"John", 7500);
dt.Rows.Add(2, 2500, "Wesly", 8500);
dt.Rows.Add(3, 4500, "Ruseel", 9500);

Grd1.DataSource = dt;
Grd1.DataBind();

protected void Button1_Click(object sender, EventArgs e)
{
Response.Redirect("WebForm7.aspx?EmpNo" + TxtEmpno.Text);

DataTable dt = new DataTable();
if (dt.Rows.Count > 0)
{
Response.Redirect("WebForm7.aspx?EmpName=" + dt.DefaultView[0]["EmpName"].ToString());
}
}




Step4: Retrieve the Query string data and populate in to current page using below code(C#).





protected void Page_Load(object sender, EventArgs e)
{
string Qrtsttring = Request.QueryString["EmpName"].ToString();
string Qrtsttring = Request.QueryString["EmpNo"].ToString();
}



Conclusion


In this article, we have discussed about passing data using Query String.
1. Create a new project
2. Choose correct template
3. Add Web Forms
4. Bind Grid view
5. Navigate one page to another page

Hope, you find this article helpful.


Comments

Author: Sridhar Thota30 Aug 2015 Member Level: Gold   Points : 7

Passing Multiple values in the Query String:

QueryStrings are separated by & symbol.
If we want more than one value to be passed in
QueryString then we should use "&" between two
QueryStrings.

With in the Button1_Click event of Default.aspx,
am sending three values Name, Password and Id
for second webpage Default2.aspx with in the url.


protected void Button1_Click(object sender,
EventArgs e)
{
Response.Redirect("~/Default2.aspx?Name=" +
TextBox1.Text + "&Password=" + TextBox2.Text
+ "&Id=" + TextBox3.Text );
}


Accessing Multiple Values in the Query String:

To read a QueryString value we use Request.QueryString.
Place 3 labels on webpage Default2.aspx.
With in the page load logic displaying the
QueryStrings values in labels.

protected void Page_Load(object sender,
EventArgs e)
{
Label1.Text = Request.QueryString["Name"];
Label2.Text = Request.QueryString["Password"];
Label3.Text = Request.QueryString["Id"];
}

We can use index to read QueryString as well.

protected void Page_Load(object sender,
EventArgs e)
{
Label1.Text= Request.QueryString[0];
Label2.Text= Request.QueryString[1];
Label3.Text= Request.QueryString[2];
}


Regards

Sridhar Thota.
If you learn from defeat..
You haven't really lost..



  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:
    Email: