c# - How to have a button in MasterPage and use differently in content pages -


i have button in masterpage:

<asp:button id="tyt" runat="server" clientidmode="static" onclick="tyt_click" usesubmitbehavior="false" text="submit messagebox" /> 

i want use button different thing in different pages.

i added following in masterpage:

public void tyt_click(object sender, eventargs e)         {             onmastercontrolclick(e);         }         protected void onmastercontrolclick(eventargs e)         {             if (this.mastercontrolclicked != null)             {                 this.mastercontrolclicked(this, e);             }         } 

the following in page1.aspx:

protected override void oninit(eventargs e)         {             base.oninit(e);              if (this.master site)             {                 ((site)this.master).mastercontrolclicked += new eventhandler(contentcontrolclicked);             }         }         private void contentcontrolclicked(object sender, eventargs e)         {             messagebox.show("your task");         } 

the following in page2.aspx:

protected override void oninit(eventargs e)         {             base.oninit(e);              if (this.master site)             {                 ((site)this.master).mastercontrolclicked += new eventhandler(contentcontrolclicked);             }         }         private void contentcontrolclicked(object sender, eventargs e)         {             messagebox.show("your task");         } 

only your task displayed if on page1.aspx when on page2.aspx , press button, page reloads.

how fix it?

this can solved using interface. create interface defines action or actions need.

public interface iactionpage {     void action(); } 

then have each of pages implement interface.

public partial class _default : system.web.ui.page, iactionpage {      protected void page_load(object sender, eventargs e)     {     }      public void action()     {         messagebox.show("your task");     } } 

finally, have master page's button cast current page interface , invoke method.

protected void tyt_click(object sender, eventargs e) {     var actionpage = this.page iactionpage;     if (actionpage != null)         actionpage.action(); } 

Comments

Popular posts from this blog

javascript - Jquery show_hide, what to add in order to make the page scroll to the bottom of the hidden field once button is clicked -

javascript - Highcharts multi-color line -

javascript - Enter key does not work in search box -