ASP.NET - To check click event of button in master page from content page

Asked By Libin Emmanuel on 07-Nov-11 10:44 PM
Hi,
     I have a master page and a content page where i have put a button in the masterpage in whih it lies outside its contentplaceholder. In the content page i need to check whether the masterpage's button is clicked or not. How will i check that. Please do help me.
Thanks
Libin
Jitendra Faye replied to Libin Emmanuel on 07-Nov-11 10:49 PM

You can directly write code inside Button_Click() event  in Master Page.

Like this-

//Button code in Master page


protected void Button1_Click(object sender, EventArgs e)

{


   Response.Write("<script>alert('Button Clicked.')</script>");

}



Try this and let me know.
Libin Emmanuel replied to Jitendra Faye on 07-Nov-11 10:57 PM
But inside this click event i wont be able to disable some of the content pages controls on this button click.isn't it? So i want the button click event to disable some controls in the content page.
Anoop S replied to Libin Emmanuel on 07-Nov-11 10:58 PM

Do not specify any button click handler in the master page, instead specify one for each content page. You may try:

In page_load for each content page:

Button btn = this.Master.FindControl("Button1") as Button; //specify your button id  

btn.Click += new System.EventHandler(myBtnClickHandler);
Jitendra Faye replied to Libin Emmanuel on 07-Nov-11 11:03 PM
You can access child page control in Master page also.

Use this code to disable child page control from master page.



//Write this code in master page

protected void Button1_Click(object sender, EventArgs e)

{


TextBox TextBox1 = (TextBox)ContentPlaceHolder1.FindControl("TextBox1");

if (TextBox1 != null)

{

TextBox1.Enabled =

false ;

}

}


Try this and let em know.



dipa ahuja replied to Libin Emmanuel on 08-Nov-11 03:38 AM
Implemnet the button event in the master file , locate the textbox of content page:

Textbox in contet page:
  <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>

button event in master file:
protected void Button1_Click(object sender, EventArgs e)
{
  TextBox txt1 = (TextBox)ContentPlaceHolder1.FindControl("TextBox1"); //get id of textbox isnide content page
  txt1.Enabled = false;
}