There are various techniques to pass values from one page to another page . These are Session, ViewState, QueryString, findControl , etc
Here we use one by one for passing values from one page to another page.
Session["Session variable"] = variable;
variable = Session["Session variable"].Tostring();
Note : Session value store in all page of browser.
http://www.id-script.com/default.aspx?id=7 // here id=7 define query string data
lets take an simple example
Step-1 : Take two Web forms (first.aspx and second.aspx)
Step-2 : first page contain one textbox , one button and one hyperlink control
Output
Here we use one by one for passing values from one page to another page.
Use Session:
Syntax :Session["Session variable"] = variable;
variable = Session["Session variable"].Tostring();
Note : Session value store in all page of browser.
Use QueryString
Syntax : Basically query string pass in URLhttp://www.id-script.com/default.aspx?id=7 // here id=7 define query string data
lets take an simple example
Step-1 : Take two Web forms (first.aspx and second.aspx)
Step-2 : first page contain one textbox , one button and one hyperlink control
<%@ Page Language="C#" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
protected void Button1_Click(object sender, EventArgs e)
{
Session["username"] = TextBox1.Text;
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
Enter Session data :
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox><br />
<asp:Button ID="Button1" runat="server" Text="Store Session"
onclick="Button1_Click" />
<br />
<asp:HyperLink ID="HyperLink1" runat="server" NavigateUrl ="~/second.aspx?id=7" >QueryString</asp:HyperLink>
<br />
</div>
</form>
</body>
</html>
Step-3 : Second page contains two label control.<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
protected void Button1_Click(object sender, EventArgs e)
{
Session["username"] = TextBox1.Text;
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
Enter Session data :
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox><br />
<asp:Button ID="Button1" runat="server" Text="Store Session"
onclick="Button1_Click" />
<br />
<asp:HyperLink ID="HyperLink1" runat="server" NavigateUrl ="~/second.aspx?id=7" >QueryString</asp:HyperLink>
<br />
</div>
</form>
</body>
</html>
<%@ Page Language="C#" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
protected void Page_Load(object sender, EventArgs e)
{
Label1.Text = "Session data " + Session["username"].ToString();
Label2.Text = "query string data" + Page.Request.QueryString["id"].ToString();
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Label ID="Label1" runat="server" Text=""></asp:Label><br />
<asp:Label ID="Label2" runat="server" Text=""></asp:Label>
</div>
</form>
</body>
</html>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
protected void Page_Load(object sender, EventArgs e)
{
Label1.Text = "Session data " + Session["username"].ToString();
Label2.Text = "query string data" + Page.Request.QueryString["id"].ToString();
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Label ID="Label1" runat="server" Text=""></asp:Label><br />
<asp:Label ID="Label2" runat="server" Text=""></asp:Label>
</div>
</form>
</body>
</html>
Output
Tidak ada komentar:
Posting Komentar