ASP.NET服务器控件数据绑定总结

  1 using System;
  2 using System.Collections.Generic;
  3 using System.Text;
  4 using System.Web.UI.WebControls;//
  5 using System.Data.SqlClient;
  6 using System.Data;//
  7 using System.Web.UI;//
  8 using Wuqi.Webdiyer;//此控件请在网上下载
  9 
 10 namespace Study
 11 {
 12     public sealed class WebControlsBind
 13     {
 14        #region DropDownlist 绑定
 49         /// <summary>
 50         /// 绑定DropDownList控件
 51         /// </summary>
 52         /// <param name="p_ddlControl">控件</param>
 53         /// <param name="p_drSource">数据源</param>
 54         /// <param name="p_strText">文本值</param>
 55         /// <param name="p_strValue"></param>
 56         /// <param name="p_blAll">是否包含全部项,若不包含返回false,若包含返回true</param>
 57         public static void BindDDL(DataTable p_drSource, DropDownList p_ddlControl, string p_strText, string p_strValue, bool p_blAll)
 58         {
 59             p_ddlControl.DataSource = p_drSource;
 60             p_ddlControl.DataTextField = p_strText;
 61             p_ddlControl.DataValueField = p_strValue;
 62             p_ddlControl.DataBind();
 63             if (p_blAll == true)
 64             {
 65                 ListItem li = new ListItem("全部", "0");
 66                 p_ddlControl.Items.Insert(0, li);//使用add方法只能添加到最后,使用Insert方法可以添加到指定的位置
 67 
 68             }
 69         }
 70 
 71         #endregion
 72 
 73         #region 带分页的数据绑定Repeater控件
 74         /// <summary>
 75         /// 带分页的Repeater的数据绑定
 76         /// </summary>
 77         /// <param name="dsSource">数据源</param>
 78         /// <param name="rpt">Repeater控件</param>
 79         /// <param name="anp">分页控件</param>
 80         public static void Bindrpt(DataTable dsSource, Repeater rpt, AspNetPager anp)
 81         {
 82             DataView dv = dsSource.DefaultView;
 83             anp.RecordCount = dv.Count;
 84             if (dv.Count > 0)
 85             {
 86                 PagedDataSource pds = new PagedDataSource();
 87                 pds.DataSource = dv;
 88                 pds.AllowPaging = true;
 89                 pds.CurrentPageIndex = anp.CurrentPageIndex - 1;
 90                 pds.PageSize = anp.PageSize;
 91                 rpt.DataSource = pds;
 92                 rpt.DataBind();
 93 
 94             }
 95 
 96 
 97         }
 98         public static void Bindrpt(DataTable dsSource, Repeater rpt, AspNetPager anp,string order)
 99         {
100             DataView dv = dsSource.DefaultView;
101             dv.Sort = order;
102             DataTable dt = dv.ToTable();
103             anp.RecordCount = dv.Count;
104             if (dv.Count > 0)
105             {
106                 PagedDataSource pds = new PagedDataSource();
107                 pds.DataSource = dv;
108                 pds.AllowPaging = true;
109                 pds.CurrentPageIndex = anp.CurrentPageIndex - 1;
110                 pds.PageSize = anp.PageSize;
111                 rpt.DataSource = pds;
112                 rpt.DataBind();
113 
114             }
115 
116 
117         }
118 
119         #endregion
120         #region 带分页的数据绑定Datalist控件
121         /// <summary>
122         /// 带分页的Repeater的数据绑定
123         /// </summary>
124         /// <param name="dsSource">数据源</param>
125         /// <param name="dlt">DataList控件</param>
126         /// <param name="anp">分页控件</param>
127         public static void BindDlt(DataTable dsSource, DataList dlt, AspNetPager anp)
128         {
129             DataView dv = dsSource.DefaultView;
130             anp.RecordCount = dv.Count;
131             if (dv.Count > 0)
132             {
133                 PagedDataSource pds = new PagedDataSource();
134                 pds.DataSource = dv;
135                 pds.AllowPaging = true;
136                 pds.CurrentPageIndex = anp.CurrentPageIndex - 1;
137                 pds.PageSize = anp.PageSize;
138                 dlt.DataSource = pds;
139                 dlt.DataBind();
140 
141             }
142 
143 
144         }
145 
146 
147         #endregion 
148 
149 
150 
151 
152 
153     }
154 }

 

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。