熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> .NET編程 >> 正文

DropdownList綁定的兩種方法

2022-06-13   來源: .NET編程 
    DropdownList綁定的兩種方法
    動態綁定方法一動態綁定數據庫中的字段
    SqlConnection conn = UtilitySqlClassOperateDataBaseReturnConn();
    string strSQL = select * from CompanyType;
    SqlDataAdapter ada = new SqlDataAdapter(strSQL conn);
    DataSet ds = new DataSet();
    adaFill(ds CompanyType);
    DropDownListDataSource = dsTables[CompanyType]DefaultView;
    DropDownListDataValueField = dsTables[CompanyType]Columns[]ColumnName;
    DropDownListDataTextField = dsTables[CompanyType]Columns[]ColumnName;
    DropDownListDataBind();
    dsDispose();
    動態綁定方法二利用DropDownListItemsAdd方法
    PRotected void Page_Load(object sender EventArgs e)
    {
    if (!IsPostBack)
    {
    SqlConnection conn = UtilitySqlClassOperateDataBaseReturnConn();
    try
    {
    connOpen();
    thisDropDownListItemsAdd();
    string strSQL = select CompanyType from CompanyType;
    SqlCommand com = new SqlCommand(strSQL conn);
    SqlDataReader dr = comExecuteReader();
    while (drRead())
    {
    thisDropDownListItemsAdd(dr[CompanyType]ToString());
    }
    }
    catch (Exception ex)
    {
    ResponseWrite(<scirpt>alert( + exMessageToString() + )</script>);
    }
    finally
    {
    connClose();
    }
    }
    }


   第一種方法
    string ConnString = ConfigurationSettingsAppSettings[ConnectionString];
    //創建一個SqlConnection
    SqlConnection Conn = new SqlConnection( ConnString );
    string SQL_Select = select id ItemName from DDLItem order by id desc;
    //構造一個SqlDataAdapter
    SqlDataAdapter myAdapter = new SqlDataAdapter( SQL_Select Conn);
    //開始讀取數據
    ConnOpen();
    DataSet dataSet = new DataSet();
    myAdapterFill( dataSetTable );
    ConnClose();
    //開始綁定DropDownList
    //指定DropDownList使用的數據源
    DropDownListDataSource = dataSetTables[Table]DefaultView;
    //指定DropDownList使用的表裡的那些字段
    DropDownListDataTextField = ItemName; //dropdownlist的Text的字段
    DropDownListDataValueField = id;//dropdownlist的Value的字段
    DropDownListDataBind();
    第二種方法
    conOpen();
    SqlCommand cmd = new SqlCommand(strSqlcon);
    SqlDataReader dr = cmdExecuteReader();
    while (drRead())
    {
    DropDownListItemsAdd(new ListItem(dr[status]ToString() dr[status_Id]ToString()));
    }


From:http://tw.wingwit.com/Article/program/net/201311/13161.html
  • 上一篇文章:

  • 下一篇文章:
  • 推薦文章
    Copyright © 2005-2022 電腦知識網 Computer Knowledge   All rights reserved.