transferring information between pages
Post on 07-Apr-2018
222 Views
Preview:
TRANSCRIPT
8/6/2019 Transferring Information Between Pages
http://slidepdf.com/reader/full/transferring-information-between-pages 1/4
TRANSFERRING INFORMATION BETWEEN PAGES
Cross Page Posting
A cross-page postback is a technique that extends the postback mechanism youve already
learned about so that one page can send the user to another page, complete with all theinformation for that page.
Query String
The query string is the portion of the URL after the question mark. In this case, it defines a
single variable named q, which contains the string organic+gardening.
Another common approach is to pass information using a query string in the URL.This
approach is commonly found in search engines. For example, if you perform a search on the
Google website, youll be redirected to a new URL that incorporates search Parameters.
QueryString1.aspx.CS
protected void Page_Load(object sender, EventArgs e){
if (!this.IsPostBack){
lstItems.Items.Add("A");lstItems.Items.Add("B");lstItems.Items.Add("C");
}}protected void Button1_Click(object sender, EventArgs e){
if (lstItems.SelectedIndex == -1){
// Error Message}else{
string url = "~/Topics/Transferring Information/QueryString2.aspx?";//url += "Item=" + lstItems.SelectedItem.Text;string X = lstItems.SelectedItem.Text;string Y = chkDetails.Checked.ToString();url = url + "Item=" + X + "&" + "Mode=" + Y;Response.Redirect(url);
}}
8/6/2019 Transferring Information Between Pages
http://slidepdf.com/reader/full/transferring-information-between-pages 2/4
QueryString2.aspx.CS
protected void Page_Load(object sender, EventArgs e){
lblInfo.Text = "Item: " + Request.QueryString["Item"] + "<br/>" +"Selected:"+ Request.QueryString["Mode"];
}
// ³?´ needed at page URL
//Items and Mode are query stringname
Cookies
Cookies are small files that are created on the clients hard drive (or, if theyre temporary,
in the web browsers memory). One advantage of cookies is that they work transparently
without the user being aware that information needs to be stored. They also can be easily
used by any page in your application and even be retained between visits, which allows for
truly long-term storage.
Reference: using System.Net;
CookieExample.aspx.cs
protected void Page_Load(object sender, EventArgs e){
HttpCookie cookie = Request.Cookies["Preferences1"];if (cookie == null){
lblWelcome.Text = "<b>Unknown Customer</b>";}else{
lblWelcome.Text = "Welcome : " + cookie["Name"];}
}
protected void cmdStore_Click(object sender, EventArgs e)
8/6/2019 Transferring Information Between Pages
http://slidepdf.com/reader/full/transferring-information-between-pages 3/4
{// Check for a cookie, and only create a new one if// one doesn't already exist.HttpCookie cookie = Request.Cookies["Preferences1"];if (cookie == null){
cookie = new HttpCookie("Preferences1");}else{}cookie["Name"] = txtName.Text;cookie.Expires = DateTime.Now.AddYears(1);Response.Cookies.Add(cookie);lblWelcome.Text = "<b>Cookie Created.</b><br /><br />";lblWelcome.Text += "New Customer: " + cookie["Name"];
}
8/6/2019 Transferring Information Between Pages
http://slidepdf.com/reader/full/transferring-information-between-pages 4/4
Session State
Session state management is one of ASP.NETs premiere features. It allows you to storeany type of data in memory on the server. The information is protected, because it is never
transmitted to the client, and its uniquely bound to a specific session. Every client that accesses the application has a different session and a distinct collection of information.
Session.aspx.cs
protected void Button1_Click(object sender, EventArgs e){
Session["txtvalue"] = TextBox1.Text;Response.Redirect("Session2.aspx");
}
Session2.aspx.cs
protected void Page_Load(object sender, EventArgs e){
Label1.Text="<b>Welcome "+Session["txtvalue"];}
Session.aspx Session2.aspx
top related