protected void Page_PreRender(object sender, EventArgs e) { // insert into the drop down the user listItem MyDropDown.Items.Clear(); foreach (ListItem listItem in ListItems) { MyDropDown.Items.Add(listItem); } if (!hasDataSource) { if (tableName != "") { using (MySqlConnection conn = new MySqlConnection(ConnStr)) { string sql = String.Format("Select {0},{1} From {2} {3} ", this.DataTextField, DataValueField, this.tableName, this.dataOrderField == "" ? "" : "Order By " + this.dataOrderField); MySqlDataAdapter da = new MySqlDataAdapter(sql, conn); DataSet ds = new DataSet(); da.Fill(ds, "dropDown"); MyDropDown.DataSource = ds.Tables["dropDown"].DefaultView; MyDropDown.DataBind(); } } } else { MyDropDown.DataBind(); } TextBoxHolderDiv.Attributes["class"] = this.cssClass; string mySelected = DataFieldValue; string a = SelectedValue; if (!String.IsNullOrEmpty(mySelected) && MyDropDown.Items.FindByValue(mySelected) != null) { MyDropDown.SelectedItem.Selected = false; for (int i = 0; i < MyDropDown.Items.Count; i++) { MyDropDown.Items[i].Selected = false; if (MyDropDown.Items[i].Value == mySelected) { MyDropDown.Items[i].Selected = true; myspan.InnerText = MyDropDown.Items[i].Text; MyHiddenField.Value = MyDropDown.Items[i].Text; MyHiddenFieldVlaue.Value = MyDropDown.Items[i].Value; } } } else { for (int i = 0; i < MyDropDown.Items.Count; i++) { if (MyDropDown.Items[i].Selected) { myspan.InnerText = MyDropDown.Items[i].Text; MyHiddenField.Value = MyDropDown.Items[i].Text; MyHiddenFieldVlaue.Value = MyDropDown.Items[i].Value; } } } MyDropDown.Attributes.Add("onchange", "javascript:document.getElementById('" + myspan.ClientID + "').innerHTML=this.options[this.selectedIndex].text;document.getElementById('" + MyHiddenField.ClientID + "').value=this.options[this.selectedIndex].text;document.getElementById('" + MyHiddenFieldVlaue.ClientID + "').value=this.options[this.selectedIndex].value;" + onClientSelectedChanged); }
public void DataBind() { MyDropDown.DataBind(); }