我在更新面板中有一个包含下拉列表的内容页面:
<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)...
基本上它是一个级联下拉列表。如果在Year上选择了某个值,它将填充Make等等。
我现在的问题是,我需要将标记移动到母版页并保留内容页面上的逻辑。我怎么能达到这个目的?我有哪些选择和/或替代方案?
答案 0 :(得分:1)
这样的内容会在你的内容页面代码中起到作用:
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替换为您的母版页的任何类型。
这将有效,但您可以考虑将事件处理程序保留在母版页中,并在下拉列表更改时从主页面中公开新事件。这样就不需要让子页面知道母版页上控件的名称,这是不理想的。
更新: 如果DropDown列表仅需要在Master中用于布局目的,请将另一个ContentTemplate添加到母版页。这将允许您将DropDowns放置在需要它们出现的位置,但是在内容页面中维护逻辑。这将比在一个地方拥有一半代码更清洁,而在另一个地方拥有一半,并依赖FindControl来链接这两个代码。