The use of DropDownList

Source: Internet
Author: User

DropDownList is a drop-down menu, which we usually use often, and see how to bind values

1> static Add, that is, when the values are very clear

ListItem List1 = new ListItem ("A", "1");

ListItem list2 = new ListItem ("B", "2");

ListItem list3 = new ListItem ("C", "3");

ListItem list4 = new ListItem ("D", "4");





If you want to specifically place the specific index for each, we can use the Insert method to add

This.DropDownList2.Items.insert (int index,listitem Item)

You can also add directly in the background HTML:

<asp:dropdownlist id= "DropDownList2" runat= "Server" >

<asp:listitem value= "1" >a</asp:ListItem>

<asp:listitem value= "2" >b</asp:ListItem>

<asp:listitem value= "3" >c</asp:ListItem>

<asp:listitem value= "4" >d</asp:ListItem>


To HTML is

<select id= "Select" >

<option value= "1" >a</option>

<option value= "2" >a</option>

<option value= "3" >a</option>

<option value= "4" >a</option>


2> Dynamic Binding

@ View Bindings

<asp:dropdownlist id= "DropDownList1" runat= "Server"

Datatextfield= "Name" datavaluefield= "Id" >


<asp:objectdatasource id= "ObjectDataSource2" runat= "Server"

oldvaluesparameterformatstring= "original_{0}" selectmethod= "Getalluser"

Typename= "MyBookShop.BLL.UserManager" ></asp:ObjectDataSource>

The only downside to this binding is that "-Please select-" is not displayed in the first list.

@ Code Binding

This kind of binding method is very flexible, but also can solve the above problem well, let's see How to bind

This. Dropdownlist1.datasource = Usermanager.getalluser ();

This. Dropdownlist1.databind ();

There are two ways to do this:

1 ListItem list = new ListItem ("--Please select--", "0");

This. Dropdownlist1.datasource = Usermanager.getalluser ();

This. DropDownList1.Items.Insert (0, list);

This. Dropdownlist1.datatextfield = "name";

This. Dropdownlist1.datavaluefield = "id";

This. Dropdownlist1.databind ();

But when I bind to a dataset, it does not add ("--Please Choose--"), which is to be written down below, as

protected void Linkbind ()
DataSet ds = linkmanager.getlist ("jh_checked= ' is '");
Drourl.datasource = ds;
Drourl.datatextfield = "Jh_linkname";
Drourl.datavaluefield = "Jh_linkurl";
Drourl.databind ();
ListItem item = new ListItem ("--Please select---", "#");
DroUrl.Items.Insert (0,item);

2 ilist<departinfo> departlist = Departinfomanager.getdepartlist ();

Departlist.insert (0, new Departinfo {departid = 0, Departname = "--Please Select--"});

Dddepart.datasource = departlist;

Dddepart.datatextfield = "Departname";

Dddepart.datavaluefield = "Departid";

Dddepart.databind ();

Usually we will use in the link, select one time the link will change, and open a new page, this is very good implementation

protected void Dropdownlist1_textchanged (object sender, EventArgs e)
String link = this. Dropdownlist1.selectedvalue;
Response.Write ("<script> ('" +link+ "', ' _blank ') </script>");

You can do it with a flexible grasp.

The use of DropDownList

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: 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.