Solved

Programmatically Select DropDownList Item

Posted on 2008-10-14
5
5,130 Views
Last Modified: 2013-12-17
Experts,

I have the following asp:dropdownlist: (see code snippet).
I am holding user addresses in my database.  The "State" column in the database is/will be updated using the same dropdownlist, so when a user selects 'AZ' from the dropdownlist a "2" will be sent to the database to be stored in the "State" column.

My problem is programmatically changing the selected item of the dropdownlist, based on what I pull from the database.  If I use ddlState.SelectedIndex = 4 (or whatever number within my range) it works just fine.  However I need to pull the # from the database.  I have tried all manner of things, and getting several different errors.  Here are a few things I've tried that aren't working.

1.  ddlState.SelectedIndex = myDataReader["State"];
Error: CS0266: Cannot implicitly convert type 'object' to 'int'. An explicit conversion exists (are you missing a cast?)

2. ddlState.SelectedIndex = Convert.ToInt32(myDataReader["State"]);
Error:System.Web.HttpException: Cannot have multiple items selected in a DropDownList.

3. ddlState.SelectedIndex = ddlState.Items.IndexOf(ddlState.Items.FindByValue(myDataReader["State"].ToString()));
No Error, but does nothing......Index 0 ends of being selected.

4. int i = myDataReader["State"];
    ddlState.Items[i].Selected = true;
Error:  System.Web.HttpException: Cannot have multiple items selected in a DropDownList.

Errors in 2 and 4 are happening I because Index 0 is Selected on Page Load, but I don't understand how this error is happening if the SelectedIndex is being changed before the page is refreshed on a postback?

Please offer advice.  I'm open to change the way I am doing this as well, if a better methodology exists.

Thanks,
Pete.
<asp:DropDownList ID="ddlState" runat="server">
     <asp:ListItem Value="0" Text="AL" />
     <asp:ListItem Value="1" Text="AK" />
     <asp:ListItem Value="2" Text="AZ" />
     <asp:ListItem Value="3" Text="AR" />
     <asp:ListItem Value="4" Text="CA" />
     <asp:ListItem Value="5" Text="CO" />
     //Rest of the 45 States
</asp:DropDownList>

Open in new window

0
Comment
Question by:P1ST0LPETE
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
5 Comments
 
LVL 13

Expert Comment

by:crazyman
ID: 22715372
Have you tried .SelectedValue
0
 
LVL 10

Author Comment

by:P1ST0LPETE
ID: 22715390
Found my answer:

int i = myDataReader["State"];
ddlState.SelectedIndex = i;
0
 
LVL 10

Accepted Solution

by:
P1ST0LPETE earned 0 total points
ID: 22715401
Correction:

int i = Convert.ToInt32(myDataReader["State"]);
ddlState.SelectedIndex = i;
0
 
LVL 13

Expert Comment

by:crazyman
ID: 22715449
I wouldnt save indexes, better of saving the id or code and using SelectedValue, what happens if you add another state in the middle then he indexes wont line up...
0
 
LVL 6

Expert Comment

by:esolve
ID: 22741208
I've just noticed that in your 2nd example you should call the ddlState.ClearSelection(); method to prevent this error from happening again.
0

Featured Post

Forrester Webinar: xMatters Delivers 261% ROI

Guest speaker Dean Davison, Forrester Principal Consultant, explains how a Fortune 500 communication company using xMatters found these results: Achieved a 261% ROI, Experienced $753,280 in net present value benefits over 3 years and Reduced MTTR by 91% for tier 1 incidents.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
A quick way to get a menu to work on our website, is using the Menu control and assign it to a web.sitemap using SiteMapDataSource. Example of web.sitemap file: (CODE) Sample code to add to the page menu: (CODE) Running the application, we wi…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…

724 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question