This
article will show you how you can pass value from one page to another page in asp.net using c#.net. In this
article I have shown three ways to pass and get the on other page.
Some of my
previous articles are as follows: How
To Use Hyper Link Tag In Gridview In Asp.Net C#, Get
Control Value By Id in Asp.Net Using JavaScript, Enable
Or Disable GridView Button By Row CheckBox Using jQuery In Asp.Net, Browse,
Read and Populate or Show or Bind CSV File Data In GridView Using C#.Net in
Asp.net, Beginning
ASP.NET for Visual Studio 2015 Download eBook PDF, Bind
Class Property to GeidView Using C#.net In Asp.net, Gridview
Auto Generate Row Number In Asp.Net Using C#.Net, jQuery
Top Right ToolTip On Hyper Link With Question Mark Sign On Mouse Cursor In
Asp.net.
So for this article first I have created an asp.net application and add the two pages in the application. Now on page one add the below code.
So for this article first I have created an asp.net application and add the two pages in the application. Now on page one add the below code.
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Page1.aspx.cs"
Inherits="WebApplication7.Page1" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Page 1</title>
</head>
<body>
<form id="form1" runat="server">
<h3>Click Button To
Pass Value</h3>
Enter
Value :<asp:TextBox ID="txtValue" runat="server" Width="201px"></asp:TextBox>
<br />
<br />
<div>
<asp:Button ID="Button1" runat="server" Text="By Request Querystring" OnClick="Button1_Click"
Width="203px" />
<br />
<br />
<asp:Button ID="Button2" runat="server" Text="By Session"
Width="204px" OnClick="Button2_Click"
/>
<br />
<br />
<asp:Button ID="Button3" runat="server" Text="By HiddenField" Width="201px" PostBackUrl = "Page2.aspx"/><br />
</div>
</form>
</body>
</html>
|
In above code I have taken three button controls. Each button represents a way to transfer the data. In above I have passed the textbox value from one page to other page. As you execute the page1 your page will look as shown below.
Now have a look of the below code.
Way 1:
protected void Button1_Click(object sender, EventArgs e)
{
Response.Redirect("Page2.aspx?value=" + txtValue.Text);
}
|
In above code I have used request querystring to pass the value.
Way2:
protected void Button2_Click(object sender, EventArgs e)
{
Session["Value"] = txtValue.Text;
Response.Redirect("Page2.aspx");
}
|
In above I have used session to pass the value to other page.
Way3:
<asp:Button ID="Button3" runat="server" Text="By HiddenField" Width="201px" PostBackUrl = "Page2.aspx"/>
|
In above I have used PostBackUrl to pass the value and access it on other page. Now we will check the code for page2.
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Page2.aspx.cs"
Inherits="WebApplication7.Page2" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Page 2</title>
</head>
<body>
<form id="form1" runat="server">
<div>
Request Querystring :<asp:Label ID="Label1" runat="server" Text=""></asp:Label><br />
Session Value :<asp:Label ID="Label2" runat="server" Text=""></asp:Label><br />
Http
Post:<asp:Label ID="Label3" runat="server" Text=""></asp:Label><br />
</div>
</form>
</body>
</html>
|
Now have a look
the below code of page2 access the pass value.
Way1 To
access:
if (Request.QueryString["value"] != null)
{
Label1.Text = Request.QueryString["value"].ToString();
}
|
if (Session["value"] != null)
{
Label2.Text = Session["value"].ToString();
}
|
if (Request.Form["txtValue"] != null)
{
Label3.Text = Request.Form["txtValue"].ToString();
}
|
Now press F5 to check the output.
0 comments:
Please let me know your view