跨页投递 [英] cross page posting
问题描述
我只是想跨页投递的例子。 我已经添加1文本框和放大器; 1按钮Default.aspx页
i am just trying the example of cross page posting. i have added 1 textbox & 1 button to default.aspx page
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<asp:Button ID="Button2" runat="server" Text="Button" PostBackUrl="~/About.aspx"/>
我增加了以下code至code-背后about.aspx页的文件
i have added following code to code-behind file of about.aspx page
protected void Page_Load(object sender, EventArgs e)
{
if (Page.PreviousPage != null)
{
TextBox SourceTextBox =
(TextBox)Page.PreviousPage.FindControl("TextBox1");
if (SourceTextBox != null)
{
Label1.Text = SourceTextBox.Text;
}
else
Label1.Text = "no value";
}
else
Label1.Text = "no value from previous page";
}
当我进入TextBox1中与放一些文字;点击按钮,它进入about.aspx但标签显示值没有价值,它没有显示TextBox1中的文本值,这是为什么不正常?
when i enters some text in textbox1 & clicks button, it goes to about.aspx but label shows value "no value", its not showing textbox1's text value, why this is not working properly?
推荐答案
如果您有母版页则code 页previousPage.FindControl(TextBox1的);
无法工作,因为 TextBox1的
是的ContentPlaceHolder。
,必须先找到<$ C $下C>的ContentPlaceHolder。,然后找到 TextBox1的
If you have master page then the code Page.PreviousPage.FindControl("TextBox1");
not work because the TextBox1
is under the ContentPlaceHolder.
and must first locate the ContentPlaceHolder.
and then find the TextBox1
但是有得到的值的最简单的方法如下:
But there is an easiest way to get the value as:
将这个previous页:
Place this on the previous page:
public string TextFromBox1
{
get
{
return TextBox1.Text;
}
}
和重定向页面上声明什么是对的aspx为previous页:
and on the redirect page declare what is the previous page on aspx as:
<%@ Reference Page ="~/PreviousPageName.aspx" %>
和code背后获得的价值为:
and on code behind get the value as:
if (Page.PreviousPage != null)
{
if (Page.PreviousPage is PreviousPageClassName)
{
Label1.Text = ((PreviousPageClassName)Page.PreviousPage).TextFromBox1;
}
else
{
Label1.Text = "no value";
}
}
else
Label1.Text = "no value from previous page";
这篇关于跨页投递的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!