本文主要是介绍asp.net Repeater等数据控件模版内部2个DropDownList控件级联,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
1、新建一个从DropDownList继承的扩展类CommandableDropDownList.cs,代码如下:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ComponentModel;
using System.Web.UI.WebControls;namespace GrowthMonitor.BaseClass
{public class CommandableDropDownList : DropDownList{#region 属性[Browsable(true),DefaultValue("")]public string CommandName{get{return ViewState["CommandName"] == null ? String.Empty : ViewState["CommandName"].ToString();}set{ViewState["CommandName"] = value;}}[Browsable(true),DefaultValue("")]public string CommandArgument{get{return ViewState["CommandArgument"] == null ? String.Empty : ViewState["CommandArgument"].ToString();}set{ViewState["CommandArgument"] = value;}}#endregion#region 构造器public CommandableDropDownList(){//// TODO: 在此处添加构造函数逻辑//}#endregion#region 事件private static object _commandEvent = new object();public event CommandEventHandler SelectedChangedCommand{add{Events.AddHandler(_commandEvent, value);}remove{Events.RemoveHandler(_commandEvent, value);}}protected virtual void OnCommand(CommandEventArgs e){CommandEventHandler commandHander = (CommandEventHandler)Events[_commandEvent];if (commandHander != null){commandHander(this, e);}else{base.RaiseBubbleEvent(this, e);}}#endregion#region 覆写的方法protected override void OnSelectedIndexChanged(EventArgs e){base.OnSelectedIndexChanged(e);if (this.AutoPostBack){CommandEventArgs args = new CommandEventArgs(this.CommandName, this.CommandArgument);OnCommand(args);}}#endregion}
}
2、在需要使用的页面头部加上如下标记:
<%@ Register TagPrefix="ddlTag" Namespace="GrowthMonitor.BaseClass" Assembly="GrowthMonitor" %>
3、在Repeater中的DropDownList
<asp:Repeater ID="rpt" runat="server" OnItemDataBound="rpt_OnItemDataBound" OnItemCommand="rpt_OnItemCommand"><ItemTemplate><ddlTag:CommandableDropDownList ID="ddlVarietyType" runat="server" CommandName="VarietyTypeChange" AutoPostBack="true"></ddlTag:CommandableDropDownList><ddlTag:CommandableDropDownList ID="ddlVarietyName" runat="server"></ddlTag:CommandableDropDownList></ ItemTemplate>
</asp:Repeater>
4、后台事件代码
public void rpt_OnItemCommand(object sender, RepeaterCommandEventArgs e)
{if (e.CommandName == "VarietyTypeChange"){string currentVarietyTypeID = String.Empty;int index = e.Item.ItemIndex;CommandableDropDownList ddl = this.rpt.Items[index].FindControl("ddlVarietyType") as CommandableDropDownList;if (ddl != null){currentVarietyTypeID = ddl.SelectedItem.Value.Trim();}ddl = this.rpt.Items[index].FindControl("ddlVarietyName") as CommandableDropDownList;if (ddl != null){ddl.DataSource = new VTB_VarietiesBLL().GetData(" IsDelete=0 and VarietyTypeID=" + currentVarietyTypeID, " CreateTime asc");ddl.DataTextField = "VarietiesName";ddl.DataValueField = "ID";ddl.DataBind();}}
}public void rpt_OnItemDataBound(object sender, RepeaterItemEventArgs e)
{if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem){//品种类型CommandableDropDownList ddl_VarietyType = e.Item.FindControl("ddlVarietyType") as CommandableDropDownList;if (ddl_VarietyType != null){ddl_VarietyType.DataSource = new VTB_VarietyTypeBLL().GetData(" IsDelete=0 ", " CreateTime asc");ddl_VarietyType.DataTextField = "VarietyTypeName";ddl_VarietyType.DataValueField = "ID";ddl_VarietyType.DataBind();}//品种CommandableDropDownList ddl_Variety = e.Item.FindControl("ddlVarietyName") as CommandableDropDownList;if (ddl_Variety != null){ddl_Variety.DataSource = new VTB_VarietiesBLL().GetData(" IsDelete=0 and VarietyTypeID=" + ddl_VarietyType.SelectedItem.Value, " CreateTime asc");ddl_Variety.DataTextField = "VarietiesName";ddl_Variety.DataValueField = "ID";ddl_Variety.DataBind();}}
}
这篇关于asp.net Repeater等数据控件模版内部2个DropDownList控件级联的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!