Asp.net 无法使用母版页将数据从一个内容页传递到另一个内容页
我正在尝试制作一个asp网络表单,将数据发布到另一个网络表单 我做了两个独立的项目,一个使用母版页,另一个不使用 塞纳里奥: WebForm1.aspx有两个文本框和一个提交按钮Asp.net 无法使用母版页将数据从一个内容页传递到另一个内容页,asp.net,master-pages,nullreferenceexception,content-pages,Asp.net,Master Pages,Nullreferenceexception,Content Pages,我正在尝试制作一个asp网络表单,将数据发布到另一个网络表单 我做了两个独立的项目,一个使用母版页,另一个不使用 塞纳里奥: WebForm1.aspx有两个文本框和一个提交按钮 <table> <tr> <td >Name:</td> <td > <asp:TextBox ID="TextBox1" runat="server">
<table>
<tr>
<td >Name:</td>
<td >
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
</td>
<td class="auto-style1"></td>
</tr>
<tr>
<td>Id:</td>
<td>
<asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
</td>
<td> </td>
</tr>
<tr>
<td> </td>
<td>
<asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" />
</td>
<td> </td>
</tr>
</table>
案例1:[无母版页发布]
数据正常发布
案例2:[使用母版页发布]
我得到NullReferenceException
所以我把密码分解了
Page prevPage = this.PreviousPage;
if (prevPage != null)
{
ControlCollection collec = prevPage.Controls;
Control ctrl= prevPage.FindControl("TextBox1");
TextBox txtbx = (TextBox)ctrl;
Label1.Text = txtbx.Text; //Exception raised here
Label2.Text = ((TextBox)prevPage.FindControl("TextBox2")).Text;
}
调试时:
我在即时窗口中执行了“集合计数”
案例1:[无母版页发布]
返回的集合计数5
案例2:[使用母版页发布]
集合计数返回1[为什么?]
后来,
我尝试使用公共属性传递数据
WebForm1.aspx.cs
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
Server.Transfer("WebForm2.aspx");
}
public string Name { get { return TextBox1.Text; } }
public string ID { get { return TextBox2.Text; } }
WebForm1 prevPage = (WebForm1)this.PreviousPage;
if (prevPage != null)
{
ControlCollection c = prevPage.Controls;
Label1.Text = prevPage.Name;
Label2.Text = prevPage.ID;
}
WebForm2.aspx.cs
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
Server.Transfer("WebForm2.aspx");
}
public string Name { get { return TextBox1.Text; } }
public string ID { get { return TextBox2.Text; } }
WebForm1 prevPage = (WebForm1)this.PreviousPage;
if (prevPage != null)
{
ControlCollection c = prevPage.Controls;
Label1.Text = prevPage.Name;
Label2.Text = prevPage.ID;
}
现在,即使使用母版页,它也能正常工作
那么,有谁能解释一下发生了什么事情,为什么从一个内容页发布到另一个内容页,而master给了我NullReferenceException吗?首先,你必须查看提交页面的内容占位符 因此,代码看起来更像这样:
ContentPlaceHolder placeHolder = (ContentPlaceHolder)PreviousPage.Master.FindControl("ContentPlaceHolder1");
TextBox txt1 = (TextBox)placeHolder.FindControl("TextBox1");
<input name="ctl00$ContentPlaceHolder1$TextBox1" type="text" id="ContentPlaceHolder1_TextBox1" />
<input name="TextBox1" type="text" id="TextBox1" />
当您使用母版页时,在与ContentPlaceholder 1关联的内容控件中,id为TextBox1的文本框的id属性将扩展如下:
ContentPlaceHolder placeHolder = (ContentPlaceHolder)PreviousPage.Master.FindControl("ContentPlaceHolder1");
TextBox txt1 = (TextBox)placeHolder.FindControl("TextBox1");
<input name="ctl00$ContentPlaceHolder1$TextBox1" type="text" id="ContentPlaceHolder1_TextBox1" />
<input name="TextBox1" type="text" id="TextBox1" />
但是,当您不使用母版页时,没有“ContentPlaceHolder”,因此TextBox1的呈现方式如下:
ContentPlaceHolder placeHolder = (ContentPlaceHolder)PreviousPage.Master.FindControl("ContentPlaceHolder1");
TextBox txt1 = (TextBox)placeHolder.FindControl("TextBox1");
<input name="ctl00$ContentPlaceHolder1$TextBox1" type="text" id="ContentPlaceHolder1_TextBox1" />
<input name="TextBox1" type="text" id="TextBox1" />
谢谢!这解决了我的问题。但我仍然不明白,当我们从一个内容页传输到另一个内容页时,为什么需要Master.FindControl()?上一页是基于母版页的内容页。