.net常用控件绑定代码分享

来源:互联网 发布:qq软件 编辑:程序博客网 时间:2024/04/29 19:12
using System;
using System.Web.UI.WebControls;
using System.Data;

namespace ControlsBind
{
    
/// <summary>
    
/// ControlsBind 的摘要说明。
    
/// </summary>
    public class ControlsBind
    {
        
public ControlsBind()
        {
            
//
            
// TODO: 在此处添加构造函数逻辑
            
//
        }

        
#region BindDropDownList
        
/// <summary>
        
/// 绑定下拉列表框
        
/// </summary>
        
/// <param name="strValueField">Value值</param>
        
/// <param name="strTextField">显示值</param>
        
/// <param name="dt">被绑定的DataTable</param>
        
/// <param name="ddlst_List">下拉列表框</param>
        
/// <param name="bFlag">是否绑定在绑定中插入"——请选择——"</param>
        public  static void BindDropDownList(string strValueField,string strTextField,DataTable dt,DropDownList ddlst_List, bool bFlag)
        {
            
try
            {
                ddlst_List.Items.Clear();
                ddlst_List.DataSource 
= dt;
                ddlst_List.DataValueField 
= strValueField;
                ddlst_List.DataTextField 
= strTextField;
                ddlst_List.DataBind();
                dt.Dispose();
                
if (bFlag)
                {
                    ListItem NewItem 
= new ListItem("——请选择——","-1");
                    ddlst_List.Items.Insert(
0,NewItem);
                }
            }
            
catch(Exception e)
            {
                
throw(new Exception("绑定下拉列表框失败!"+ CONST.SPLITSTRING +e.Message));
            }
        }

        
/// <summary>
        
/// 绑定下拉列表框(默认在绑定中插入"——请选择——")
        
/// </summary>
        
/// <param name="strValueField">Value值</param>
        
/// <param name="strTextField">显示值</param>
        
/// <param name="dt">被绑定的DataTable</param>
        
/// <param name="ddlst_List">下拉列表框</param>
        public static void BindDropDownList(string strValueField,string strTextField,DataTable dt,DropDownList ddlst_List)
        {
             BindDropDownList(strValueField,strTextField, dt, ddlst_List, 
true);
        }

        
/// <summary>
        
/// 绑定下拉列表框
        
/// </summary>
        
/// <param name="strValueField">Value值</param>
        
/// <param name="strTextField">显示值</param>
        
/// <param name="dt">被绑定的DataTable</param>
        
/// <param name="ddlst_List">下拉列表框</param>
        
/// <param name="bFlag">是否绑定在绑定中插入"——请选择——"</param>
        public  static void BindDropDownList(string strValueField,string strTextField, string  strSQL, DropDownList ddlst_List, bool bFlag)
        {
            DataTable dt 
=  Tools.Data.GetDataTable(strSQL);
            BindDropDownList(strValueField,strTextField, dt, ddlst_List, bFlag);
        }

     
/// <summary>
        
/// 绑定下拉列表框(默认在绑定中插入"——请选择——")
        
/// </summary>
        
/// <param name="strValueField">Value值</param>
        
/// <param name="strTextField">显示值</param>
        
/// <param name="dt">被绑定的DataTable</param>
        
/// <param name="ddlst_List">下拉列表框</param>
        public static void BindDropDownList(string strValueField,string strTextField, string strSQL,DropDownList ddlst_List)
        {
            BindDropDownList(strValueField,strTextField, strSQL, ddlst_List,
true);
        }

       
#endregion

        
#region IntialDropDownList
        
/// <summary>
        
/// 根据当前Text值初始化DropDownList控件
        
/// </summary>
        
/// <param name="ddlstObj">DropDownList控件对象</param>
        
/// <param name="strText">文本</param>
        public static void InitDropDownList(DropDownList ddlstObj,string strText)
        {
            
for(int i=0; i<ddlstObj.Items.Count; i++)
            {
                
if(ddlstObj.Items[i].Text == strText)
                {
                    ddlstObj.SelectedIndex 
= i;
                    
break;
                }
            }
        }

        
/// <summary>
        
/// 根据当前Value值初始化DropDownList控件
        
/// </summary>
        
/// <param name="ddlstObj">DropDownList控件对象</param>
        
/// <param name="strValue">文本</param>
        public static void InitDropDownList(string strValue, DropDownList ddlstObj)
        {
            
for(int i=0; i<ddlstObj.Items.Count; i++)
            {
                
if(ddlstObj.Items[i].Value == strValue)
                {
                    ddlstObj.SelectedIndex 
= i;
                    
break;
                }
            }
        }  
        
#endregion
        
        
#region 
        
/// <summary>
        
/// 根据当前Value值初始化下拉列表框
        
/// </summary>
        
/// <param name="rbtLst"></param>
        
/// <param name="strText"></param>
        public static void InitRadioButtonList(RadioButtonList rbtLst, string strText)
        {
            
for (int i=0; i< rbtLst.Items.Count; i++)
            {
                
if (rbtLst.Items[i].Text  == strText)
                {
                    rbtLst.SelectedIndex 
= i;
                    
break;
                }
            }
        }

        
/// <summary>
        
/// 根据当前Text值初始化下拉列表框
        
/// </summary>
        
/// <param name="strValue"></param>
        
/// <param name="rbtLst"></param>
         public static void InitRadioButtonList(string strValue, RadioButtonList rbtLst)
        {
            
for (int i=0; i< rbtLst.Items.Count; i++)
            {
                
if (rbtLst.Items[i].Value  == strValue)
                {
                    rbtLst.SelectedIndex 
= i;
                    
break;
                }
            }
        }

        
#endregion
      
        
#region BindDataGrid
        
/// <summary>
        
/// 获取DataGrid有效的当前页Index
        
/// </summary>
        
/// <param name="iCurrentPageIndex"></param>
        
/// <param name="iDSCount"></param>
        
/// <param name="iPageSize"></param>
        
/// <returns></returns>
        public static int GetCurrentPage(int iCurrentPageIndex,int iDSCount,int iPageSize)
        {
            
if (iPageSize == 0)
            {
                iCurrentPageIndex 
= 0;
            }
             
            
if((iDSCount <= iCurrentPageIndex*iPageSize)&&(iDSCount!=0))
                iCurrentPageIndex 
= iCurrentPageIndex -1;
            
return iCurrentPageIndex;
        }

        
/// <summary>
        
/// 绑定数据窗口
        
/// </summary>
        
/// <param name="dg_Bind">被绑定的数据窗口</param>
        
/// <param name="dt">被绑定的DataTable</param>
        
/// <param name="PageIndex">绑定的当前页</param>
        public static void BindDataGrid(DataGrid dg_Bind, DataTable dt, int iPageIndex)
        {
            dg_Bind.CurrentPageIndex 
= GetCurrentPage(iPageIndex, dt.Rows.Count, dg_Bind.PageSize);
            dg_Bind.PagerStyle.Mode         
= System.Web.UI.WebControls.PagerMode.NextPrev;
            dg_Bind.PagerStyle.NextPageText 
= "下一页";
            dg_Bind.PagerStyle.PrevPageText 
= "上一页";
            dg_Bind.DataSource       
= dt;
            dg_Bind.DataBind();
              dt.Dispose();
        }

        
/// <summary>
        
/// 绑定数据窗口
        
/// </summary>
        
/// <param name="dg_Bind">被绑定的数据窗口</param>
        
/// <param name="dt">被绑定的DataTable</param>
         public static void BindDataGrid(DataGrid dg_Bind, DataTable dt)
        {
            BindDataGrid(dg_Bind, dt,
0);
        }

        
#endregion

        
#region ClearControls
        
public static void ClearAllControls(System.Web.UI.Control[] ControlArr)
        {
            
for (int i=0; i< ControlArr.Length; i++)
            {
                
string strType = ControlArr[i].GetType().ToString();
                
switch (strType)
                {
                    
case "System.Web.UI.WebControls.TextBox":
                        ((System.Web.UI.WebControls.TextBox)ControlArr[i]).Text 
= "";
                        
break;
                    
case "System.Web.UI.WebControls.Literal":
                        ((System.Web.UI.WebControls.Literal)ControlArr[i]).Text 
= "";
                        
break;
                    
case "System.Web.UI.WebControls.Label":
                        ((System.Web.UI.WebControls.Label)ControlArr[i]).Text 
= "";
                        
break;
                    
case "System.Web.UI.WebControls.DropDownList":
                        Tools.Bind.InitDropDownList(
"-1",((System.Web.UI.WebControls.DropDownList)ControlArr[i]));
                        
break;
                    
case "System.Web.UI.WebControls.CheckBox":
                        ((System.Web.UI.WebControls.CheckBox)ControlArr[i]).Checked 
= false;
                        
break;
                    
case "System.Web.UI.WebControls.ListBox":
                        ((System.Web.UI.WebControls.ListBox)ControlArr[i]).Items.Clear();
                        
break;
                    
                }
            }
        }
        
#endregion
    }
}