母版页上的下拉列表,内容页上的下拉列表操作(逻辑,事件)

发布于 2024-09-13 06:03:05 字数 2064 浏览 4 评论 0原文

我在更新面板中有一个带有下拉列表的内容页面:

             <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>

逻辑(事件)也存在于内容页面上:

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)...

基本上它是一个级联下拉列表。当在“年份”上选择某个值时,它将填充“制造商”等。

我现在的问题是,我需要将标记移动到母版页并保留内容页上的逻辑。我怎样才能实现这一目标?我的选择和/或替代方案是什么?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

不必在意 2024-09-20 06:03:05

像这样的事情可以在您的内容页代码后面实现:

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();
}

您需要从母版页的下拉列表中删除 OnSelectedIndexChanged 属性。您还需要将 SiteMaster 替换为母版页的类型。

这可行,但您可能会考虑将事件处理程序保留在母版页中,并公开母版页中下拉列表更改时触发的新事件。这将消除您的子页面需要知道母版页上的控件名称的需要,这是不理想的。

更新:
如果下拉列表仅出于布局目的而需要位于母版页中,请向母版页添加一个额外的 ContentTemplate。这将允许您将下拉菜单放置在需要出现的任何位置,但保持内容页面中的逻辑。这比将一半代码放在一个地方,一半放在另一个地方并依靠 FindControl 链接两者要干净得多。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文