One: DropDownList
1.1 DropDownList binding Data
1.1.1 DropDownList Fixed Binding
This approach is suitable for those already fixed data bound to DropDownList.
Cases
Copy Code code as follows:
<asp:dropdownlist runat= "Server" id= "Ddlarea" width= "120px" >
<asp:listitem value= "0" > select Gender </asp:Listitem>
<asp:listitem value= "1" > Male </asp:Listitem>
<asp:listitem value= "2" > Women </asp:Listitem>
</asp:DropDownList>
1.1.2 DropDownList Dynamic Binding
Front desk:
Background: Two methods: (note, each binding must clear the original record, example: DdlArea.Items.Clear ();)
First type:
Copy Code code as follows:
SqlConnection conn = new SqlConnection ("server=.; Uid=sa;database=pubs ");
SqlDataAdapter dap = new SqlDataAdapter ("Select * from Jobs", conn);
DataTable dt = new DataTable ();
Dap. Fill (DT);
DropDownList1.Items.Clear ();
Dropdownlist1.datasource = DT;
Dropdownlist1.datatextfield = "Job_desc";
Dropdownlist1.datavaluefield = "job_id";
Dropdownlist1.databind ();
DropDownList1.Items.Insert (0, New ListItem ("Select data", "random Binding");//Insert default entry, this must be after data binding effect:
The second type:
Copy Code code as follows:
SqlConnection conn = new SqlConnection ("server=.; Uid=sa;database=pubs ");
SqlDataAdapter dap = new SqlDataAdapter ("Select * from Jobs", conn);
DataTable dt = new DataTable ();
Dap. Fill (DT);
if (dt. Rows.Count!= 0)
{
DropDownList1.Items.Clear ();
for (int i = 0; i < dt. Rows.Count; i++)
{
DROPDOWNLIST1.ITEMS.ADD (DT, new ListItem. rows[i]["Display value"]. ToString (), dt. rows[i]["Usbkey"]. ToString ()));
}
DropDownList1.Items.Insert (0, "Choice of internet cafes");
Dropdownlist1.items[0]. Value = "0"; Or
DropDownList1.Items.Insert (0, New ListItem ("Select data", "random Binding");//Insert default entry, this must be placed in the data binding
}
Else
{
DropDownList1.Items.Insert (0, "No Internet Café record");
Dropdownlist1.items[0]. Value = "0";
}
Second: The value problem of DropDownList1:
2.1 Take DropDownList1 index value, that is, select value <asp:listitem value= "1" > Male </asp:Listitem> Take 1
. NET DropDownList1.SelectedValue.ToString ()
javascirpt var ddl1=document.getelementbyidx_x ("DropDownList1"). SelectedIndex;
2.2 Take DropDownList1 option, that is, select item Value <asp:listitem value= "1" > Male </asp:Listitem> Pick Male
. NET in DropDownList1.SelectedItem.ToString ();
JavaScript document.getelementbyidx_x ("DropDownList1"). Options[document.getelement ("Selectid"). SelectedIndex]. Value
III: DROPDOWNLIST1 Event Issues:
Important: When using the Ontextchanged,onselectedindexchanged event, you must set the
Copy Code code as follows:
<asp:dropdownlist runat= "Server" ontextchanged= "dropdownlist1_textchanged" onselectedindexchanged= " Dropdownlist1_selectedindexchanged1 ">
Ontextchanged,onselectedindexchanged the difference between these two events, I did not test out, only know onselectedindexchanged this event than OnTextChanged executed earlier, That is, if both events exist, the OnSelectedIndexChanged event is executed first and then the ontextchanged is executed.
four: How to avoid repeating the values in the DropDownList dropdown box?
AppendDataBoundItems whether duplicate values are added. Really for add, fake for not fill
Reason: The DropDownList control AppendDataBoundItems property is set to "True", and is changed to false.
For example, if the professional DropDownList control AppendDataBoundItems property is set to "True", then the value of the selected faculties will be added continuously.
Five: the difference
Copy Code code as follows:
Depart_ddl. Items.insert (0,new ListItem ("Not selected", "0")); This is the first item to add data.
Items.Add is added at the end
DROPDOWNLIST1.ITEMS.ADD (New ListItem ("Text", "value")); is added at the end
DropDownList1.Items.Insert (index,new ListItem ("Text", "value"); This is the first item to add data.
VI: Read data from the database and bind to DropDownList
Copy Code code as follows:
if (ds. Tables[0]. rows[0]["State"]. ToString () = = "True")
{
DropDownListState.Items.FindByValue ("1"). Selected =true;
}
Else
{
DropDownListState.Items.FindByValue ("0"). Selected =true;
}