【问题标题】:Unable to pass data from one content page to another using master page无法使用母版页将数据从一个内容页传递到另一个内容页
【发布时间】:2014-12-25 18:45:47
【问题描述】:

我正在尝试制作一个将数据发布到另一个网络表单的 asp 网络表单。

我做了两个独立的项目,一个使用母版页,一个不使用。

塞纳里奥:

WebForm1.aspx有两个文本框和一个提交按钮

<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>&nbsp;</td>
        </tr>
        <tr>
            <td>&nbsp;</td>
            <td>
                <asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" />
            </td>
            <td>&nbsp;</td>
        </tr>
    </table>

WebForm2.aspx.cs 有两个标签,应该显示从 WebForm1.aspx 接收到的数据

Page prevPage = this.PreviousPage;
        if (prevPage != null)
        {
            Label1.Text = ((TextBox)prevPage.FindControl("TextBox1")).Text;
            Label2.Text = ((TextBox)prevPage.FindControl("TextBox2")).Text;
        }

案例 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;
        }

调试时: 我在即时窗口中执行了“collec.Count”。

案例 1:[无母版页发帖]

collec.Count 返回 5

案例 2:[使用母版页发布]

collec.Count 返回 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; } }

WebForm2.aspx.cs

WebForm1 prevPage = (WebForm1)this.PreviousPage;
        if (prevPage != null)
        {
            ControlCollection c = prevPage.Controls;
            Label1.Text = prevPage.Name;
            Label2.Text = prevPage.ID;
        }

现在它可以正常工作,即使是母版页。

那么谁能解释我发生了什么以及为什么从一个内容页面发布到另一个内容页面而主人给我 NullReferenceException ?

【问题讨论】:

    标签: asp.net master-pages nullreferenceexception content-pages


    【解决方案1】:

    首先你必须查看提交页面的内容占位符

    因此,代码看起来更像这样:

    ContentPlaceHolder placeHolder = (ContentPlaceHolder)PreviousPage.Master.FindControl("ContentPlaceHolder1");
            TextBox txt1 = (TextBox)placeHolder.FindControl("TextBox1");
    

    当您使用母版页时,在与 ContentPlaceHolder1 相关联的内容控件内的 id 为 TextBox1 的 TextBox 的 id 属性将像这样扩展:

    <input name="ctl00$ContentPlaceHolder1$TextBox1" type="text" id="ContentPlaceHolder1_TextBox1" />
    

    但是当您不使用母版页时,没有“ContentPlaceHolder”,因此 TextBox1 将呈现如下:

    <input name="TextBox1" type="text" id="TextBox1" />
    

    【讨论】:

    • 谢谢!这解决了我的问题。但是我还是不明白为什么我们从一个内容页面转移到另一个内容页面时需要Master.FindControl()?
    • 上一页是基于母版页的内容页。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多