comment remplir DropDownList à partir de la firebase database dans asp.net?
et quand je prends la valeur de la DropDownList comment attraper cet événement?
Conn.Open(); SQL = "SELECT distinct city FROM MEN"; dsView = new DataSet(); adp = new SqlDataAdapter(SQL, Conn); adp.Fill(dsView, "MEN"); adp.Dispose(); DropDownList1. ?????? (what do to ?)
Merci d’avance
Vous définissez DataSource
, DataTextField
et DataValueField
et appelez DataBind()
afin de remplir la liste déroulante.
La source de données peut être à peu près n’importe quel IEnumerable
et le texte et la valeur seront recherchés avec reflection.
L’événement que vous souhaitez intercepter est l’événement SelectedIndexChanged
. Il se déclenchera lorsque vous modifierez la sélection.
Prenez d’abord les détails dans un jeu de données, puis le code suivant vous aidera:
DropDownList1.DataSource = ds DropDownList1.DataTextField = "emailid" DropDownList1.DataValueField = "userid" DropDownList1.DataBind() DropDownList1.Items.Insert(0, New ListItem("select", "-1"))
Exemple de code simple:
DropDownList.DataSource = yourDataSource; DropDownList.DataTextField = "displayNameColumnName "; DropDownList.DataValueField = "TheValueColumnName"; DropDownList.DataBind();
Cela pourrait être une procédure complète pour vous dans ce cas:
protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { BindDropDownLists(); } } protected void Page_Init(object sender, EventArgs e) { SqlDataSource sqlDS = new SqlDataSource(); sqlDS.ConnectionSsortingng = ConfigurationManager.ConnectionSsortingngs[0].ToSsortingng(); sqlDS.SelectCommand = "select GenderID,Gender from mylookupGender"; form1.Controls.Add(sqlDS); DropDownList ddl = new DropDownList(); ddl.ID = "dddlGender"; ddl.DataSource = sqlDS; ddl.DataTextField = "Gender"; ddl.DataValueField = "GenderID"; form1.Controls.Add(ddl); // ... Repeat above code 9 times or put in a for loop if they're all the same... } private void BindDropDownLists() { foreach (Control ctl in form1.Controls) { if (ctl is DropDownList) { (ctl as DropDownList).DataBind(); } } }
//...Wrote separate class for calling this function FunctionClass obj = new FunctionClass(); List details = new List (); bool result1 = obj.DataDrop(out details); if (result1 == true) { dropDownDesignation.DataSource = details; dropDownDesignation.DataTextField = "designation"; dropDownDesignation.DataValueField = "Designation_ID"; dropDownDesignation.DataBind(); dropDownDesignation.Items.Insert(0, new ListItem("--Select--", "0")); } //..This function wrote inside FunctionClass and called from aspx.cs page public bool DataDrop(out List designationDetails) { designationDetails = new List (); conn = new SqlConnection(connectionName); conn.Open(); command.Connection = conn; command.CommandType = CommandType.StoredProcedure; command.CommandText = "DesignationDetails"; userReader = command.ExecuteReader(); if(userReader.HasRows) { while(userReader.Read()) { designationDetails.Add(new Designation() { designationId=userReader.GetInt32(0), designation=userReader.GetSsortingng(1) }); } } return true; } //..This should declare outside the class but inside the namespace public class Designation { public int designationId { get; set; } public ssortingng designation { get; set; } }
Un autre moyen de lier dropdownlist est …