1、直接赋值
foreach (DataRow dr in ds.Tables[0].Rows) //遍历获取两个值 { Ext.Net.ListItem listItem = new Ext.Net.ListItem(); //每次创建一个Ext.Net.ListItem的对象 listItem.Value = dr["ID"].ToString(); listItem.Text = dr["Name"].ToString(); drpComBox.Items.Add(listItem); }
2、设置选中项,注意这种方法不能用于Store方式
public void SetListValue(ComboBox p_rg, string p_value) { int num = 0; foreach (ListItem listItem in (StateManagedCollection<ListItem>)p_rg.Items) { if (listItem.Value == p_value) { p_rg.SelectedIndex = num; ExtNet.AddScript(string.Format("{0}.setValue(\"{1}\");", (object)p_rg.ClientID.ToString(), (object)p_value)); break; } ++num; } }
3、使用Store方式
this.drpComBox.DisplayField = "Name"; this.drpComBox.ValueField = "ID"; this.drpComBOx.StoreID = "storeStudent";
protected void storeStudent_Refresh(object sender, StoreRefreshDataEventArgs e) { .. storeStudent.DataSource = ds.Tables[0]; storeStudent.DataBind(); }
设置当前值
this.drpComBox.SetValue(Id); this.drpComBox.Text = v.Name
使用这种方式赋值有问题,vaule一直等于text,通过如下函数可以
public void SetListValue(ComboBox p_combox, string p_text, object p_value) { p_combox.Text = p_text; ExtNet.AddScript(string.Format("{0}.setValue(\"{1}\");", (object) p_combox.ClientID.ToString(), (object) p_value.ToString())); if (p_text.Length > 0) ExtNet.AddScript(string.Format("document.getElementById(\"{0}\").value=\"{1}\";", (object) p_combox.ClientID.ToString(), (object) p_text)); ExtNet.AddScript(string.Format("document.getElementsByName(\"{0}_Value\")[0].value=\"{1}\";", (object) p_combox.ClientID.ToString(), (object) p_value.ToString())); }