标签:
WebForm中,若一个asp页面中需要有很多个DropDownList控件的话,在后台即需写很多的绑定数据源方法,代码量较多且不易维护;然后咱自己就写了个用户自定义的控件,便于重用;但所牺牲的即是程序上的效率;话不多说,直接上代码:
控件源代码:
<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="WUCDropDownList.ascx.cs" Inherits="DWTWPS.CommonHelper.UserControls.WUCDropDownList" %> <asp:DropDownList ID="ddlTarget" runat="server" OnSelectedIndexChanged="DoSelectedIndexChanged"></asp:DropDownList>
控件后台.cs代码:
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Linq; using System.Reflection; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; namespace DWTWPS.CommonHelper.UserControls { public partial class WUCDropDownList : System.Web.UI.UserControl { private Repeater _rp; [Browsable(true)] public string TextField { get { return this.ddlTarget.DataTextField; } set { this.ddlTarget.DataTextField = value; } } [Browsable(true)] public string ValueField { get { return this.ddlTarget.DataValueField; } set { this.ddlTarget.DataValueField = value; } } [Browsable(true)] public string SelectedValue { get { return this.ddlTarget.SelectedValue; } set { this.ddlTarget.SelectedValue = value; } } [Browsable(true)] public string SelectedText { get { return this.ddlTarget.SelectedItem.ToString(); } } [Browsable(true)] public int SelectedIndex { get { return this.ddlTarget.SelectedIndex; } set { this.ddlTarget.SelectedIndex = value; } } [Browsable(true)] public string CssClass { get { return this.ddlTarget.CssClass; } set { this.ddlTarget.CssClass = value; } } [Browsable(true)] public bool AutoPostBack { get { return this.ddlTarget.AutoPostBack; } set { this.ddlTarget.AutoPostBack = value; } } [Browsable(true)] [DefaultValue(true)] public bool Enabled { get { return ddlTarget.Enabled; } set { ddlTarget.Enabled = value; } } [Browsable(true)] [DefaultValue(true)] public DataTable DataSource { get { return ddlTarget.DataSource as DataTable; } set { ddlTarget.DataSource = value; } } private string _dataAssembly = "Wps.BLL"; //程序集名:默认Wps.BLL [Browsable(true)] public string DataAssembly { get { return _dataAssembly; } set { _dataAssembly = value; } } //类名 [Browsable(true)] public string DataAssemblyInstance { get; set; } //方法名 [Browsable(true)] public string FunctionName { get; set; } //可执行语句 [Browsable(true)] public string SqlToFunc { get; set; } //筛选参数的集合 public Dictionary<string, string> ParameterDic { get; set; } private bool _isFirstViewData = true; //第一次是否数据可见 [Browsable(true)] public bool IsFirstViewData { get { return _isFirstViewData; } set { _isFirstViewData = value; } } private string _firstValueValue = "0"; [Description("下拉数据的第一个值")] [Browsable(true)] [DefaultValue("0")] public string FirstVauleValue { get { return _firstValueValue; } set { _firstValueValue = value; } } private string _firstTextValue = "-请选择-"; //下拉数据的一个文本值 public string FirstTextValue { get { return _firstTextValue; } set { _firstTextValue = value; } } public WUCDropDownList() { BaseSelectedIndexChangedCancel = false; } public bool BaseSelectedIndexChangedCancel { get; set; } [Browsable(true)] public event EventHandler SelectedIndexChanged; protected void Page_Load(object sender, EventArgs e) { if (!Page.IsPostBack) { DataBind(); } } protected void Page_Init(object sender, EventArgs e) { Control c = Parent; _rp = c.FindControl("Repeater") as Repeater; while (c != null && _rp == null) { _rp = c.FindControl("Repeater") as Repeater; c = c.Parent; } } public override void DataBind() { //第一次数据不可见 if (!IsFirstViewData) { Clear(); return; } DataTable tbl = new DataTable(); object obj = null; if (!string.IsNullOrEmpty(_dataAssembly) && !string.IsNullOrEmpty(DataAssemblyInstance)) { var instance = Assembly.Load(_dataAssembly).CreateInstance(_dataAssembly + "." + DataAssemblyInstance); if (instance != null) { Type function = instance.GetType(); if (ParameterDic != null && ParameterDic.Count > 0) { obj = function.InvokeMember(FunctionName, BindingFlags.InvokeMethod, null, instance, new object[] {ParameterDic, TextField + "," + ValueField}); } else obj = function.InvokeMember(FunctionName, BindingFlags.InvokeMethod, null, instance, new object[] {SqlToFunc}); } } else { obj = DataSource; } if (obj is DataTable) { tbl = (DataTable)obj; if (tbl != null && tbl.Rows.Count > 0) { DataRow dr = tbl.NewRow(); dr[ValueField] = _firstValueValue; dr[TextField] = _firstTextValue; tbl.Rows.InsertAt(dr, 0); ddlTarget.DataSource = tbl; ddlTarget.DataBind(); } else { ddlTarget.Items.Clear(); ddlTarget.DataSource = tbl; ddlTarget.DataBind(); } } } public void Clear() { ddlTarget.Items.Clear(); ddlTarget.Items.Insert(0, new ListItem(_firstTextValue, _firstValueValue)); } protected void DoSelectedIndexChanged(object sender, EventArgs e) { if (SelectedIndexChanged != null) { SelectedIndexChanged(sender, e); } } } }
到此,我们即完成了自定义控件的开发,下面我们来看看怎么使用的;
html中控件的引入:
<%@ Register Src="~/CommonHelper/UserControls/WUCDropDownList.ascx" TagPrefix="uc1" TagName="WUCDropDownList" %>
<div class="region fl border_style clear region_03"> <div class="border_left fl"></div> <div class="border_mid fl"> <uc1:WUCDropDownList runat="server" ID="ddlGuiGe" AutoPostBack="true" OnSelectedIndexChanged="ddlS_SelectedIndexChanged" IsFirstViewData="False" DataAssemblyInstance="Mat_GuiGeBLL" TextField="Mat_GuiGe_Title" ValueField="Mat_GuiGe_ID" FunctionName="GetMatGuiGe" SqlToFunc="SELECT Mat_GuiGe_Title,Mat_GuiGe_ID FROM Mat_GuiGe" FirstTextValue="-选择规格-" FirstVauleValue="0" CssClass="sWidth" /> </div> <div class="border_right fl"></div> </div>
到此,运行该页,其实已经可以将数据绑定出来了的;但很多时候,都会设计到下拉的多级联动,则此时,需要再页面的.cs代码中写上代码,进行更改二级下拉框的数据和绑定,代码如下:
protected void ddlKeMu2_SelectedIndexChanged(object sender, EventArgs e) { string selEd = ddlKeMu2.SelectedValue; if (selEd != "0") { Dic.Clear(); Dic.Add("CLT_Id_Three", selEd); Dic.Add("Mat_GuiGe_JinCheng", "0"); ddlGuiGe.ParameterDic = Dic; ddlGuiGe.IsFirstViewData = true; ddlGuiGe.DataBind(); } else ddlGuiGe.Clear(); }
调用了ddlGuiGe.DataBind()后,即刷新了下拉框中数据源。
标签:
原文地址:http://www.cnblogs.com/liuqichun/p/4499649.html