本文介绍了如何将值从一个页面传递到另一个页面。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
亲爱的, 正在使用asp C# 如何传递值一页到另一页。 我的项目有2页 在第一页我有3个文本框和一个提交按钮 在第二页我有3个标签 我输入的是什么第一页3个文本框并单击提交按钮,这3个值必须显示在第二页标签中。 请帮助如何执行此操作。 谢谢
Dear all, am working on asp C# How to pass values from one page to another page. I have 2 pages in my project In first page i have 3 textboxes and a submit button In second page i have 3 labels what ever i enter in first page 3 textboxes and click on submit button, these 3 values must display in second page labels. Please help how to do this. Thanks
推荐答案有三种方法可以做到: 1)查询字符串:wwww .mydomain / newpage.apsx?TB1Value ='hello'&TB2Value ='再见' 2)Cookie - 这是存储在客户端上并且可以持久使用 3)会话 - 这个存储在服务器上,会话结束后会立即删除。 使用哪个取决于你:但谷歌和MSDN可以帮你判断e并向您展示如何实现它们 - 它们都非常简单! There are three ways you can do it: 1) Query string: wwww.mydomain/newpage.apsx?TB1Value='hello'&TB2Value='goodbye' 2) Cookie - this is stored on the client and can be long lasting 3) Session - this is stored on the server, and will be removed as soon as the session ends. Which to use is up to you: but Google and MSDN can help you decide and show you how to implement them - they are all very easy!
您需要使用会话变量。在第一页中,在按钮单击方法中: You need to use session variables. In the first page, in the button click method: protected void yourButton_Click(object sender, EventArgs e) { Session["txtBox1"] = yourFirstTextBox.Text; Session["txtBox2"] = yourSecondTextBox.Text; Session["txtBox3"] = yourThirdTextBox.Text; Response.Redirect("SecondPage.aspx"); }
在第二页中,将其添加到 Page_Load 方法中:
And in the second page, add this in the Page_Load method:
protected void Page_Load(object sender, EventArgs e) { if (Session["txtBox1"] != null) { yourFirstLabel.Text = (string)Session["txtBox1"]; } else { // the session value "txtBox1" is null } // do this for all labels }有关ASP.NET会话的更多信息: 在ASP.NET中探索会话 [ ^ ]
on顶部 griff , programfox , sandeep 解决方案 另一个使用cookies的实例。 on top of griff,programfox,sandeep solution another live example using cookies. // in page1.aspx , button click HttpCookie mycookie = new HttpCookie("mycookie"); mycookie["text1"] = text1.Text; mycookie["text2"] = text2.Text; mycookie["text2"] = text3.Text; Response.Cookies.Add(mycookie); Response.Redirect("page2.aspx"); // in page2.aspx page load HttpCookie mycookie = Request.Cookies["mycookie"]; if (mycookie != null) { label1.Text = mycookie["text1"]; label2.Text = mycookie["text2"]; label3.Text = mycookie["text3"]; }
更多推荐
如何将值从一个页面传递到另一个页面。
发布评论