原来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
原文地址:https://www.cnblogs.com/listhome/p/549555.html