How to access content page controls from master page in asp.net

后端 未结 7 1041
一生所求
一生所求 2020-12-13 16:04

it is very easy to access master page control from content page like

protected void Page_Load(object sender, EventArgs e)
{
    // content page load event
          


        
相关标签:
7条回答
  • 2020-12-13 16:16

    In master page button click event should access page contents by:-

    protected void Button1_Click(object sender, EventArgs e)
    {
        TextBox TextBox1 = (TextBox)ContentPlaceHolder1.FindControl("TextBox1");
        if (TextBox1 != null)
        {
            Label1.Text = TextBox1.Text;
        }
    }
    
    0 讨论(0)
  • 2020-12-13 16:19

    C# code within Site.Master:

    <div>
      <asp:ContentPlaceHolder ID="MainContent" runat="server">
      </asp:ContentPlaceHolder>
    </div>
    

    Code within Site.Master.cs:

    public partial class SiteMaster : MasterPage
    {
        protected void Page_Load(object sender, EventArgs e)
        {          
            ToolkitScriptManager1.RegisterPostBackControl(this.MainContent.FindControl("btnOnDefaultPage"));            
        }
    }
    

    This is an example how a client control on the Default.aspx is referenced from a Site.Master.cs

    0 讨论(0)
  • 2020-12-13 16:20

    In my opinion it even better to use event raise from Master page and catch this event in contenet page for changing some contenet on this page, for instance. The main advantage is reusability. In future you may want to change content on other content page from the Master page and in this case you should only add event handler to this content page without changing code on master page. Within such approach you needn't hardcode control name from some content page. And moreover you shouldn't add dependency for some content's control at all.

    A sample of implementation you can find here, for example.

    0 讨论(0)
  • 2020-12-13 16:22

    you should look for contentplaceholder from master page then contentplaceholder in child of the master page

    this.Page.Master.FindControl("ContentPlaceHolder1").FindControl("controlAFromPage");
    
    0 讨论(0)
  • 2020-12-13 16:23

    You can find control by using this:

    ContentPlaceHolder contentPage = Page.MasterPage.FindControl("ContentPlaceHolder1") as ContentPlaceHolder;
    Label lblHead =(Label)contentPage.FindControl("lblHeading");
    Response.Write(lblHead.Text);
    

    Source: http://xpode.com/ShowArticle.aspx?ArticleId=629

    0 讨论(0)
  • 2020-12-13 16:27

    Try this code

    Page.Master.FindControl("MainContent").FindControl("DivContainer_MyProfile").Visible = True
    
    0 讨论(0)
提交回复
热议问题