Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

asp.net

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
    DropDownList thisDropDown = this.Master.FindControl("someDropDown") as DropDownList;
    userLabel.Text = thisDropDown.SelectedValue;
}

but how could i access controls of content page from master page. suppose a textbox there in content page and one button is there in master page. i want that when i will click on master page button then i want to show the text of textbox in the content page in the label of master page. how to achieve it. please help me with code sample. thanks.

like image 895
Thomas Avatar asked Feb 12 '11 18:02

Thomas


2 Answers

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;
    }
}
like image 81
Waqas Raja Avatar answered Sep 26 '22 02:09

Waqas Raja


It's been a while, but I believe you can do so by using the ContentPlaceHolder as a reference:

Control control = this.myContentPlaceHolder.FindControl("ContentPageControlID");
like image 35
Brad Christie Avatar answered Sep 26 '22 02:09

Brad Christie