开发者

dropdown lists on master page, dropdown lilsts manipulation(logic, events) on content page

开发者 https://www.devze.com 2023-01-10 15:39 出处:网络
I have a Content Page withdrop down lists within an update panel: <asp:UpdatePanel ID=\"upVehicleFilter\" runat=\"server\" ChildrenAsTriggers=\"false\" UpdateMode=\"Conditional\">

I have a Content Page with drop down lists within an update panel:

             <asp:UpdatePanel ID="upVehicleFilter" runat="server" ChildrenAsTriggers="false" UpdateMode="Conditional">
                <ContentTemplate>

                    <asp:DropDownList id="ddlYear" runat="server" AutoPostBack="true" OnSelectedIndexChanged="ddlYear_SelectedIndexChanged"></asp:DropDownList>
                    <asp:DropDownList id="ddlMake" runat="server" AutoPostBack="true" OnSelecte开发者_开发技巧dIndexChanged="ddlMake_SelectedIndexChanged"></asp:DropDownList>
                    <asp:DropDownList id="ddlModel" runat="server" AutoPostBack="true" OnSelectedIndexChanged="ddlModel_SelectedIndexChanged"></asp:DropDownList>
                    <asp:DropDownList id="ddlEngine" runat="server" AutoPostBack="true" OnSelectedIndexChanged="ddlEngine_SelectedIndexChanged"></asp:DropDownList>
                    <asp:DropDownList id="ddlAspiration" runat="server" AutoPostBack="true" OnSelectedIndexChanged="ddlAspiration_SelectedIndexChanged"></asp:DropDownList>
                    <asp:DropDownList id="ddlEngVin" runat="server"></asp:DropDownList>
                    <asp:ImageButton id="btnGo" runat="server" ImageUrl="/images/buttons/btn_go.gif" OnClick="btnVehicleGo_Click"></asp:ImageButton>

                </ContentTemplate>
            </asp:UpdatePanel>

the logic(events) also exists on the Content Page:

protected void ddlYear_SelectedIndexChanged(object sender, EventArgs e)...
protected void ddlMake_SelectedIndexChanged(object sender, EventArgs e)...
protected void ddlModel_SelectedIndexChanged(object sender, EventArgs e)...
protected void ddlEngine_SelectedIndexChanged(object sender, EventArgs e)...
protected void ddlAspiration_SelectedIndexChanged(object sender, EventArgs e)...
protected void btnVehicleGo_Click(object sender, ImageClickEventArgs e)...

Basically it's a cascading drop down lists. When some value has been selected on Year it will populate Make and so on.

My issue now, is I need to move the markup to Master Page and retain the Logic on Content Page. How would I be able to attain this? What are my options and/or alternatives?


Something like this would do the trick in your Content Page code behind:

protected override void OnInit(EventArgs e)
{
    base.OnInit(e);

    DropDownList ddlYear = ((SiteMaster)this.Master).FindControl("ddlYear") as DropDownList;
    ddlYear.SelectedIndexChanged += new EventHandler(ddlYear_SelectedIndexChanged);
}

void ddlYear_SelectedIndexChanged(object sender, EventArgs e)
{
    throw new NotImplementedException();
}

You will need to remove OnSelectedIndexChanged attributes from the drop down lists on the master page. You will also need to replace SiteMaster with whatever the type of your master page is.

This will work, but you might consider keeping the event handlers in the master page and exposing new events from the master page that fire when the drop downs change. This would eliminate the need for your child pages to know the names of the controls on the master page, which is not ideal.

Update: If the DropDown lists need to be in the Master solely for layout purposes, add an additional ContentTemplate to the master page. This will allow you to place the DropDowns wherever they need to appear, but maintain the logic in the Content pages. This will be cleaner than having half the code in one place, and half in another and relying on FindControl to link the two.

0

精彩评论

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

关注公众号