开发者

Communication problem in master page and content page

I'm having a master page in my website and having some control on master page. I want to handle the master page controls event on content page.

how can i do that ...?

even i have used property like this for a dropdown control

public  DropDownList propertymastercontrol
{
    get
    {
        return cmbVendor;   //drop down id
    }
}

and given the reference of master page on content page like this...

<%@ MasterType VirtualPath ="~/MasterPage.master" %>开发者_Python百科

still after not able to get the any event of control on content page_load event..

protected void Page_Load(object sender, EventArgs e)
    {
        Master.
    }

what can be the problem..?


I'm not sure about C#, but in VB.NET I've done it with these four steps:

  • Add events to the MasterPage code behind, raise these events as required
  • Add this to the content aspx page: <%@ MasterType VirtualPath="~/master.Master" %>
  • Add event handlers to trap the event on the content page codebehind (this is where things get very different between C# and VB.NET)
  • Add your required code to the events in the codebehind

UPDATE:

From your sample code, I can see you're not raising an event in the MasterPage (which you can respond to the content page). I was going to give an example, but it would be almost identical to this post, Pass MasterPage ImageButton event to content Page, which another SO user already has linked to in the answers here too.


I am assuming the page_Load event is in the code behind section. You have to declare the master page file there also to be able to pull the contents of the master page.

Put this in the on preInit :

this.MasterPageFile = "~/masterPage.master"; //change this to where your master page resides.


Look at this question I had, very similar:

Key points, it works just like regular .ASPX page and UserControls. In fact, the MasterPage becomes a UserControl to the content page.

Pass MasterPage ImageButton event to content Page


The quick and dirty way is to find the control in the masterpage and wire it up. Your Page_Load will look something like this:

protected void Page_Load(object sender, EventArgs e)    
{
    DropDownList cmbVendor = (DropDownList)Master.FindControl("cmbVendor");
    cmbVendor.TextChanged += new EventHandler(cmbVendor_TextChanged);
}

The cleaner way is to have the MasterPage handle the DropDown event and raise it's own event. The accepted answer for Pass MasterPage ImageButton event to content Page explains how to do this.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜