Asp.net Webform 数据源绑定控件的扩展(懒人的办法):DropDownList

  1.  开篇的废话
  2.  实现的功能
  3.  源代码
  4.  小结

 开篇的废话

在北京回老家已经一年了,今天入职第七天。工作已近有一个多星期了,还是做老本行,码农!虽然跟正式码农的日子告别有一段时间了,但是小菜的技术始终没放下。谈谈入职后的感觉,公司所有员工加起来都40+的人吧。在我们这个不入流的小城市里面算是不小的私人企业,置于我们的开发部,算上经理也有9个人了。但是公司的技术,小菜实在不敢苟同。基本上可以这么说,做Page胡乱的设置CSS,然后拖控件,双击写代码,各种问题。Try Catch漫天飞,Sql语句是+出来的。测试昨天刚测了一个项目,120多个Bug。在并不复杂的业务流程、十几个小功能、3个登陆角色的系统里面,可以算是惊人了。测试对我说,我这还没有写测试用例,手点出来的。公司是用Asp.net Webform做开发的,vs2008 framework3.5,面对着公司的老员工我不知道说什么好,也能说什么。就连一个Bing的DropDownList都是在Page_Load里面去读取DataTable,然后DataTable.NewRow(),添加空选项,Bing到DropDownList。难道说,前辈们用了这么多年,都没写烦了?这也是我今天写这个博文的主要原因。既然公司不让用MVC,不要技术只要效果,不要质量只要能用,那么,小菜在这里来封装自己的套Webform Control.也许Control不支持Ajax,但是再也不会因为,赋值,因为Binding,因为pager,因为回传,因为搜索,而产生120+的Bug。

 实现的功能

Page Code
1  <Binarysoft:ComboBox ID="ComboBox1" TableName="RBAC_Objects" DataTextField="ObjectName" DataValueField="PKey" DefaultText="选中信息" SortName="ObjectOrderBy" runat="server">
2     </Binarysoft:ComboBox>

效果图:

支持 ConnectionName 指定连接字符串名称,支持DefaultText,是否添加非数据表内的默认选项(如果为设置 DefaultText则无默认选项),支持Where字段,支持表或者视图。支持SortName排序字段,支持对IExecuteable接口的支持。如果ConnectionName为空则使用Page的IExecuteable进行数据操作,如果没有ConnectionName 和 为实现Iexecuteable 则不检索数据。注意,只需要在Page中加入如下标记,无需后台Binding即可实现DropDownList的数据绑定。支持ISPostBack的状态区分。

  源代码

  1  [DefaultProperty("Text")]
  2     [ToolboxData("<Binarysoft:ComboBox runat=server></Binarysoft:ComboBox>")]
  3     public class ComboBox : DropDownList
  4     {
  5         private Database DataAccess;
  6 
  7         [Bindable(true)]
  8         [Category("Appearance")]
  9         [DefaultValue("")]
 10         [Localizable(true)]
 11         public string TableName
 12         {
 13             get
 14             {
 15                 String s = (String)ViewState["TableName"];
 16                 return ((s == null) ? String.Empty : s);
 17             }
 18 
 19             set
 20             {
 21                 ViewState["TableName"] = value;
 22             }
 23         }
 24 
 25         [Bindable(true)]
 26         [Category("Appearance")]
 27         [DefaultValue("")]
 28         [Localizable(true)]
 29         public string ConnectionName
 30         {
 31             get
 32             {
 33                 String s = (String)ViewState["ConnectionName"];
 34                 return ((s == null) ? String.Empty : s);
 35             }
 36 
 37             set
 38             {
 39                 ViewState["ConnectionName"] = value;
 40             }
 41         }
 42 
 43         [Bindable(true)]
 44         [Category("Appearance")]
 45         [DefaultValue("")]
 46         [Localizable(true)]
 47         public string DefaultText
 48         {
 49             get
 50             {
 51                 String s = (String)ViewState["DefaultText"];
 52                 return ((s == null) ? String.Empty : s);
 53             }
 54 
 55             set
 56             {
 57                 ViewState["DefaultText"] = value;
 58             }
 59         }
 60 
 61         [Bindable(true)]
 62         [Category("Appearance")]
 63         [DefaultValue("")]
 64         [Localizable(true)]
 65         public string Where
 66         {
 67             get
 68             {
 69                 String s = (String)ViewState["Where"];
 70                 return ((s == null) ? "1=1" : s);
 71             }
 72 
 73             set
 74             {
 75                 ViewState["Where"] = value;
 76             }
 77         }
 78 
 79         [Bindable(true)]
 80         [Category("Appearance")]
 81         [DefaultValue("")]
 82         [Localizable(true)]
 83         public string SortName
 84         {
 85             get
 86             {
 87                 String s = (String)ViewState["SortName"];
 88                 return ((s == null) ? this.DataTextField : s);
 89             }
 90 
 91             set
 92             {
 93                 ViewState["SortName"] = value;
 94             }
 95         }
 96 
 97         protected override void OnDataBinding(EventArgs e)
 98         {
 99             if (!string.IsNullOrEmpty(DataTextField) && !string.IsNullOrEmpty(DataTextField) && !string.IsNullOrEmpty(TableName))
100             {
101                 DataAccess = string.IsNullOrEmpty(ConnectionName) ?
102                     (this.Page as Binarysoft.Library.Webform.UI.Page).DataAccess :
103                     DatabaseFactory.CreateDatabase(ConnectionName);
104                 this.DataSource = DataAccess.ExecuteDataTable(string.Format("SELECT {0},{1} FROM {2} WHERE {3} ORDER BY {4}", DataTextField, DataValueField, TableName, Where, SortName));
105             }
106             base.OnDataBinding(e);
107         }
108 
109         protected override void OnDataBound(EventArgs e)
110         {
111             base.OnDataBound(e);
112             if (!string.IsNullOrEmpty(DefaultText))
113             {
114                 ListItem defaultItem = new ListItem();
115                 defaultItem.Value = string.Empty;
116                 defaultItem.Text = DefaultText;
117                 this.Items.Insert(0, defaultItem);
118             }
119         }
120 
121         protected override void OnPagePreLoad(object sender, EventArgs e)
122         {
123             if (!Page.IsPostBack)
124             {
125                 this.DataBind();
126             }
127             base.OnPagePreLoad(sender, e);
128         }
129 
130         protected override void OnLoad(EventArgs e)
131         {
132             base.OnLoad(e);
133         }
134     }

 

  小结

This may not be the best, but I try to, this is just the beginning ...

posted @ 2013-03-17 09:12  E=mc&#178;  阅读(1798)  评论(3编辑  收藏  举报