Whenever we develop any application on that case we must
face some case where we needed to pass the value from one page to another page
in our asp.net application and capture the pass value in our application.
So before moving forward I will show you some of my previous
articles Free
jQuery Spell Checker Plugins For Asp.Net, Download
MVC 4 for Visual Studio 2010, Reset
Div Height by Using jQuery, Custom
Error Page in Asp.Net Using C#.Net, jQuery
DateTime Picker Calender In ASP.NET MVC | How To Access jQuery DateTime Picker
Calender Value in MVC Controller.
Now for this article first you needed to create new asp.net
application. After creating .aspx pages in form one add a textbox and a button
control this textbox control value we will pass into next page when user click
on button.
So this article I will show in two ways for passing the data
from one page to another page. Part 1 will contain pass value by requestquerystring
and part 2 will contain pass value by session.
Defaut.aspx code
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="ProjectDemo_Asp.et.Default"
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Passing
Value From One Form to Another in Asp.Net</title>
</head>
<body>
<form id="form1" runat="server">
<h3>
Passing Value in Asp.Net Page</h3>
<asp:TextBox ID="TextBox1" runat="server" Height="21px" Width="201px"></asp:TextBox>
<asp:Button ID="Button1" runat="server" Text="Click" Width="68px"
onclick="Button1_Click" style="height: 26px" />
</form>
</body>
</html>
|
Page2.aspx code
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Page2.aspx.cs" Inherits="ProjectDemo_Asp.et.Page2" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Passing
Value From One Form to Another in Asp.Net</title>
</head>
<body>
<form id="form1" runat="server">
<div>
Recived Value: <asp:Label ID="Label1" runat="server"
Text="Label"></asp:Label>
</div>
</form>
</body>
</html>
|
Part1: By Request.QueryString
.cs code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace ProjectDemo_Asp.et
{
public partial class Default : System.Web.UI.Page
{
protected
void Page_Load(object
sender, EventArgs e)
{
}
protected
void Button1_Click(object
sender, EventArgs e)
{
Response.Redirect("Page2.aspx?value=" + TextBox1.Text);
}
}
}
|
In above code we have passed the value as a parameter to the
page 2.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace ProjectDemo_Asp.et
{
public partial class Page2 : System.Web.UI.Page
{
protected
void Page_Load(object
sender, EventArgs e)
{
if
(Request.QueryString["value"] !=
null)
{
Label1.Text =
Request.QueryString["value"].ToString();
}
}
}
}
|
In above code we have check whether the pass value is null
or not if not the assign the value. If we not apply this check and user does
not pass the value the we will get error. Now run the application for output.
Part 2: By using session
Page1
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace ProjectDemo_Asp.et
{
public partial class Default : System.Web.UI.Page
{
protected
void Page_Load(object
sender, EventArgs e)
{
}
protected
void Button1_Click(object
sender, EventArgs e)
{
Session["value"]
= TextBox1.Text;
Response.Redirect("Page2.aspx");
}
}
}
|
Page2
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace ProjectDemo_Asp.et
{
public partial class Page2 : System.Web.UI.Page
{
protected
void Page_Load(object
sender, EventArgs e)
{
if
(Session["value"] != null)
{
Label1.Text = Session["value"].ToString();
}
}
}
}
|
Now run the application by applying break point.
0 comments:
Please let me know your view