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()后,即刷新了下拉框中數據源。
