ASP. net mvc DropDownList Data Binding and Usage Details

Source: Internet
Author: User

I. DropDownList
1.1 bind DropDownList to data
1.1.1 fixed DropDownList binding
This method is suitable for binding fixed data to the DropDownList.
Example

Copy codeThe Code is 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"> female </asp: Listitem>
</Asp: DropDownList>

1.1.2 dynamic binding of DropDownList
Front-end:
Backend: two methods: (Note that the original record must be cleared every time you bind it, for example, ddlArea. Items. Clear ();)
First:Copy codeThe Code is 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 the default item, which must be put after data binding:

Second:Copy codeThe Code is 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 (new ListItem (dt. rows [I] ["display value"]. toString (), dt. rows [I] ["usbkey"]. toString ()));
}
DropDownList1.Items. Insert (0, "Select Internet cafe ");
DropDownList1.Items [0]. Value = "0"; or
// DropDownList1.Items. Insert (0, new ListItem ("Select data", "random binding"); // Insert the default item, which must be placed in Data Binding
}
Else
{
DropDownList1.Items. Insert (0, "no internet cafe record ");
DropDownList1.Items [0]. Value = "0 ";
}

Ii. Value of DropDownList1:
2.1 retrieve the index value of DropDownList1, that is, select the value <asp: Listitem value = "1"> male </asp: Listitem> to get 1
DropDownList1.SelectedValue. ToString () in. net ()
Export cirpt var ddl1 = document. getElementByIdx_x ("DropDownList1"). selectedIndex;
2.2 select the DropDownList1 option, that is, select the item value <asp: Listitem value = "1"> male </asp: Listitem> to take male
DropDownList1.SelectedItem. ToString ();
Javascript document. getElementByIdx_x ("DropDownList1"). options [document. getElement ("selectID"). selectedIndex]. value
Iii. DropDownList1 event:
Important: When OnTextChanged and OnSelectedIndexChanged are used, you must setCopy codeThe Code is as follows: <asp: DropDownList runat = "server" OnTextChanged = "DropDownList1_TextChanged" OnSelectedIndexChanged = "DropDownList1_SelectedIndexChanged1">

OnTextChanged and OnSelectedIndexChanged have some differences between the two events. I did not test the differences. I only know that OnSelectedIndexChanged is earlier than OnTextChanged, that is, if both events exist, the OnSelectedIndexChanged event is first executed before OnTextChanged is executed.
4. How to avoid repeated values in the DropDownList drop-down list?
AppendDataBoundItems: whether to add a duplicate value. This parameter is added. If this parameter is left blank
Cause: the AppendDataBoundItems attribute of the DropDownList control is set to "True". Change it to False.
For example, if the AppendDataBoundItems attribute of the DropDownList control after the major is set to "True", the value of the Major will be continuously added after the Department is selected.
V. Differences Copy codeThe Code is as follows: depart_ddl.Items.Insert (0, new ListItem ("This item is not selected", "0"); this is to add data in the first item.
Items. Add is added at the end
DropDownList1.Items. Add (new ListItem ("Text", "value"); Add
DropDownList1.Items. Insert (Index, new ListItem ("Text", "value"); this is the first item to add data.

6. read data from the database and bind it to the DropDownList. Copy codeThe Code is as follows: if (ds. Tables [0]. Rows [0] ["State"]. ToString () = "True ")
{
DropDownListState. Items. FindByValue ("1"). Selected = true;
}
Else
{
DropDownListState. Items. FindByValue ("0"). Selected = true;
}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.