访问内容页面控件

时间:2011-10-23 05:24:48

标签: asp.net master-pages

我正在使用以下代码访问母版页内容页面内的控件

 Button btn = (Button)ContentPlaceHolder2.FindControl("btnProceed");
            btn.Text="test";

它确实在内容页面中找到控件并运行异常。但是按钮文本没有改变。在内容页面中btnProceed文本字段设置为“继续”。我需要的是当我点击一个时主页面内容页面上的imageButton btnProceed按钮文本应该更改为“test”,这当前没有发生。这个问题的原因是什么?

2 个答案:

答案 0 :(得分:0)

你可以尝试这样......

 Button btn= Master.FindControl("ContentPlaceHolder2").FindControl("btnProceed") as Button;
 btn.Text ="test"; 

答案 1 :(得分:0)

内容页面上的按钮是通过标记还是在运行时创建的? 如果它在标记中,以下代码工作正常..

主页面上的图像按钮点击处理程序

protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
    {
        Button btn = ContentPlaceHolder1.FindControl("Button1") as Button;

        btn.Text = "Proceed";
    }

如果我们在内容page.aspx中有这样的内容:

<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
    <asp:Button ID="Button1" runat="server" Text="Button" />
</asp:Content>