可以绑定Datagrid,DataList,DropDownList,RadioList,,,等控件。很想把DataGrid的很多操作都封装了,免得每次copy代码那么累
using System;
using System.Data ;
using System.Data.SqlClient ;
using System.Web.SessionState;

namespace LiJiangRiverWeb
{
 /// <summary>
 /// BindListControl 绑定ListControl类控件。
 /// </summary>
 public class BindListControl
 {
  private  SqlConnection SqlConn=new SqlConnection("Data Source=localhost;User Id=sa;Password=sa;Initial Catalog=DB_LiJiangRiver;");
  /// <summary>
  /// 链接字符串
  /// </summary>
  private string m_tableName;
  /// <param name="MyCheckBox"></param>
  /// <param name="MyCheckBoxName"></param>
  private System.Web.UI.WebControls.ListControl m_ListControl;
  /// <summary>
  /// 绑定ListControl名称
  /// </summary>
  /// <param name="myListControl"></param>
  /// <param name="TableName"></param>\
  private string m_sqlQuery;
  /// <summary>
  /// 查询语句
  /// </summary>
  /// <param name="myListControl"></param>
  /// <param name="TableName"></param>
  private System.Web.UI.WebControls.BaseDataList m_DataList;
  /// <summary>
  /// DataGrid
  /// </summary>
  /// <param name="myListControl"></param>
  /// <param name="TableName"></param>
  /// <param name="sqlQuery"></param>
  public BindListControl(System.Web.UI.WebControls.ListControl myListControl,string TableName,string sqlQuery)
  {
   this.m_ListControl = myListControl;
   this.m_tableName = TableName;
   this.m_sqlQuery =sqlQuery;
   Bind_ListControl();

  }
  public BindListControl(System.Web.UI.WebControls.ListControl myListControl,string TableName)
  {
   this.m_ListControl = myListControl;
   this.m_tableName = TableName;
   Bind_ListControl();
  }
  //public BindListControl()
  public BindListControl(System.Web.UI.WebControls.BaseDataList  myDataList,string sqlQuery,string TableName)
  {
   this.m_DataList = myDataList;
   this.m_sqlQuery = sqlQuery;
   this.m_tableName=TableName;
   Bind_DataList();
  }

  private void Bind_ListControl()//绑定ListControl函数
  {
   //
   // TODO: 在此处添加构造函数逻辑
   //
   // SqlConnection SqlConn=new SqlConnection("Data Source=localhost;User Id=sa;Password=sa;Initial Catalog=DB_LiJiangRiver;");
   DataSet ds = new DataSet();

  
   if (m_sqlQuery == null)
    m_sqlQuery = "Select * from "+ m_tableName;
   SqlConn.Open();
   SqlDataAdapter sqldpt = new SqlDataAdapter (m_sqlQuery,SqlConn);
   sqldpt.Fill (ds,m_tableName);
   m_ListControl.DataSource = ds.Tables [m_tableName].DefaultView ;
   m_ListControl.DataValueField =ds.Tables[m_tableName].Columns[0].Caption ;
   m_ListControl.DataTextField =ds.Tables [m_tableName].Columns[1].Caption ;
   m_ListControl.DataBind();
   SqlConn.Close ();

  }
  private void Bind_DataList()//绑定dataList函数
  {
   //SqlConnection SqlConn=new SqlConnection("Data Source=localhost;User Id=sa;Password=sa;Initial Catalog=DB_LiJiangRiver;");
   DataSet ds = new DataSet();
   SqlConn.Open();
   SqlDataAdapter sqldpt = new SqlDataAdapter (m_sqlQuery,SqlConn);
   sqldpt.Fill (ds,m_tableName);
            m_DataList.DataSource = ds.Tables [m_tableName].DefaultView ;
   m_DataList.DataBind();
   SqlConn.Close();
  }
 }
}

posted on 2004-06-03 10:30  奚彧  阅读(598)  评论(0编辑  收藏  举报