自定义控件(14)编写一个简单的数据绑定控件
2017-10-18 10:24
357 查看
接下来我会定义一个简单的数据绑定控件,把数据库中的数据显示在网页上。
控件代码
显示结果
控件代码
using System; using System.Collections; using System.Collections.Generic; using System.ComponentModel; using System.Linq; using System.Text; using System.Web.UI; using System.Web.UI.WebControls; namespace PZY.BLL.Web.SimpleListView { public class SimpleListView : CompositeDataBoundControl { protected override HtmlTextWriterTag TagKey { get { return HtmlTextWriterTag.Ul; } } private ITemplate _itemTemplate; [TemplateContainer(typeof(ListViewItem))] [PersistenceMode(PersistenceMode.InnerProperty)] [Browsable(false)] [EditorBrowsable(EditorBrowsableState.Advanced)] public ITemplate ItemTemplate { get { return _itemTemplate; } set { _itemTemplate = value; } } protected override int CreateChildControls(IEnumerable dataSource, bool dataBinding) { int index = 0; Controls.Clear(); if (ItemTemplate != null) { //数据库中每行数据 foreach (object dataItem in dataSource) { ListViewItem listViewItem = new ListViewItem(dataItem, index, index); ItemTemplate.InstantiateIn(listViewItem); Controls.Add(listViewItem); index++; } //绑定 DataBind(false); ChildControlsCreated = true; } return index; } } [ToolboxItem(false)] public class ListViewItem : WebControl, IDataItemContainer { private object _dataItem; private int _index; private int _displayIndex; #region IDataItemContainer 成员 public object DataItem { get { return _dataItem; } } public int DataItemIndex { get { return _index; } } public int DisplayIndex { get { return _displayIndex; } } #endregion protected override HtmlTextWriterTag TagKey { get { return HtmlTextWriterTag.Li; } } public ListViewItem() { } public ListViewItem(object dataItem, int index, int displayIndex) { this._dataItem = dataItem; this._index = index; this._displayIndex = displayIndex; } } }html代码
<div> <cc1:SimpleListView ID="SimpleListView1" DataSourceID="sdsProducts" runat="server"> <ItemTemplate> <div> ISDN: <asp:Label ID="ISDNLabel" runat="server" Text='<%# Eval("id") %>'></asp:Label> Title: <asp:Label ID="TitleLabel" runat="server" Text='<%# Eval("name") %>'></asp:Label> Author: <asp:Label ID="AuthorLabel" runat="server" Text='<%# Eval("sort") %>'></asp:Label> </div> </ItemTemplate> </cc1:SimpleListView> <asp:SqlDataSource ID="sdsProducts" runat="server" ConnectionString="<%$ ConnectionStrings:myConn %>" OldValuesParameterFormatString="original_{0}" SelectCommand="select Id,Name,Sort from ArticleType"> <UpdateParameters> <asp:Parameter Name="Id" /> <asp:Parameter Name="Name" /> <asp:Parameter Name="Sort" /> </UpdateParameters> </asp:SqlDataSource> </div>
显示结果
相关文章推荐
- 终于解决了一个问题--如何在数据绑定时不让combox控件触发SelectedIndexChanged事件
- ADO.NET Entity Framework 一个简单数据绑定例子
- 关于数据绑定的一个小小的总结:绑定数据到List类型的控件(RadioButtonList,ListBox等),双重绑定。
- 使用objectdatasource结合数据绑定控件进行简单三层架构的开发
- winform有一个combobox和一个textbox控件,如何进行动态绑定,使得combobox中的数据改变时,textbox中的数据也随之改变?
- 编写简单一个程序 将一个长整型数据 写入字节数组中,在从字节数组中读出来
- 最近编写一个日历控件,对包含有日期及时间记录的数据查询很方便
- Demo: Dictionary>> 泛型读取XML文件数据作为数据源(DataSource)绑定到DropDownList控件 定义个一个公用类
- Winform开发常用控件之DataGridView的简单数据绑定——代码绑定DataSet、DataTable、IList、SqlDataReader
- asp.net下的“Eval()、XPath() 和 Bind() 这类数据绑定方法只能在数据绑定控件的上下文中使用。”错误的一个可能的成因
- 使用objectdatasource结合数据绑定控件进行简单三层架构的开发
- 一个显示地图数据,并实现简单浏览查询程序源码(SDK编写)
- 在FormView控件中嵌套数据绑定控件的问题,以及从一个对象中反射获取属性值的简单办法
- 数据传不到。BLL层,多个控件绑定了一个数据字段。
- VB.Net自己写的一个控件:ComboBox下拉列表中显示多列数据(可以绑定数据表)
- 数据源控件与数据绑定控件的进一步简单讨论(1)
- asp.net Xml绑定到数据控件的两种简单方法
- 一个简单控件的编写--系统托盘
- WebForm简单控件数据绑定
- C#用继承实现自定义控件 让控件默认绑定一个事件