473,498 Members | 1,379 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

dropdown list control basic question

Hi,

I have a drop down that I have populated. I am use to using winforms with
the items.add function to add objects to the drop downs, but on web forms it
seems you can only add text string.

How do i then force the drop down to be a specific item in the drop down
(such as setting a default value)
If its possible to add more than just a text string, is it possible to have
an index. For example I want to have a list of countriues in the drop down.
When the user selected a country, say 'France', I am able to access the
index of that item, which may be the 'FR'

Thanks all
Jun 13 '07 #1
4 1158
On Jun 13, 10:07 am, "Aussie Rules" <AussieRu...@nospam.nospamwrote:
Hi,

I have a drop down that I have populated. I am use to using winforms with
the items.add function to add objects to the drop downs, but on web forms it
seems you can only add text string.

How do i then force the drop down to be a specific item in the drop down
(such as setting a default value)

If its possible to add more than just a text string, is it possible to have
an index. For example I want to have a list of countriues in the drop down.
When the user selected a country, say 'France', I am able to access the
index of that item, which may be the 'FR'

Thanks all
Hello!

You can add ListItems to a drop down on webforms
these objects can be created with a display string, and a key string

so if you wanted to add countries, it could be something like:

ddlCountries.Items.Add( new ListItem("Frances", "FR") );

and when wanting to know the value of which item is selected in your
drop down you can just access the SelectedValue property of the
control

string selected = ddlCountries.SelectedValue;

also, in case you wanted to select a specific item via code behind,
you can with the SelectedIndex property

ddlCountries.SelectedIndex =
ddlCountries.Items.IndexOf( ddlCountries.Items.FindByValue( "FR" ) );
hope this is what you were looking for
good luck

Jun 13 '07 #2
With web forms you can have a text and a value associated with a list item
in the DropDownList

<asp:DropDownList ID="DropDownList1" runat="server">
<asp:ListItem Text="France" Value="FR" />
<asp:ListItem Text="Finland" Value="FI" Selected="True" />
</asp:DropDownList>

Adding in code could be:

DropDownList1.Items.Add(New ListItem("France", "FR"))
DropDownList1.Items.Add(New ListItem("Finland", "FI"))

'Setting Finland to be a default selection
DropDownList1.Items.FindByValue("FI").Selected = True

'Or optionally to ensure only one item is always selected
DropDownList1.SelectedIndex =
DropDownList1.Items.IndexOf(DropDownList1.Items.Fi ndByValue("FI"))

Then accessing what's selected can be done via SelectedIndex, SelectedItem
or SelectedValueproperties or via Items collection

--
Teemu Keiski
AspInsider, ASP.NET MVP
http://blogs.aspadvice.com/joteke
http://teemukeiski.net

"Aussie Rules" <Au*********@nospam.nospamwrote in message
news:ux*************@TK2MSFTNGP06.phx.gbl...
Hi,

I have a drop down that I have populated. I am use to using winforms with
the items.add function to add objects to the drop downs, but on web forms
it seems you can only add text string.

How do i then force the drop down to be a specific item in the drop down
(such as setting a default value)
If its possible to add more than just a text string, is it possible to
have an index. For example I want to have a list of countriues in the drop
down. When the user selected a country, say 'France', I am able to access
the index of that item, which may be the 'FR'

Thanks all


Jun 13 '07 #3
Hello! Good Site! Thanks you! wyhcswlselwc
Jun 21 '07 #4
Hello! Good Site! Thanks you! mqkqjouukffaj
Aug 16 '07 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
1365
by: Manish | last post by:
Hi All, I'm having this problem I hope someone can help provide a solution for it :) I've this dropdown list box in a usercontrol which I'm populating from the database (it's viewstate property...
3
1486
by: Developerforum | last post by:
Hi, My customer is looking for a web control that allow them to perform speedy data entry. They wanted a dropdown list box which they can also key in the code directly (like a text box). I...
0
1299
by: john | last post by:
I have a dropdown with a SelectedIndexChanged event handler. I want the event to only get called if the dropdown list was the control that caused the postback. But if the value of the list is...
0
1249
by: Kay | last post by:
Hello, I have developed a web custom control, I want one of the properties of the control to appear as a dropdown list so the user can select from the list. My question is: how do I define an...
0
1223
by: Kay | last post by:
Hello, I have developed a web custom control, I want one of the properties of the control to appear as a dropdown list so the user can select from the list. My question is: how do I define an...
5
11862
by: jung_h_park | last post by:
From: jung_h_park@yahoo.com Newsgroups: microsoft.public.dotnet.framework.aspnet Subject: Dropdown List not retaining its SelectedValue Date: Mon, 26 Jun 2006 21:02:57 -0700 Hello, My...
0
2893
by: cindy | last post by:
I have a dynamic datagrid. I have custom classes for the controls public class CreateEditItemTemplateDDL : ITemplate { DataTable dtBind; string strddlName; string strSelectedID; string...
0
1335
by: Kay | last post by:
Hello, I have written my own custom control and I want one of its properties to display as a dropdown list when clicked, so the user can select from the list, it would be similar to the asp...
0
1598
by: Kay O'Keeffe | last post by:
Hello, I have written my own custom control and I want one of its properties to display as a dropdown list when clicked, so the user can select from the list, it would be similar to the asp...
3
1703
by: =?Utf-8?B?UGF1bA==?= | last post by:
Hi I have a generic list of data list <typelistcategory; listcategory = new list <type>; I also have a dropdown box that I have put into a template column of a gridview. I was able to populate...
0
7124
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
6998
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7163
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7200
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
4904
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4586
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3078
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1416
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
651
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.