DropDownList绑定及修改
2010-12-29 14:48
246 查看
一、DropDownList: 1、选项值保存到数据库: Hashtable ht=new Hashtable();//这里用Hashtable ht.Add("字段名",DropDownListID.SelectedItem.Text.ToString());//保存选项Text ht.Add("字段名",DropDownListID.SelectedItem.Value.ToString());//保存选项Value 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+"."; } } }
相关文章推荐
- TreeView树型菜单添加,修改,删除,绑定DropDownList功能
- 【转】4种方法把数据绑定到Dropdownlist
- 牛腩购物18 : 添加商品页面2 。泛型List<T>的使用,linq 的使用, 如何在 DropDownList 里面绑定二级分类(运用linq),fileupload和图片控件的配合使用用来上传图片,try catch
- DropDownList绑定数据表实现两级联动
- DataSet绑定Hashtable为DropDownList提供数据源
- .net连接数据源以及dropdownlist的数据绑定
- [.net]DataGrid中绑定DropDownList[转]
- Tomcat发布网站(修改端口、修改默认发布目录、多域名绑定)
- 将数组绑定到dropdownlist上
- asp.net读取Xml文件并进行DropDownList数据绑定
- dropdownlist选项绑定
- 在ASPxGridView绑定的时候修改模板列的值
- 解决获取DropDownList绑定的记录值,获得的总是第一个记录?
- 如何解决FormView中实现DropDownList连动选择时出现 "Eval()、XPath() 和 Bind() 这类数据绑定方法只能在数据绑定控件的上下文中使用" 的错误
- Dropdownlist绑定
- DropdownList绑定的两种方法
- 动态修改DataTable中某列的值并绑定到DataGridView
- 如何用HashTable数据源绑定DataList、DropDownList等相似控件
- DropDownList 绑定数据库中的字段获取下拉列表值
- html的<a>标签IE下绑定点击事件后也会修改href问题