前端页面代码:
<style type="text/css"> #DataList1 { border:solid 1px #ff0000; width:600px;} #GridView1 { border:solid 1px #00ff00; margin-top:10px; width:600px;} .Repeater { border:solid 1px #0000ff; margin-top:5px; width:600px;} </style> <asp:DataList ID="DataList1" runat="server"> <ItemTemplate> <table border="0"> <tr> <td> <%#Eval("UserSite")%> <%#Eval("UserName")%> </td> <td> <%#Eval("UserAge")%> <%#Eval("UserSex")%> </td> </tr> </table> </ItemTemplate> </asp:DataList> <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" GridLines="None" UseAccessibleHeader="False" OnRowDataBound="GridView1_RowDataBound"> <Columns> <asp:TemplateField HeaderText="站点"> <ItemTemplate> <asp:Label ID="UserSite" runat="server" Text='<%#Bind("UserSite") %>'></asp:Label> </ItemTemplate> <ItemStyle HorizontalAlign="Center"/> </asp:TemplateField> <asp:BoundField DataField="UserName" HeaderText="姓名" /> <asp:BoundField DataField="UserAge" HeaderText="年龄" /> <asp:BoundField DataField="UserSex" HeaderText="性别" /> </Columns> </asp:GridView> <asp:Repeater ID="Repeater1" runat="server"> <ItemTemplate> <table border="0" class="Repeater"> <tr> <td> <%#Eval("UserSite")%> <%#Eval("UserName")%> </td> <td> <%#Eval("UserAge")%> <%#Eval("UserSex")%> </td> </tr> </table> </ItemTemplate> </asp:Repeater>
使用C#实例:
private DataTable dt = new DataTable(); private DataView dv = new DataView(); protected void Page_Load(object sender, EventArgs e) { if (!Page.IsPostBack) { #region 基础数据 dt = new DataTable(); dt.Columns.Add("UserName"); dt.Columns.Add("UserAge"); dt.Columns.Add("UserSex"); dt.Columns.Add("UserSite"); DataRow dr = dt.NewRow(); dr["UserName"] = "郑德才"; dr["UserAge"] = "27"; dr["UserSex"] = "男"; dr["UserSite"] = "http://www.zhengdecai.com"; dt.Rows.Add(dr); dr = dt.NewRow(); dr["UserName"] = "旺旺"; dr["UserAge"] = "29"; dr["UserSex"] = "女"; dr["UserSite"] = "http://www.zhengdecai.cn"; dt.Rows.Add(dr); dv = new DataView(dt); #endregion bindDataInfo(); } } /// <summary> /// 数据绑定 /// </summary> private void bindDataInfo() { BindDataHelper.BindDataList(DataList1, dv); BindDataHelper.BindGridView(GridView1, dv); BindDataHelper.BindRepeater(Repeater1, dv); }
类库信息:
/// <summary> /// 绑定服务器数据控件大全 /// </summary> public class BindDataHelper { #region 绑定服务器数据控件 简单绑定DataList /// <summary> /// 简单绑定DataList /// </summary> /// <param name="ctrl">控件ID</param> /// <param name="mydv">数据视图</param> public static void BindDataList(Control ctrl, DataView mydv) { ((DataList)ctrl).DataSourceID = null; ((DataList)ctrl).DataSource = mydv; ((DataList)ctrl).DataBind(); } #endregion #region 绑定服务器数据控件 SqlDataReader简单绑定DataList /// <summary> /// SqlDataReader简单绑定DataList /// </summary> /// <param name="ctrl">控件ID</param> /// <param name="mydv">数据视图</param> public static void BindDataReaderList(Control ctrl, SqlDataReader mydv) { ((DataList)ctrl).DataSourceID = null; ((DataList)ctrl).DataSource = mydv; ((DataList)ctrl).DataBind(); } #endregion #region 绑定服务器数据控件 简单绑定GridView /// <summary> /// 简单绑定GridView /// </summary> /// <param name="ctrl">控件ID</param> /// <param name="mydv">数据视图</param> public static void BindGridView(Control ctrl, DataView mydv) { ((GridView)ctrl).DataSourceID = null; ((GridView)ctrl).DataSource = mydv; ((GridView)ctrl).DataBind(); } #endregion #region 绑定Repeater /// <summary> /// 绑定服务器控件 简单绑定Repeater /// </summary> /// <param name="ctrl">控件ID</param> /// <param name="mydv">数据视图</param> public static void BindRepeater(Control ctrl, DataView mydv) { ((Repeater)ctrl).DataSourceID = null; ((Repeater)ctrl).DataSource = mydv; ((Repeater)ctrl).DataBind(); } #endregion }
显示效果:
以上类库内容来源互联网,站长稍作整理
发表评论:
◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。