drop down list

P

puja

hi all,

I have a requirement where I need to populate drop down list (which is not
run at server control. i.e it is an html control ) using server side code in
C#?

How can I add option values to drop down list in page load event.

Thanks
puja
 
G

Guest

Hi Puja,

There are different ways. Here are a couple to give you ideas for what is
good for you:

1. Drop the HTML Select control onto the page.
2. Right-click and select Run As Server Control. The HTML for the control
will look like this (notice the runat="server" attribute):

<select id="Select1" runat="server">
<option selected="selected"></option>
</select>

3a. You could add code to your Page_Load that directly assigns to the Items
collection like this:

Select1.Items.Clear();
Select1.Items.Add(new ListItem("One", "Eins"));
Select1.Items.Add(new ListItem("Two", "Zwie"));
Select1.Items.Add(new ListItem("Three", "Drei"));

3b. Alternatively, you can databind like this:

string[] alphas = { "Alpha", "Beta", "Charlie" };
Select1.Items.Clear();

Select1.DataSource = alphas;
Select1.DataBind();

4. If you want one of the items to be selected, you can set it like this:

Select1.SelectedIndex = 1;

Joe
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top