随笔 - 30  文章 - 13  评论 - 120  阅读 - 53127

原来2.0里实现数据绑定控件这么简单!

  1using System;
  2using System.Collections.Generic;
  3using System.ComponentModel;
  4using System.Text;
  5using System.Web;
  6using System.Web.UI;
  7using System.Web.UI.WebControls;
  8
  9
 10namespace WYN.WebControls
 11{
 12
 13    [ToolboxData("<{0}:FilterTextBox runat=server></{0}:FilterTextBox>")]
 14    public class FilterTextBox : DataBoundControl 
 15    {
 16        private ListItemCollection listSource = null;
 17        protected ListItemCollection ListSource
 18        
 19            get{
 20
 21                if (listSource == null)
 22                    listSource = new ListItemCollection();
 23
 24                return listSource;
 25            }

 26       
 27        }

 28
 29        private int selectedIndex = 0;
 30        public  int SelectedIndex
 31        {
 32            get
 33            {
 34
 35                return selectedIndex;
 36            }

 37            set
 38            {
 39                selectedIndex = value;
 40
 41                if (selectedIndex < 0 || selectedIndex > ListSource.Count)
 42                {
 43                    selectedIndex = 0;
 44                }

 45
 46                if (listSource.Count != 0)
 47                {
 48                    listSource[selectedIndex].Selected = true;
 49                }

 50            }

 51        }

 52
 53        public string DataTextField
 54        {
 55            get
 56            {
 57                object o = ViewState["DataTextField"];
 58                return ((o == null? string.Empty : (string)o);
 59            }

 60            set
 61            {
 62                ViewState["DataTextField"= value;
 63                if (Initialized)
 64                {
 65                    OnDataPropertyChanged();
 66                }

 67            }

 68        }

 69
 70        public string DataValueField
 71        {
 72            get
 73            {
 74                object o = ViewState["DataValueField"];
 75                return ((o == null? string.Empty : (string)o);
 76            }

 77            set
 78            {
 79                ViewState["DataValueField"= value;
 80                if (Initialized)
 81                {
 82                    OnDataPropertyChanged();
 83                }

 84            }

 85        }

 86
 87
 88        protected override void PerformSelect()
 89        {
 90
 91            // Call OnDataBinding here if bound to a data source using the
 92            // DataSource property (instead of a DataSourceID), because the
 93            // databinding statement is evaluated before the call to GetData.       
 94            if (!IsBoundUsingDataSourceID)
 95            {
 96                OnDataBinding(EventArgs.Empty);
 97            }

 98
 99            // The GetData method retrieves the DataSourceView object from  
100            // the IDataSource associated with the data-bound control.            
101            GetData().Select(CreateDataSourceSelectArguments(),
102                OnDataSourceViewSelectCallback);
103
104            // The PerformDataBinding method has completed.
105            RequiresDataBinding = false;
106            MarkAsDataBound();
107
108            // Raise the DataBound event.
109            OnDataBound(EventArgs.Empty);
110        }

111        private void OnDataSourceViewSelectCallback(System.Collections.IEnumerable retrievedData)
112        {
113
114            // Call OnDataBinding only if it has not already been 
115            // called in the PerformSelect method.
116            if (IsBoundUsingDataSourceID)
117            {
118                OnDataBinding(EventArgs.Empty);
119            }

120            // The PerformDataBinding method binds the data in the  
121            // retrievedData collection to elements of the data-bound control.
122            PerformDataBinding(retrievedData);
123        }

124
125
126        protected override void PerformDataBinding(System.Collections.IEnumerable retrievedData)
127        {
128            base.PerformDataBinding(retrievedData);
129
130            // If the data is retrieved from an IDataSource as an 
131            // IEnumerable collection, attempt to bind its values to a 
132            // set of TextBox controls.
133            if (retrievedData != null)
134            {
135
136                foreach (object dataItem in retrievedData)
137                {
138
139                    ListItem item = new ListItem();
140
141
142                    if (!String.IsNullOrEmpty(DataTextField))
143                    {
144                        item.Text = DataBinder.GetPropertyValue(dataItem,
145                            DataTextField, null); //以后要加上DataFormat
146
147                        item.Value = DataBinder.GetPropertyValue(dataItem,
148                            DataValueField, null);
149                    }

150                    else
151                    {
152                        PropertyDescriptorCollection props =
153                            TypeDescriptor.GetProperties(dataItem);
154
155                        // Set the "default" value of the TextBox.
156                        item.Text = "";
157                        item.Value = "";
158
159                        // Set the true data-bound value of the TextBox,
160                        // if possible.
161                        if (props.Count >= 1)
162                        {
163                            if (null != props[0].GetValue(dataItem))
164                            {
165                                item.Text = props[0].GetValue(dataItem).ToString();
166                                item.Value = props[0].GetValue(dataItem).ToString();
167                            }

168                        }

169                    }

170
171                    ListSource.Add(item);
172                }

173            }

174        }

175
176        public override void RenderBeginTag(HtmlTextWriter writer)
177        {
178            writer.WriteBeginTag("div>");
179        }

180
181        protected override void RenderContents(HtmlTextWriter output)
182        {
183            // Make sure the control is declared in a form tag 
184            // with runat=server.
185            if (Page != null)
186            {
187                Page.VerifyRenderingInServerForm(this);
188            }

189            
190
191            //生成输入文本框的Html字符串,呈现在页面
192            String inputTextBox = prepareInputBox();
193            output.Write(inputTextBox);
194
195            output.Write("<BR>");
196
197            //生成备选框的Html字符串,呈现在页面
198            String listBox = prepareListBox();
199            output.Write(listBox);  
200
201        }

202
203
204        public override void RenderEndTag(HtmlTextWriter writer)
205        {
206            writer.WriteEndTag("div");
207        }

208
209        Private
249    }

250}

251
posted on   Yunanw  阅读(260)  评论(0编辑  收藏  举报
编辑推荐:
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
阅读排行:
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· .NET周刊【3月第1期 2025-03-02】
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· [AI/GPT/综述] AI Agent的设计模式综述
< 2006年11月 >
29 30 31 1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 1 2
3 4 5 6 7 8 9

点击右上角即可分享
微信分享提示