您现在的位置是:首页 >

对外部参照进行绑定的方法是 DropdownList绑定的两种方法

火烧 2022-05-31 20:35:40 1034
Dro dow Li t绑定的两种方法 Dro dow Li t绑定的两种方法 动态绑定方法一 动态绑定数据库中的字段 SqlCo ectio co = UtilitySqlCla O erateDa

DropdownList绑定的两种方法  

对外部参照进行绑定的方法是 DropdownList绑定的两种方法
    DropdownList绑定的两种方法    动态绑定方法一 动态绑定数据库中的字段     SqlConnection conn = UtilitySqlClass OperateDataBase ReturnConn();    string strSQL = select * from CompanyType ;    SqlDataAdapter ada = new SqlDataAdapter(strSQL conn);    DataSet ds = new DataSet();    ada Fill(ds CompanyType );    DropDownList DataSource = ds Tables[ CompanyType ] DefaultView;    DropDownList DataValueField = ds Tables[ CompanyType ] Columns[ ] ColumnName;    DropDownList DataTextField = ds Tables[ CompanyType ] Columns[ ] ColumnName;    DropDownList DataBind();    ds Dispose();    动态绑定方法二 利用DropDownList Items Add方法     PRotected void Page_Load(object sender EventArgs e)    {    if (!IsPostBack)    {    SqlConnection conn = UtilitySqlClass OperateDataBase ReturnConn();    try    {    conn Open();    this DropDownList Items Add( );    string strSQL = select CompanyType from CompanyType ;    SqlCommand = new SqlCommand(strSQL conn);    SqlDataReader dr = ExecuteReader();    while (dr Read())    {    this DropDownList Items Add(dr[ CompanyType ] ToString());    }    }    catch (Exception ex)    {    Response Write( <scirpt>alert( + ex Message ToString() + )</script> );    }    finally    {    conn Close();    }    }    }

   第一种方法     string ConnString = ConfigurationSettings AppSettings[ 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);    //开始读取数据    Conn Open();    DataSet dataSet = new DataSet();    myAdapter Fill( dataSet Table );    Conn Close();    //开始绑定DropDownList    //指定DropDownList使用的数据源    DropDownList DataSource = dataSet Tables[ Table ] DefaultView;    //指定DropDownList使用的表里的那些字段    DropDownList DataTextField = ItemName ; //dropdownlist的Text的字段    DropDownList DataValueField = id ;//dropdownlist的Value的字段    DropDownList DataBind();    第二种方法     con Open();    SqlCommand cmd = new SqlCommand(strSql con);    SqlDataReader dr = cmd ExecuteReader();    while (dr Read())    {    DropDownList Items Add(new ListItem(dr[ status ] ToString() dr[ status_Id ] ToString()));    } lishixinzhi/Article/program/net/201311/13161  
永远跟党走
  • 如果你觉得本站很棒,可以通过扫码支付打赏哦!

    • 微信收款码
    • 支付宝收款码