用于绑定asp.net控件的类

本文介绍了一种用于将各种控件(如下拉框、列表框、DataGrid等)绑定到数据源的方法。该方法提供了多种绑定方式,包括直接绑定、通过SQL查询绑定以及特殊需求下的绑定操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

using System; using System.Data; using System.Configuration; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.HtmlControls; using System.Drawing; using Microsoft.Web.UI.WebControls; using System.Text; using System.IO;

namespace OA {  /// <summary>  /// BoundCb 的摘要说明  /// 绑定数据源到下拉框  /// </summary>  public class BoundControl  {

  Dataprocess MyDataprocess=new Dataprocess();   public BoundControl()   {    //    // TODO: 在此处添加构造函数逻辑    //

  }

  #region 自下拉框列   public void AddDropDownListItem(DropDownList Dr,string ValueString)   {    Dr.Items.Clear();    ListItem CItem=new ListItem(ValueString,"0");    Dr.Items.Add(CItem);

  }   #endregion

  #region 绑定到下拉框   /// <summary>   /// Cb 下拉控件   /// Table 绑定的数据源表   /// ValuetField Value字段   /// TextField   显示字段   /// All         定位为空   /// </summary>   /// <param name="Cb"></param>   /// <param name="Table"></param>   /// <param name="ValuetField"></param>   /// <param name="TextField"></param>   /// <param name="All"> </param>       public void DoBoundDropDown(DropDownList Cb, DataTable Table, string ValuetField, string TextField,bool All)   {    //    // TODO: 在此处添加构造函数逻辑    //    if(All || Table.Rows.Count==0)    {     DataRow Row=Table.NewRow();     Row[0]=0;     Row[1]="";     Table.Rows.InsertAt(Row,0);    }    Cb.DataSource = Table;    Cb.DataValueField = ValuetField;    Cb.DataTextField = TextField;          Cb.DataBind();      }   public void DoBoundDropDown(DropDownList Cb, string SelectStr, string ValuetField, string TextField,bool All)   {    //    // TODO: 在此处添加构造函数逻辑       //

   DataTable Table=MyDataprocess.GetDataTable(SelectStr);    if(All|| Table.Rows.Count==0)    {     DataRow Row=Table.NewRow();     Row[0]=0;     Row[1]="";     Table.Rows.InsertAt(Row,0);    }    Cb.DataSource = Table;    Cb.DataValueField = ValuetField;    Cb.DataTextField = TextField;          Cb.DataBind();      }

  public void DoBoundDropDown(DropDownList Cb, DataTable Table, string ValuetField, string TextField,bool All,int defaultID)   {    //    // TODO: 在此处添加构造函数逻辑    //    if(All|| Table.Rows.Count==0)    {     DataRow Row=Table.NewRow();     Row[0]=0;     Row[1]="";     Table.Rows.InsertAt(Row,0);    }    Cb.DataSource = Table;    Cb.DataValueField = ValuetField;    Cb.DataTextField = TextField;          Cb.DataBind();      }      public void DoBoundDropDown(DropDownList Cb, string SelectStr,bool All)   {        DataTable Table;    Table=MyDataprocess.GetDataTable(SelectStr);    if(All|| Table.Rows.Count==0)    {         DataRow Row=Table.NewRow();         Row[0]=0;     Row[1]="";     Table.Rows.InsertAt(Row,0);    }    Cb.DataSource = Table;    Cb.DataValueField = Table.Columns[0].ColumnName;    Cb.DataTextField =Table.Columns[1].ColumnName;          Cb.DataBind();      }

  public void DoBoundDropDown(DropDownList Cb,DataTable Table,bool All)   {         if(All|| Table.Rows.Count==0)    {         DataRow Row=Table.NewRow();         Row[0]=0;     Row[1]="";     Table.Rows.InsertAt(Row,0);         }    Cb.DataSource = Table;    Cb.DataValueField = Table.Columns[0].ColumnName;    Cb.DataTextField =Table.Columns[1].ColumnName;          Cb.DataBind();      }   #endregion  

  #region 绑定到列表框   /// <summary>   /// List ListBox控件   /// Table 绑定的数据源表   /// ValuetField Value字段   /// TextField   显示字段   /// </summary>   public void DoBoundListBox(ListBox List, DataTable Table,string ValueField,string TextField)   {    //    // TODO: 在此处添加构造函数逻辑    //    List.DataSource = Table;    List.DataValueField = ValueField;    List.DataTextField = TextField;    List.DataBind();   }   #endregion

  #region 绑定到DataGrid   /// <summary>   ///   /// </summary>   /// <param name="Dgrid">DataGrid控件</param>   /// <param name="Table">要指定的数据源</param>   /// <param name="FieldName">字段列表</param>   /// <param name="FieldDesc">字段描述</param>   public void DoBoundDataGrid(DataGrid Dg,DataTable Table)   {       Dg.AutoGenerateColumns=false;    Dg.DataSource=Table;    Dg.DataBind();    if(Dg.PageCount-1<Dg.CurrentPageIndex)     Dg.CurrentPageIndex=0;    if ((Dg.SelectedIndex<0||Dg.SelectedIndex>Dg.Items.Count-1)&&Dg.Items.Count>0)     Dg.SelectedIndex=0;       else if(Dg.Items.Count==0)     Dg.SelectedIndex=-1;           }

  public void DoBoundDataGridAuto(DataGrid Dg,DataTable Table,bool Total)   {       if(Total)    {     DataRow NewRow=Table.NewRow();     NewRow[0]="合计:";     Table.Rows.Add(NewRow);    }    Dg.AutoGenerateColumns=true;    Dg.DataSource=Table;    Dg.DataBind();    if(Dg.PageCount-1<Dg.CurrentPageIndex)     Dg.CurrentPageIndex=0;    if ((Dg.SelectedIndex<0||Dg.SelectedIndex>Dg.Items.Count-1)&&Dg.Items.Count>0)     Dg.SelectedIndex=0;       else if(Dg.Items.Count==0)     Dg.SelectedIndex=-1;        }   

   public void DoBoundDataGridAuto(DataGrid Dg,DataTable Table,bool Total,bool AmountStr)    {        if(Total)     {      DataRow NewRow=Table.NewRow();      if(AmountStr)       NewRow[0]="合计:";      Table.Rows.Add(NewRow);     }     Dg.AutoGenerateColumns=true;     Dg.DataSource=Table;     Dg.DataBind();     if(Dg.PageCount-1<Dg.CurrentPageIndex)      Dg.CurrentPageIndex=0;     if ((Dg.SelectedIndex<0||Dg.SelectedIndex>Dg.Items.Count-1)&&Dg.Items.Count>0)      Dg.SelectedIndex=0;        else if(Dg.Items.Count==0)      Dg.SelectedIndex=-1;            }

    public void DoBoundDataGrid(DataGrid Dg,DataTable Table,bool Empty)   {          Dg.AutoGenerateColumns=false;    Dg.DataSource=Table;    Dg.DataBind();    if(Dg.PageCount-1<Dg.CurrentPageIndex)     Dg.CurrentPageIndex=0;    if ((Dg.SelectedIndex<0||Dg.SelectedIndex>Dg.Items.Count-1)&&Dg.Items.Count>0)     Dg.SelectedIndex=0;       else if(Dg.Items.Count==0)     Dg.SelectedIndex=-1;           if(Empty)         {         if(Table.Rows.Count==0)         {           DataRow Row=Table.NewRow();      Table.Rows.Add(Row);      Dg.DataSource=Table;      Dg.DataBind();      for(int i=0;i<Dg.Columns.Count;i++)      {       if(Dg.Columns[i].HeaderText.Trim().Length>0 &&Dg.Columns[i].Visible)       {        Dg.Items[0].Cells[i].Text="查找到0条记录";        break;

      }      }           }       }

          }

  public void DoBoundDataGrid(DataGrid Dg,DataView Dv)   {       Dg.AutoGenerateColumns=false;    Dg.DataSource=Dv;    Dg.DataBind();    if(Dg.PageCount-1<Dg.CurrentPageIndex)     Dg.CurrentPageIndex=0;    if ((Dg.SelectedIndex<0||Dg.SelectedIndex>Dg.Items.Count-1)&&Dg.Items.Count>0)     Dg.SelectedIndex=0;       else if(Dg.Items.Count==0)     Dg.SelectedIndex=-1;

          }

  public void DoBoundDataGrid(DataGrid Dg,string SelectStr)    {            Dg.AutoGenerateColumns=false;    Dg.DataSource=MyDataprocess.GetDataTable(SelectStr);       Dg.DataBind();    if(Dg.PageCount-1<=Dg.CurrentPageIndex)     Dg.CurrentPageIndex=0;    if ((Dg.SelectedIndex<0||Dg.SelectedIndex>Dg.Items.Count-1)&&Dg.Items.Count>0)     Dg.SelectedIndex=0;    else if(Dg.Items.Count==0)     Dg.SelectedIndex=-1;  

  }

  public void DoBoundDataGrid(DataGrid Dg,PagedDataSource Paged)    {           Dg.AutoGenerateColumns=false;    Dg.DataSource=Paged.DataSource;    Dg.DataBind();    if(Dg.PageCount-1<Dg.CurrentPageIndex)     Dg.CurrentPageIndex=0;    if ((Dg.SelectedIndex<0||Dg.SelectedIndex>Dg.Items.Count-1)&&Dg.Items.Count>0)     Dg.SelectedIndex=0;    else if(Dg.Items.Count==0)     Dg.SelectedIndex=-1;  

  }

 

  #endregion

  #region 初始化DataGrid   public void InitialDataGrid(DataGrid Dg)   {    DataTable Table=new DataTable();       Dg.DataSource=Table;    Dg.DataBind();

  }   #endregion

  #region 绑定到DataList   /// <summary>   ///   /// </summary>   /// <param name="DList">DataList控件</param>   /// <param name="Table">要指定要绑定的数据表</param>      public void DoBoundDataList(DataList DList,DataTable Table)   {           DList.DataSource=Table;    DList.DataBind();  

  }

  public void DoBoundDataList(DataList DList,string SelectStr)   {              DList.DataSource=MyDataprocess.GetDataTable(SelectStr);    DList.DataBind();  

  }

  public void DoBoundDataList(DataList DList,DataView Dv )   {                  DList.DataSource=Dv;    DList.DataBind();  

  }   public void DoBoundDataList(DataList DList,PagedDataSource Pds )   {            DList.DataSource=Pds;    DList.DataBind(); 

  }

  public void DoBoundDataList(DataList DList,DataTable Table,ref PagedDataSource Pds )   {     Pds.DataSource=Table.DefaultView;       DList.DataSource=Pds;    DList.DataBind(); 

  }

  public void DoBoundDataList(DataList DList,string SelectStr, ref PagedDataSource Pds )   {      Pds.DataSource=MyDataprocess.GetDataTable(SelectStr).DefaultView;       DList.DataSource=Pds;    DList.DataBind();  

  }   public void DoBoundDataList(DataList DList,string SelectStr, ref PagedDataSource Pds,string FilterStr )   {          DataView Dv=MyDataprocess.GetDataTable(SelectStr).DefaultView;    Dv.RowFilter=FilterStr;     Pds.DataSource=Dv;     DList.DataSource=Pds;    DList.DataBind();  

  }

  #endregion

  #region 绑定到TreeView   /// <summary>   ///   /// </summary>   /// <param name="DList">DataList控件</param>   /// <param name="Table">要指定要绑定的数据表</param>      public void DoBoundTreeView(TreeView TV,DataTable Table)   {                 

  }

  public void DoBoundTreeView(TreeView TV,string SelectStr)   {               

  }

  #endregion

  #region 获取选择TreeView节点   public TreeNode SelectNode(TreeView Tv)   {    string[] NodeID=Tv.SelectedNodeIndex.Split('.');    TreeNode Node=null;    foreach(string Indexstring in NodeID)    {     if(Node==null)     {      Node=Tv.Nodes[Convert.ToInt32(Indexstring)];     }     else     {      Node=Node.Nodes[Convert.ToInt32(Indexstring)];     }

   }         return Node;

  }   #endregion

  #region 获取节点层次   public int NodeLevel(TreeNode Node)   {    string[] NodeIndex=Node.GetNodeIndex().Split('.');    return NodeIndex.Length;   }   #endregion

  #region 更改选定的颜色   public void SelectGridColor(DataGrid Dg)   {        foreach(DataGridItem Item in Dg.Items)    {     Item.BackColor=Color.Empty;    }    if(Dg.SelectedItem!=null)     Dg.SelectedItem.BackColor=Color.LightGray;

  }

  public void SelectGridColor(DataList Dl)   {        foreach(DataListItem Item in Dl.Items)    {     Item.BackColor=Color.Empty;    }    if(Dl.SelectedItem!=null)     Dl.SelectedItem.BackColor=Color.LightGray;

  }   #endregion

  #region 转到DataGrid页   public void GotoPage(DataGrid Dg,int PageIndex)   {    Dg.CurrentPageIndex=PageIndex;

  }   #endregion

  #region 返回DataGrid页参数   public string[] PageItems(DataGrid Dg)      {    string[] Items=new string[4];    Items[0]=Dg.Items.Count.ToString();    Items[1]=Dg.PageSize.ToString();    Items[2]=Convert.ToString(Dg.CurrentPageIndex+1);    Items[3]=Dg.PageCount.ToString();        return Items;       }   #endregion

  #region 返回DataGrid的DataItem   public DataRow GetGridItemRow(DataGrid Dg)   {    if(Dg.SelectedIndex!=-1 &&Dg.DataSource!=null)     return ((DataTable)Dg.DataSource).Rows[Dg.SelectedItem.DataSetIndex];        else     return null;       }   #endregion

  #region 新增DataGrid列   public void AddDataGrid(DataGrid Dg,DataRow Row)   {    DataTable Table=(DataTable)Dg.DataSource;    Table.Rows.Add(Row);    Dg.DataSource=Table;    Dg.DataBind();       }   #endregion

  #region 防止页面滚动

  public void RetainScrollPosition(Page page)   {

      StringBuilder saveScrollPosition  =new StringBuilder();    StringBuilder setScrollPosition  = new StringBuilder();      page.RegisterHiddenField("__SCROLLPOS", "0");

   saveScrollPosition.Append("<script language='javascript'>") ;    saveScrollPosition.Append("function saveScrollPosition() {") ;    saveScrollPosition.Append(" document.forms[0].__SCROLLPOS.value = mybody.scrollTop;") ;    saveScrollPosition.Append("}") ;    saveScrollPosition.Append("mybody.οnscrοll=saveScrollPosition;") ;    saveScrollPosition.Append("</script>") ;

   page.RegisterStartupScript("saveScroll", saveScrollPosition.ToString()) ;

   if (page.IsPostBack)    {     setScrollPosition.Append("<script language='javascript'>") ;     setScrollPosition.Append("function setScrollPosition() {") ;     setScrollPosition.Append(" mybody.scrollTop = " + page.Request["__SCROLLPOS"] +";") ;     setScrollPosition.Append("}") ;     setScrollPosition.Append("mybody.οnlοad=setScrollPosition;") ;     setScrollPosition.Append("</script>");

    page.RegisterStartupScript("setScroll", setScrollPosition.ToString()) ;    }       }   #endregion

  #region 控制条件显示   public void ListControl(DropDownList Dr,System.Web.UI.Control Cn,bool Empty)   {    if(Empty)    {     if(Dr.Items.Count<=2)      Cn.Visible=false;     else      Cn.Visible=true;    }    else    {     if(Dr.Items.Count<=1)      Cn.Visible=false;     else      Cn.Visible=true;    }

  }

  #endregion

  #region Word文件转换成二进制流   public Byte[] WordFileInput(string FileName)   {    //File.Copy(FileName,"Temp.doc",true);       //FileStream uPost=new FileStream("Temp.doc",FileMode.Open,FileAccess.Read);    FileStream uPost=new FileStream(FileName,FileMode.Open,FileAccess.Read);    int uPostLength =int.Parse(uPost.Length.ToString());    byte[] PostArray = new Byte[uPostLength];       uPost.Read(PostArray, 0, uPostLength);    uPost.Close();    File.Delete("Temp.doc");    return PostArray;   }

  #endregion

  #region 上传文件转换成二进制流   public Byte[] FileInputByte(HtmlInputFile MyFile)   {    byte[] PostArray;    int uPostLength =MyFile.PostedFile.ContentLength;    PostArray= new Byte[uPostLength];       MyFile.PostedFile.InputStream.Read(PostArray, 0, uPostLength);        return PostArray;   }

  #endregion

 

 } }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值