473,765 Members | 2,159 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

dropdownlist default selected

How do i make a dropdownlist selected value based on the value i retrive from
the database.

Basically i have an edit page and like to display the default value in a
dropdown list from the database.

for example: if the ddl_value is 2 from the database i want the second list
item to be selected by default.
<asp:DropDownLi st id="ddl" runat="server">
<asp:ListItem Value="0">value 1</asp:ListItem>
<asp:ListItem Value="1">value 2</asp:ListItem> // make this dynamically
selected
<asp:ListItem Value="0">value 3</asp:ListItem>
<asp:ListItem Value="1">value 4</asp:ListItem>
</asp:DropDownLis t>

I am using the dropdownlist inside a repeater.. and only retriving single
record at a time based on a querystring... and using a datareader to fill the
form.

Many thanks in advance..
Nov 18 '05 #1
2 11312
Hi,

You need to find the item that has the text value "value 2" and then get its
index value. Once you have the index value, you can set the SelectedIndex to
that value.

Here's a quick way to accomplish all three:

Private Sub Page_Load _
(ByVal sender As System.Object, _
ByVal e As System.EventArg s) _
Handles MyBase.Load
If Not IsPostBack Then
ddl.SelectedInd ex = ddl.Items.Index Of _
(ddl.Items.Find ByText("value 2"))
End If
End Sub

<asp:DropDownLi st id="ddl" runat="server">
<asp:ListItem Value="0">value 1</asp:ListItem>
<asp:ListItem Value="1">value 2</asp:ListItem>
<asp:ListItem Value="0">value 3</asp:ListItem>
<asp:ListItem Value="1">value 4</asp:ListItem>
</asp:DropDownLis t>

Does this help?

Ken
Microsoft MVP [ASP.NET]

"huzz" <hu**@discussio ns.microsoft.co m> wrote in message
news:C6******** *************** ***********@mic rosoft.com...
How do i make a dropdownlist selected value based on the value i retrive
from
the database.

Basically i have an edit page and like to display the default value in a
dropdown list from the database.

for example: if the ddl_value is 2 from the database i want the second
list
item to be selected by default.
<asp:DropDownLi st id="ddl" runat="server">
<asp:ListItem Value="0">value 1</asp:ListItem>
<asp:ListItem Value="1">value 2</asp:ListItem> // make this dynamically
selected
<asp:ListItem Value="0">value 3</asp:ListItem>
<asp:ListItem Value="1">value 4</asp:ListItem>
</asp:DropDownLis t>

I am using the dropdownlist inside a repeater.. and only retriving single
record at a time based on a querystring... and using a datareader to fill
the
form.

Many thanks in advance..


Nov 18 '05 #2
You have to use either FindByValue() or FindByText() method of the items
collection. If found, it returns the ListItem otherwise, the method returns
null.

ListItem item = MyDropDown.item s.FindByValue(" TX");
if(item != null)
{
MyDropDown.Sele ctedItem.Select ed = false;
item.Selected = true;
}

Here is the reference link to the FindByValue and FindByText methods
http://msdn.microsoft.com/library/de...valuetopic.asp

HTH
-Chris
~
http://weblogs.austinspad.com/caustin

"huzz" <hu**@discussio ns.microsoft.co m> wrote in message
news:C6******** *************** ***********@mic rosoft.com...
How do i make a dropdownlist selected value based on the value i retrive from the database.

Basically i have an edit page and like to display the default value in a
dropdown list from the database.

for example: if the ddl_value is 2 from the database i want the second list item to be selected by default.
<asp:DropDownLi st id="ddl" runat="server">
<asp:ListItem Value="0">value 1</asp:ListItem>
<asp:ListItem Value="1">value 2</asp:ListItem> // make this dynamically
selected
<asp:ListItem Value="0">value 3</asp:ListItem>
<asp:ListItem Value="1">value 4</asp:ListItem>
</asp:DropDownLis t>

I am using the dropdownlist inside a repeater.. and only retriving single
record at a time based on a querystring... and using a datareader to fill the form.

Many thanks in advance..

Nov 18 '05 #3

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

Similar topics

12
2805
by: Stanley J Mroczek | last post by:
How do you load a dropdownlist when edit is clicked in a datagrid ? <Columns> <asp:BoundColumn DataField="OptionDescription" ItemStyle-Wrap="True" HeaderText="Option Description"></asp:BoundColumn> <asp:TemplateColumn runat="server" HeaderText="Id Type Option" "> <itemtemplate> <asp:label runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "TypeOption") %>' /> <asp:label runat="server" ID="LlbTypeOption" Visible=False...
3
2930
by: Olivier Verdin | last post by:
Hi, I have a page with several Textboxes and several DropDownList. When I click on a 'save' button, it creates a record in a database. The page works fine under Internet Explorer. It does not work under FireFox. The dropdownlist are always selecting the zero index value. If I debug and I stop the program in the page_load, the dropdownlist are already set to zero index.
1
1437
by: Frank | last post by:
Hello, I have some text boxes and 2 drop down lists (DDL) within a panel like so: Everything updates fine but the values from the DDLs are always the items selected by default. In other words, the items selected from the DDL do not show up....only the default selected items do. Any suggestions? <asp:panel id=Pnl_Address runat="server" width="390" Height="210px" horizontalalign="Center">
1
2955
by: RichardH | last post by:
Hi, I have a ListItemCollection that I bind to DropDownList: ListItemCollection items = new ListItemCollection(); ListItem item; item = new ListItem("Option 1", "1"); items.Add(item); item = new ListItem("Option 2", "2"); item.Selected = true; items.Add(item);
2
7917
by: Dabbler | last post by:
In my Registrant FormView I have a DropDownList which loads data from a secondary SqlDataSource "sdsOfficeParks". I need the user to select an office park but save the selected value in the FormView's ObjectDataSource "odsRegistrant" . The following solution can't work because I need the ddl selected value bound to my odsRegistrant but both tables have the same column name for primary key. <asp:DropDownList ID="ddlOfficePark"...
1
10399
by: Ben | last post by:
I have a formview with a few dropdownlists (software version, database version, etc). When a software version is selected, the database version dropdownlist updates itself accordingly. When in edit mode, everything works fine - the selected value in each list defaults to the value from the database record (selectedvalue=bind...). However, in insert mode (insertitem), it is not working. I pass in a value on the query string that I would...
1
5407
by: clickon | last post by:
Forget about the controlParameter for the moment, for testing purposes i have created the following Markup: <asp:Table ID="tblSelectRoute" runat="server" CssClass="asp-table"> <asp:TableRow> <asp:TableCell CssClass="asp-table-header">Select Route<asp:Label ID="lblTest" runat="server"></asp:Label> </asp:TableCell>
4
6140
by: rn5a | last post by:
I am binding a DropDownList with records existing in a database table. I want to add an extra item *SELECT COMPANY* at index 0 so that by default, it gets selected. This is how I tried it but the extra item just doesn't get added to the DropDownList: ============================================= <script runat="server"> Sub Page_Load(..........) Dim dSet As DataSet Dim sqlConn As SqlConnection
18
5433
by: Redhairs | last post by:
Is it possible to get DropDownList.SelectedValue in Page_PreInit() event during the postback?
0
9568
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10156
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10007
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9832
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7375
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5275
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3924
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 we have to send another system
2
3531
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2805
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.