views:

108

answers:

1

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" OnSelectedIndexChanged="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?

+1  A: 

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.

sgriffinusa
Tried it. Though, I am having issues with the content, seems like an issue with the update panel. Issue #1: It's not refreshing the content. The event was triggered though. Issue #2: When I select a value on the dropdown for the 2nd time, it refreshing the entire page and the contents are invalid. I just tested it on the first DropDownList I have, which is ddlYear. And On my ddlYears event, I have this code:
koderoid
protected void ddlYear_SelectedIndexChanged(object sender, EventArgs e) { HtmlGenericControl divVehicleFilter = (HtmlGenericControl)this.Master.FindControl("divVehicleFilter"); UpdatePanel upVehicleFilter = (UpdatePanel)divVehicleFilter.FindControl("upVehicleFilter"); DropDownList ddlMake = (DropDownList)upVehicleFilter.FindControl("ddlMake"); DropDownList ddlModel = (DropDownList)upVehicleFilter.FindControl("ddlModel"); DropDownList ddlEngine = (DropDownList)upVehicleFilter.FindControl("ddlEngine");
koderoid
DropDownList ddlAspiration = (DropDownList)upVehicleFilter.FindControl("ddlAspiration"); DropDownList ddlEngVin = (DropDownList)upVehicleFilter.FindControl("ddlEngVin"); //Make ddlMake.Items.Clear(); ddlMake.Items.Add(new ListItem("Select Make...", "-1")); using (DataTable dt = GetVehicleMake()) { foreach (DataRowView rv in dt.DefaultView) { ddlMake.Items.Add(new ListItem(rv["Make"].ToString().Trim(), rv["Make"].ToString().Trim())); } }
koderoid
ddlMake.Enabled = true; ddlModel.Enabled = false; ddlEngine.Enabled = false; ddlAspiration.Enabled = false; ddlEngVin.Enabled = false; ddlModel.SelectedIndex = 0; ddlEngine.SelectedIndex = 0; ddlAspiration.SelectedIndex = 0; ddlEngVin.SelectedIndex = 0; upVehicleFilter.Update(); }
koderoid
I'd like to thank you, sgriffinusa, for helping me on this matter. The solution looks promising.
koderoid
You really should consider putting all of the logic in the Master page. It would help to alleviate the issues that you are having. What is the reason for keeping the logic in the child pages?
sgriffinusa
The logic on the content page defines the logic on the DropDowns. It's a drill down filter approach. I have 2 types of Filters. Vehicles and Attributes. Both filters are optional. The attributes filter I have is kinda like similar to newegg.com's approach, called "Guided Search". the Attributes Filter has no issue because I am creating it dynamically and they're just <a /> tags, unlike the Drop Downs.
koderoid
Is the reason for putting the DropDowns in the master page solely for layout purposes? If so, you can add an additional ContentTemplate to your master page that will allow you to put the drop downs wherever you want within the master, but maintain the logic in the content pages.
sgriffinusa
Exactly what I need. Man you nailed it. I did not know you can do that. New knowledge for me. This will be very useful for other stuffs. Very powerful. I was in the middle of re-organizing the codes based on your first solution, until I saw your 2nd solution. It works flawlessly. Thank you very much.
koderoid
Anytime, glad to be of assistance.
sgriffinusa