user1503463
user1503463

Reputation:

How to display a listitem in a dropdown list using ASP.NET?

This is my list definition

public class EventsList
    {
        public int EventID { get; set; }
        public string EventName { get; set; }
    }

This is C# code

string strCurrentUser = CommonWeb.GetLoginUser();
       EventsClass EventObj = new EventsClass();
       DataSet ds;
       List< EventsList> eventList = new List<EventsList>();
       EventsList eventobj = new EventsList();
       ds=EventObj.GetEvents(strCurrentUser);

I have a dropdown in which it should have the dropdown menu as the EventName alone. How can I retrieve only the EventName from the list and display that in the dropdown?

Upvotes: 1

Views: 4733

Answers (4)

Mahmoud Gamal
Mahmoud Gamal

Reputation: 79929

To display the EventName only to the dropdown, you will need to set the following two properties:

Something like:

dropdown.DataSource = ds;
dropdown.DataValueField  = "EventID";
dropdown.DataTextField = "EventName";
dropdown.DataBind();

Upvotes: 4

Dany
Dany

Reputation: 2174

You can use like also if you have tables in datasource

DropDownlist2.DataSource = dataSet.Tables["Table2"];
Dropdownlist2.DataTextField= "SomeTextFieldInTable2";
Dropdownlist2.DataValueField="SomeValueFieldInTable2";
DropDownlist2.DataBind();

Upvotes: 0

Adil
Adil

Reputation: 3268

In asp.net, you can achieve this like

DropDownList1.DataSource = ds;
DropDownList1.DataTextField = "EventName";
DropDownList1.DataValueField = "EventID";
DropDownList1.DataBind();

Upvotes: 0

Sravan Kumar
Sravan Kumar

Reputation: 1457

ddlName.DataSource = ds;
ddlName.DataTextField= "EventName";
ddlName.DataValueField= "EventID";
ddlName.DataBind();

Here DataValueField is upto you whether to use ID or the Name itself.

Upvotes: 0

Related Questions