473,378 Members | 1,470 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

C# Web App: Setting selected value of drop down list on page load

55
Hi all,

I was wondering of an easy way to set the selected value of a drop down list by using a value retrieved from the database. Here is my code:


Expand|Select|Wrap|Line Numbers
  1.  
  2. SqlCommand cmdUser = new SqlCommand("SELECT ID FROM Tble WHERE ID = 2", connSQL);
  3.  
  4.         cmdUser.CommandType = CommandType.Text;
  5.  
  6.         SqlDataReader rdrUser = cmdUser.ExecuteReader();
  7.  
  8.         while (rdrUser.Read())
  9.         {
  10.             ddlID.SelectedValue = rdrUser.GetString(0);
  11.         }
  12.         rdrUser.Close();
  13.  
  14.  
On the front end .aspx page the drop down list has a bound data source so I need to set the selected value of this drop down list to the database one.

Many thanks in advance.
Sep 30 '08 #1
1 13680
dmj07
55
Managed to solve this for anybody who may be interested.

New code in OnDataBound function of drop down list.

OnDataBound="ddlID_DataBound"

Expand|Select|Wrap|Line Numbers
  1. while (rdrUser.Read())
  2. {
  3.             ddlMaterialType.ClearSelection();
  4.             string test = rdrUser.GetString(0);
  5.             ddlID.Items.FindByValue(test).Selected = true;
  6. }
  7.  
Sep 30 '08 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: maflu | last post by:
Hello, I am rather a beginner in Javascript and have the following problem. There is a drop down list on my Web page with various values to be selected by the user. Once the user has selected a...
2
by: Patrick | last post by:
Hello, I have a drop down list that when a value is selected the page refreshes itself but the selected value changes back to the default value. I would like to keep the selected value after the...
4
by: Dan | last post by:
Can anyone offer suggestions on how to do this or if it is possible? I have a form that uses a drop down box and 2 text fields. What I am trying to do is have the value of each text box set by...
3
by: Stephen Adam | last post by:
Hi there, I'm sure i'm missing something really simple here, all i want to do is get the value of the selected item in a list box. Even after much fiddling about last night I still could not get...
3
by: John Walker | last post by:
Hi, On an ASP.NET page I have a drop down list control. When the user pulls down the list and makes a selection, I perform validation, and if the validation fails I want the selected item in...
4
by: nz mark in aus | last post by:
Hi there I am populating an empty <asp:dropdownlist on the client side using Javascript using the following code Heres my dropdown list <asp:dropdownlist id="ddlDealers"...
2
by: rk330 | last post by:
Greetings I have a drop down list populated by my sql server database. The data on this ASP page consists of numbers that I use to calculate a grand total. This is performed by a javascript...
2
by: comerica1 | last post by:
I have the basic structure as follows: 2 drop downs A & B. A is populated using (datasource methods - from database). B is poulated using A's selected value as input and retrieve the list...
15
by: ajos | last post by:
Hello all, Im making a dependent drop down for my application for filtering purpose. I have populated the drop down from the database and retrieved in the html. <tr> <td valign="top"...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.