473,625 Members | 2,662 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

55 New Member
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 13713
dmj07
55 New Member
Managed to solve this for anybody who may be interested.

New code in OnDataBound function of drop down list.

OnDataBound="dd lID_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
39554
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 value, he/she clicks on the submit button. When he/she does that I would like 2 things to happen: the selected value of the drop down list is passed to another html page which opens in a new brower window open. Could you please help me do...
2
13305
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 page is refreshed. any ideas? Thanks in advance. Patrick
4
6294
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 the choice from the drop down box. Something like: <form name="populatefrm" id="contactfrm" method="post"
3
26292
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 my code to work. Below is some code which highlights my problem. All I want to do is set the lable control's text property to the value of the selected drop down list value - in this example i've shown the three ways i've tried. Please help!
3
2988
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 the drop down box to go back to what the value was before the user tried to change it, but at that point I will not know what the original value was. Or is there a drop down control "revert" method, or is there any way of knowing what the original...
4
9176
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" Runat="server"></asp:dropdownlist> heres the Java var myDropDownList = frmMain.ddlDealers;
2
7278
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 function. See below... function CalculateGrandTotal(SubTotal, ServiceFee) { var shippingSelect = document.getElementById('shipping'); var shippingRequested = shippingSelect.options.value; var grandTotal = Number(shippingRequested) +...
2
1928
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 into B. for this I use iframe & dynamic population. so far so good. now there is a 3rd variable based on which I need to make the B's drop down selected.
15
23267
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" width="138" height="26"><small><font face="Verdana"><strong>Branch</strong></font></small></td> <td width="379" height="26"> <select name="branch" size="1" onChange="messValue()"> <option value="">(select branch)</option> <% ...
0
8251
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
8182
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 synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8688
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
7178
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 launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6115
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
4085
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...
0
4188
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2614
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
1496
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.