DropDownList绑定及修改

一、DropDownList:
1、选项值保存到数据库:
    Hashtable ht= new  Hashtable(); //这里用Hashtable
    ht.Add( "字段名" ,DropDownListID.SelectedItem.Text.ToString()); //保存选项Text
    ht.Add( "字段名" ,DropDownListID.SelectedItem.Value.ToString()); //保存选项Value
  Hashtable ht=new Hashtable();//这里用Hashtable 
                ht.Add("0", "选择学科");
                ddlSubName.DataSource = ht;
                ddlSubName.DataTextField = "value";
                ddlSubName.DataValueField = "key";
                ddlSubName.DataBind();

2、选项值由数据库绑定到DropDownList:
    首先DropDownListID.ClearSelection(); //清除选项
     DropDownListID.Items.FindByText(dr[ "字段名" ].ToString()).Selected = true ; //选项Text
     DropDownListID.Items.FindByValue(dr[ "字段名" ].ToString()).Selected = true ; //选项Value
 
二、RadioButtonList:
1、选项值保存到数据库(同DropDownList):
    Hashtable ht= new  Hashtable(); //这里用Hashtable
    ht.Add( "字段名" ,RadioButtonListID.SelectedItem.Text.ToString()); //保存选项Text
    ht.Add( "字段名" ,RadioButtonListID.SelectedItem.Value.ToString()); //保存选项Value
 
2、选项值由数据库绑定到RadioButtonList
    string  SelectItem = dr[ "字段名" ].ToString(); //将数据库中的选项值从DataRow中读出赋给变量SelectItem
    for  ( int  i = 0; i < RadioButtonListID.Items.Count; i++)
    { //用for循环判断那项被选种
        if  (RadioButtonListID.Items[i].Text == SelectItem)RadioButtonListID.Items[i].Selected = true ;
    }
 
三、CheckBoxList:
1、选项值保存到数据库
    string  SelectItem = "" ; //声明一个变量来接受选项
    for  ( int  i = 0; i < CheckBoxListID.Items.Count; i++)
    { //用for循环将所有选项用","隔开连接起来
         if  (CheckBoxListID.Items[i].Selected)
         {
             SelectItem = SelectItem + CheckBoxListID.Items[i].Value + "," ; //选项后加","隔开
         }
    }
    ht.Add( "字段名" ,SelectItem.ToString());
 
2、选项值由数据库绑定到CheckBoxList
    string  SelectItem = dr[ "字段名" ].ToString();
    string [] arrStr = SelectItem.Split( ',' ); //字段是以","隔开
    foreach  ( string  str in  arrStr)
    {
        for  ( int  i = 0; i <CheckBoxListID.Items.Count; i++)
        {
           if  ( this .CheckBoxListID.Items[i].Value == str)
           {
              this .CheckBoxListID.Items[i].Selected = true ;
           }
        }
    }
=================================================
1.把数据绑定到CheckBoxList中
 
   protected  void  Page_Load( object  sender, EventArgs e)
         {
             if  (!Page.IsPostBack)
             {
                 SqlConnection con = GetDBCon.GetCon();
                 con.Open();
                 SqlDataAdapter sda = new  SqlDataAdapter( "select * from admin" , con);
                 DataSet ds = new  DataSet();
                 sda.Fill(ds, "admin" );
                 this .CheckBoxList1.DataSource = ds.Tables[0];
                 this .CheckBoxList1.DataTextField = "username" ; //绑定的字段名
                 this .CheckBoxList1.DataValueField = "userid" ; //绑定的值
                 this .CheckBoxList1.DataBind();
                
              
                 
             }
         }
 
2.循环读取出来
 
  protected  void  CheckBoxList1_SelectedIndexChanged( object  sender, EventArgs e)
         {
             this .Lab2.Text = "" ;
             for  ( int  i = 0; i < CheckBoxList1.Items.Count; i++)
             {
                 if  ( this .CheckBoxList1.Items[i].Selected)
                 {
                     this .Lab2.Text = this .Lab2.Text+CheckBoxList1.Items[i].Text+ "." ;
                 }
             }
         }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值