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

时间:2021-03-24 13:50:59
 using System;
using System.Collections.Generic;
using System.Text;
using System.Web.UI.WebControls;//
using System.Data.SqlClient;
using System.Data;//
using System.Web.UI;//
using Wuqi.Webdiyer;//此控件请在网上下载 namespace Study
{
public sealed class WebControlsBind
{
#region DropDownlist 绑定
/// <summary>
/// 绑定DropDownList控件
/// </summary>
/// <param name="p_ddlControl">控件</param>
/// <param name="p_drSource">数据源</param>
/// <param name="p_strText">文本值</param>
/// <param name="p_strValue">值</param>
/// <param name="p_blAll">是否包含全部项,若不包含返回false,若包含返回true</param>
public static void BindDDL(DataTable p_drSource, DropDownList p_ddlControl, string p_strText, string p_strValue, bool p_blAll)
{
p_ddlControl.DataSource = p_drSource;
p_ddlControl.DataTextField = p_strText;
p_ddlControl.DataValueField = p_strValue;
p_ddlControl.DataBind();
if (p_blAll == true)
{
ListItem li = new ListItem("全部", "");
p_ddlControl.Items.Insert(, li);//使用add方法只能添加到最后,使用Insert方法可以添加到指定的位置 }
} #endregion #region 带分页的数据绑定Repeater控件
/// <summary>
/// 带分页的Repeater的数据绑定
/// </summary>
/// <param name="dsSource">数据源</param>
/// <param name="rpt">Repeater控件</param>
/// <param name="anp">分页控件</param>
public static void Bindrpt(DataTable dsSource, Repeater rpt, AspNetPager anp)
{
DataView dv = dsSource.DefaultView;
anp.RecordCount = dv.Count;
if (dv.Count > )
{
PagedDataSource pds = new PagedDataSource();
pds.DataSource = dv;
pds.AllowPaging = true;
pds.CurrentPageIndex = anp.CurrentPageIndex - ;
pds.PageSize = anp.PageSize;
rpt.DataSource = pds;
rpt.DataBind(); } }
public static void Bindrpt(DataTable dsSource, Repeater rpt, AspNetPager anp,string order)
{
DataView dv = dsSource.DefaultView;
dv.Sort = order;
DataTable dt = dv.ToTable();
anp.RecordCount = dv.Count;
if (dv.Count > )
{
PagedDataSource pds = new PagedDataSource();
pds.DataSource = dv;
pds.AllowPaging = true;
pds.CurrentPageIndex = anp.CurrentPageIndex - ;
pds.PageSize = anp.PageSize;
rpt.DataSource = pds;
rpt.DataBind(); } } #endregion
#region 带分页的数据绑定Datalist控件
/// <summary>
/// 带分页的Repeater的数据绑定
/// </summary>
/// <param name="dsSource">数据源</param>
/// <param name="dlt">DataList控件</param>
/// <param name="anp">分页控件</param>
public static void BindDlt(DataTable dsSource, DataList dlt, AspNetPager anp)
{
DataView dv = dsSource.DefaultView;
anp.RecordCount = dv.Count;
if (dv.Count > )
{
PagedDataSource pds = new PagedDataSource();
pds.DataSource = dv;
pds.AllowPaging = true;
pds.CurrentPageIndex = anp.CurrentPageIndex - ;
pds.PageSize = anp.PageSize;
dlt.DataSource = pds;
dlt.DataBind(); } } #endregion }
}