473,516 Members | 3,465 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

DropdownList: How to select by value programaticly?

I have an ASP DropdownList populated. What I want is to
select a new item, in my code, by set a datavale to
dropdownlist's selectedItem.Value property. I did the
following:

MyDropdown.SelectedItem.Value = "value 1"

Even though the "value 1" is in the dropdownlist's data
value collection, the dropdown's datatext doesn't change.

What am I doing wrong here? How should I do this?

Please help!

Nov 17 '05 #1
3 43393
You should say

MyDropdown.SelectedIndex = <Index of the Item you want to select.>

Rajeev

"Northern" <li******@msn.com> wrote in message
news:05****************************@phx.gbl...
I have an ASP DropdownList populated. What I want is to
select a new item, in my code, by set a datavale to
dropdownlist's selectedItem.Value property. I did the
following:

MyDropdown.SelectedItem.Value = "value 1"

Even though the "value 1" is in the dropdownlist's data
value collection, the dropdown's datatext doesn't change.

What am I doing wrong here? How should I do this?

Please help!

Nov 17 '05 #2
Thank you so much Ken. That's exactly what I need.
You are the MVP!

Thank a million!
-----Original Message-----
Hi,

If I understand what you're after, you need to determine the index of the valueyou are seeking and then set the SelectedIndex property with that. The examplebelow might be what you need. Let us know?

Ken
MVP [ASP.NET]

Private Sub Button1_Click _
(ByVal sender As System.Object, _
ByVal e As System.EventArgs) _
Handles Button1.Click
DropDownList1.SelectedIndex = _
DropDownList1.Items.IndexOf _
(DropDownList1.Items.FindByValue("Green"))
End Sub

'--------------------------------------------------------- ------ <form id="Form1" method="post" runat="server">
<p>
<asp:dropdownlist id="DropDownList1" runat="server"> <asp:listitem value="Red">Red</asp:listitem>
<asp:listitem value="Blue">Blue</asp:listitem>
<asp:listitem value="Green">Green</asp:listitem>
</asp:dropdownlist></p>
<p>
<asp:button id="Button1" runat="server" text="SelectGreen"></asp:button></p>
</form>
"Northern" <li******@msn.com> wrote in message
news:05****************************@phx.gbl...
I have an ASP DropdownList populated. What I want is to
select a new item, in my code, by set a datavale to
dropdownlist's selectedItem.Value property. I did the
following:

MyDropdown.SelectedItem.Value = "value 1"

Even though the "value 1" is in the dropdownlist's data
value collection, the dropdown's datatext doesn't change.

What am I doing wrong here? How should I do this?

Please help!
.

Nov 17 '05 #3
Glad to hear that it worked for you!

Ken

"Northern" <li******@msn.com> wrote in message
news:00****************************@phx.gbl...
Thank you so much Ken. That's exactly what I need.
You are the MVP!

Thank a million!
Nov 17 '05 #4

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

Similar topics

1
4409
by: krian | last post by:
Hi, How can I change the styles of the DropDownList(Select box)..? and also I want to place an image in the place of DropDownButton of Select Box .. How can i done these in DHTML..?. I would like to know an example on Customizing DorpDownButton of the DropDownList or Select Box(Changing DropDownButton border,placing image as background to...
1
4913
by: Lars Pedersen | last post by:
Hi! Have can i select an item in a DropDown List, based on a datavalue. thanks -Lars
1
8556
by: Micheal | last post by:
I have an ASP.NET website using C# that has a Web Control DropDownList. The DropDownList is filled by a database call to SQL Server. The problem is that when I click to change what is selected from the DropDownList, it only select the 'first' value in the DropDownList no matter what I select. Any idea? Thanks in advance
2
9372
by: Aymer | last post by:
Scenario: i have a drop down list control in my form. i am binding it with a dataset. that dataset contains the value 1, 2, 3, ..., 10. so when the dropdownlist control is loaded, it contain the values 1, 2, 3, ..., 10. Problem: How do i create a default value for the dropdownlist? After the dropdownlist control is loaded and binded,...
2
4708
by: HH | last post by:
Hi, I have a dropdown list that is datafilled via a SQL table. The text part is always unique. (A list of countries) Each country is assigned one of three numbers. (This being the 'value' of the ddl.) ie: Country Value Australia 1
4
15973
by: Chris Kettenbach | last post by:
Good morning all, I am sure this has been asked but I did not see anything. I have a datalist control. In the edititemtemplate I have a dropdownlist. I know on the itemdatabound event is where I can set the dropdownlist selectedindex. How do I set the correct value? private void lstDegrees_ItemDataBound(object sender,...
6
8244
by: Dabbler | last post by:
I have a dropdownlist in a GridView ItemTemplate. I need to bind the ddl to an SqlDataSource, then have a value from a boundfield in the row be passed as the keyfield for select where clause. Im trying to load the ddl with a list of dates from another table keyed on GridView row field that only apply to this row. Any suggestions would be...
2
1423
by: Trih | last post by:
Hello, I'm writing a .Net 2.0 web application in C# and I have a strange problem with something as simple as a dropdownlist. As a test I have added a single dropdownlist to an empty web page and in the code behind I have the following code: protected void Page_Load(object sender, EventArgs e) { ListItemCollection lic = new...
1
10438
by: arunbojan | last post by:
Hi All, I have an editable datagrid with 10 columns, one of those is ddl column which I used for showing the status.... By default all the columns are readonly, when user clicks edit, status ll be enabled and im getting the values in the grid....... say, following are the values in status
0
7273
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...
0
7182
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
1
7136
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7547
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...
0
5712
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
3265
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...
0
3252
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
823
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
487
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...

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.