ASP. NET MVC DropDownList data binding and usage details

  • 2020-05-19 04:29:21
  • OfStack

1: DropDownList
1.1 DropDownList binds data
1.1.1 DropDownList fixed binding
This approach is suitable for data binding to DropDownList that is already fixed.
case

 
<asp:DropDownList runat="server" ID="ddlArea" Width="120px" > 
<asp:Listitem value="0"> Sex selection </asp:Listitem> 
<asp:Listitem value="1"> male </asp:Listitem> 
<asp:Listitem value="2"> female </asp:Listitem> 
</asp:DropDownList> 

1.1.2 DropDownList dynamic binding
Front desk:
Background: two methods: (note that the original record is cleared 1 time for each binding, e.g. ddlArea.Items.Clear ();)
1 species:
 
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 the default entry, which must be placed after the data binding effect:  

The second:
 
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(new ListItem(dt.Rows[i][" According to the value "].ToString(), dt.Rows[i]["usbkey"].ToString())); 
} 
DropDownList1.Items.Insert(0, " Choose the Internet bar "); 
DropDownList1.Items[0].Value = "0";  or  
// DropDownList1.Items.Insert(0, new ListItem(" Select data ", " Random binding "));// Insert the default entry, which must be placed in the data binding  
} 
else 
{ 
DropDownList1.Items.Insert(0, " No Internet bar record "); 
DropDownList1.Items[0].Value = "0"; 
} 

2. Problem of the value of DropDownList1:
2.1 take the index value of DropDownList1, that is, select value value < asp:Listitem value="1" > male < /asp:Listitem > Take 1
. The net DropDownList1. SelectedValue. ToString ()
javascirpt var ddl1=document.getElementByIdx_x("DropDownList1").selectedIndex;
2.2 take the option of DropDownList1, that is, select the item value < asp:Listitem value="1" > male < /asp:Listitem > Take male
. The net DropDownList1. SelectedItem. ToString ();
javascript document.getElementByIdx_x("DropDownList1").options[document.getElement("selectID").selectedIndex].value
3: DropDownList1 event problem:
Important: when using OnTextChanged,OnSelectedIndexChanged events must be set
 
<asp:DropDownList runat="server" OnTextChanged="DropDownList1_TextChanged"OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged1"> 

I have not found out the specific difference between OnTextChanged and OnSelectedIndexChanged. I only know that OnSelectedIndexChanged will execute earlier than OnTextChanged. In other words, if both events exist, OnSelectedIndexChanged will be executed first and OnTextChanged will be executed later.
4: how do I avoid repeatedly adding values in the DropDownList drop-down box?
Does AppendDataBoundItems add duplicate values? True is added, false is not added
Reason: the DropDownList control AppendDataBoundItems property is set to "True", instead of False.
For example, if the DropDownList control AppendDataBoundItems property is set to "True", then the value in the major will be added after the department is selected.
5: the difference between
 
depart_ddl.Items.Insert(0,new ListItem(" Don't choose the ","0"));  This is adding data in the first entry.  
Items.Add It's added at the end  
DropDownList1.Items.Add(new ListItem("Text","value"));  It's added at the end  
DropDownList1.Items.Insert(Index,new ListItem("Text","value")); This is adding data in the first entry.  

6: read data from the database and bind to DropDownList
 
if (ds.Tables[0].Rows[0]["State"].ToString ()=="True") 
{ 
DropDownListState.Items.FindByValue("1").Selected =true; 
} 
else 
{ 
DropDownListState.Items.FindByValue("0").Selected =true; 
} 


Related articles: