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

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

 26InBlock.gif       
 27ExpandedSubBlockEnd.gif        }

 28InBlock.gif
 29InBlock.gif        private int selectedIndex = 0;
 30InBlock.gif        public  int SelectedIndex
 31ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 32InBlock.gif            get
 33ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
 34InBlock.gif
 35InBlock.gif                return selectedIndex;
 36ExpandedSubBlockEnd.gif            }

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

 45InBlock.gif
 46InBlock.gif                if (listSource.Count != 0)
 47ExpandedSubBlockStart.gifContractedSubBlock.gif                dot.gif{
 48InBlock.gif                    listSource[selectedIndex].Selected = true;
 49ExpandedSubBlockEnd.gif                }

 50ExpandedSubBlockEnd.gif            }

 51ExpandedSubBlockEnd.gif        }

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

 60InBlock.gif            set
 61ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
 62InBlock.gif                ViewState["DataTextField"= value;
 63InBlock.gif                if (Initialized)
 64ExpandedSubBlockStart.gifContractedSubBlock.gif                dot.gif{
 65InBlock.gif                    OnDataPropertyChanged();
 66ExpandedSubBlockEnd.gif                }

 67ExpandedSubBlockEnd.gif            }

 68ExpandedSubBlockEnd.gif        }

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

 77InBlock.gif            set
 78ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
 79InBlock.gif                ViewState["DataValueField"= value;
 80InBlock.gif                if (Initialized)
 81ExpandedSubBlockStart.gifContractedSubBlock.gif                dot.gif{
 82InBlock.gif                    OnDataPropertyChanged();
 83ExpandedSubBlockEnd.gif                }

 84ExpandedSubBlockEnd.gif            }

 85ExpandedSubBlockEnd.gif        }

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

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

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

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

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

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

168ExpandedSubBlockEnd.gif                        }

169ExpandedSubBlockEnd.gif                    }

170InBlock.gif
171InBlock.gif                    ListSource.Add(item);
172ExpandedSubBlockEnd.gif                }

173ExpandedSubBlockEnd.gif            }

174ExpandedSubBlockEnd.gif        }

175InBlock.gif
176InBlock.gif        public override void RenderBeginTag(HtmlTextWriter writer)
177ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
178InBlock.gif            writer.WriteBeginTag("div>");
179ExpandedSubBlockEnd.gif        }

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

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

202InBlock.gif
203InBlock.gif
204InBlock.gif        public override void RenderEndTag(HtmlTextWriter writer)
205ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
206InBlock.gif            writer.WriteEndTag("div");
207ExpandedSubBlockEnd.gif        }

208InBlock.gif
209ContractedSubBlock.gifExpandedSubBlockStart.gif        Private#region Private
210ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
211InBlock.gif        /// 生成输入文本框的Html字符串
212InBlock.gif        /// </summary>
213ExpandedSubBlockEnd.gif        /// <returns></returns>

214InBlock.gif        private String prepareInputBox()
215ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
216InBlock.gif            String id = this.ClientID + "_inputBox";
217ExpandedSubBlockStart.gifContractedSubBlock.gif            String txtHtmlString = String.Format("<input name=\"dot.gif{0}\" type=\"text\" id=\"dot.gif{0}\" />", id);
218InBlock.gif
219InBlock.gif            return txtHtmlString;
220ExpandedSubBlockEnd.gif        }

221ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
222InBlock.gif        /// 生成备选框的Html字符串
223InBlock.gif        /// </summary>
224ExpandedSubBlockEnd.gif        /// <returns></returns>

225InBlock.gif        private String prepareListBox()
226ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
227InBlock.gif            String id = this.ClientID + "_listBox";
228ExpandedSubBlockStart.gifContractedSubBlock.gif            StringBuilder sbHtmlString = new StringBuilder(String.Format("<select name=\"dot.gif{0}\" id=\"dot.gif{0}\">",id));
229InBlock.gif            sbHtmlString.Append("\r\n");
230InBlock.gif            foreach (ListItem item in ListSource)
231ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
232InBlock.gif
233InBlock.gif                sbHtmlString.Append("<option value=\"");
234InBlock.gif                sbHtmlString.Append(item.Value);
235InBlock.gif
236InBlock.gif                if (item.Selected)
237InBlock.gif                    sbHtmlString.Append("Selected");
238InBlock.gif
239InBlock.gif                sbHtmlString.Append("\">");
240InBlock.gif                sbHtmlString.Append(item.Text);
241InBlock.gif                sbHtmlString.Append("</option>");
242InBlock.gif                sbHtmlString.Append("\r\n");
243ExpandedSubBlockEnd.gif            }

244InBlock.gif            sbHtmlString.Append("</select>");
245InBlock.gif            return sbHtmlString.ToString();
246InBlock.gif            
247ExpandedSubBlockEnd.gif        }

248ExpandedSubBlockEnd.gif        #endregion

249ExpandedSubBlockEnd.gif    }

250ExpandedBlockEnd.gif}

251 None.gif

转载于:https://www.cnblogs.com/listhome/archive/2006/11/03/549555.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值