473,654 Members | 3,028 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

asp:dropdownlis t - SelectedItem.Va lue

HH
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
Canada 2
USA 3
Kenya 1
UK 3

When a user selects a country say "UK" i want to store that country
name and value. But when pressing submit, the value stored is "USA, 3"

Looking around google, it seems that the ddl always takes the first
unique value in the table. Has anyone else come across this and more
importantly found a way around it???

Thanking you all in advance
H

*** Codebehind ***
namespace datafill {
public class countries : Page {
public DropDownList ddlCountries1;
public Label lblTime;
protected void Page_Load(Objec t Src, EventArgs E) {
if (!IsPostBack){
SQL CONNECTION AND QUERY GOES HERE
SqlDataAdapter myCommand1 = new SqlDataAdapter( "SELECT * FROM
ycQryCountries" , myConnection);
DataSet ds1 = new DataSet();
myCommand1.Fill (ds1, "CountriesList" );
ddlCountries1.D ataSource = ds1.Tables["CountriesL ist"].DefaultView;

ddlCountries1.D ataTextField = "Country";
ddlCountries1.D ataValueField = "TimeRange" );
ddlCountries1.D ataBind();

//close the sql connection
myConnection.Cl ose();

}
if (IsPostBack){
Page.DataBind() ;
}
}

*** aspx page ****
<asp:DropDownLi st ID="ddlCountrie s1" CssClass="ddl" runat="server"
Width="180"></asp:DropDownLis t>
<br>
<asp:Button ID="btnSubmit" Text="Submit" runat="server"
OnClick="ddlCha nge" />
</p>

<asp:label Font-Bold="true" text='<%# ddlCountries1.S electedItem.Tex t
%>' runat="server"/><br><br>
<br>
<asp:label ID="lblTime" text='<%# ddlCountries1.S electedItem.Val ue %>'
runat="server"/>

Nov 19 '05 #1
2 4722
I don't know what's going on in your ddlChange event, but I would probably
make the DataTextField and DataValueField both set to the unique country
name and then, when the submit button is clicked, requery the ycQryCountries
table to get the TimeRange associated with that country. Once you have the
value, you can store it and set the lblTime lable from the code behind
rather than through an inline code block.

Personally, I always make the ddl DataValueField have a unique value, so
I've never run into your problem. Another kind of hack way to do it without
having to requery for the TimeRange would be to store a delimited value in
the DataValueField (e.g., "UK;3"). This would ensure a unique value, but
then you would have to use the Split function to separate the UK from the 3
before storing it back to the database and assigning the lblTime text.

I'm sure there are other ways, but that's my 2 cents.

"HH" <ha************ @hotmail.com> wrote in message
news:11******** ************@g4 7g2000cwa.googl egroups.com...
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
Canada 2
USA 3
Kenya 1
UK 3

When a user selects a country say "UK" i want to store that country
name and value. But when pressing submit, the value stored is "USA, 3"

Looking around google, it seems that the ddl always takes the first
unique value in the table. Has anyone else come across this and more
importantly found a way around it???

Thanking you all in advance
H

*** Codebehind ***
namespace datafill {
public class countries : Page {
public DropDownList ddlCountries1;
public Label lblTime;
protected void Page_Load(Objec t Src, EventArgs E) {
if (!IsPostBack){
SQL CONNECTION AND QUERY GOES HERE
SqlDataAdapter myCommand1 = new SqlDataAdapter( "SELECT * FROM
ycQryCountries" , myConnection);
DataSet ds1 = new DataSet();
myCommand1.Fill (ds1, "CountriesList" );
ddlCountries1.D ataSource = ds1.Tables["CountriesL ist"].DefaultView;

ddlCountries1.D ataTextField = "Country";
ddlCountries1.D ataValueField = "TimeRange" );
ddlCountries1.D ataBind();

//close the sql connection
myConnection.Cl ose();

}
if (IsPostBack){
Page.DataBind() ;
}
}

*** aspx page ****
<asp:DropDownLi st ID="ddlCountrie s1" CssClass="ddl" runat="server"
Width="180"></asp:DropDownLis t>
<br>
<asp:Button ID="btnSubmit" Text="Submit" runat="server"
OnClick="ddlCha nge" />
</p>

<asp:label Font-Bold="true" text='<%# ddlCountries1.S electedItem.Tex t
%>' runat="server"/><br><br>
<br>
<asp:label ID="lblTime" text='<%# ddlCountries1.S electedItem.Val ue %>'
runat="server"/>

Nov 19 '05 #2
Hi,

Your are not using isPostBack(). Make sure that you don't databind on a postback
cause that erases the selected value and effectivly reseting it to the first
value in the list.

The guy in the previous post had the same problem btw so dont feel to bad.
It's a beginners mistake.

Let me know if you need furhter help.

Cheers,
Tom Pester
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
Canada 2
USA 3
Kenya 1
UK 3
When a user selects a country say "UK" i want to store that country
name and value. But when pressing submit, the value stored is "USA, 3"

Looking around google, it seems that the ddl always takes the first
unique value in the table. Has anyone else come across this and more
importantly found a way around it???

Thanking you all in advance
H
*** Codebehind ***
namespace datafill {
public class countries : Page {
public DropDownList ddlCountries1;
public Label lblTime;
protected void Page_Load(Objec t Src, EventArgs E) {
if (!IsPostBack){
SQL CONNECTION AND QUERY GOES HERE
SqlDataAdapter myCommand1 = new SqlDataAdapter( "SELECT * FROM
ycQryCountries" , myConnection);
DataSet ds1 = new DataSet();
myCommand1.Fill (ds1, "CountriesList" );
ddlCountries1.D ataSource = ds1.Tables["CountriesL ist"].DefaultView;
ddlCountries1.D ataTextField = "Country";
ddlCountries1.D ataValueField = "TimeRange" );
ddlCountries1.D ataBind();
//close the sql connection
myConnection.Cl ose();
}
if (IsPostBack){
Page.DataBind() ;
}
}
*** aspx page ****
<asp:DropDownLi st ID="ddlCountrie s1" CssClass="ddl" runat="server"
Width="180"></asp:DropDownLis t>
<br>
<asp:Button ID="btnSubmit" Text="Submit" runat="server"
OnClick="ddlCha nge" />
</p>
<asp:label Font-Bold="true" text='<%# ddlCountries1.S electedItem.Tex t
%>' runat="server"/><br><br>
<br>
<asp:label ID="lblTime" text='<%# ddlCountries1.S electedItem.Val ue %>'
runat="server"/

Nov 19 '05 #3

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

Similar topics

1
342
by: Marius | last post by:
Hi all, I need an asp:dropdownlist on a webform that is databound to a dataset. This works fine and returns the data to populate my dropdownlist. In the DB I only have valid data, but I need a option like "< Select a Region>" to be displayd as teh selected value. Currently it displays the first value of the returned dataset. I cannot seem to get this right, below is my code:
1
2465
by: Harry | last post by:
Hi, Just seeing if anyone can help me with a problem I am having with drop down lists. - I have a asp:dropdownlist that gets its values from a database. - The user then selects a value then presses the submit button. - The submit button then calls a function that sets a asp:lable to that value selected. - PROBLEM: No matter what value the user selects, the asp:lable always
2
9563
by: amessimon | last post by:
I need to display a drop down list which holds up to 250 listitems. I'd like to create this programmatically rather than have to hardcode it into the page. For example <asp:DropDownList id="numYears" runat="server" name="numYears"> <asp:ListItem Value="0" Text="0 years" Selected="true" /> <asp:ListItem Value="1" Text="1 year"/>
1
2280
by: Urmal Patel via .NET 247 | last post by:
I have problem with asp:Dropdownlist. I have a dropdownlist control in Page1.aspx and I want to assign value and Text to dropdown list form Page2.aspx from java script function. I am able to assign values to asp:textbox but not asp:dropdownlist code is as follow when i call this function BindShipper(); function BindShipper(SearchResultFor, vendorID, localID,companyName, companyAdd1, companyAdd2, companyCity, companyState, companyZip,...
7
2032
by: Lastie | last post by:
Hi all, I’ve got a ‘dropdownlist’ web control and I can add ‘listitem’ no problem. I can also bind data from an SQL database fine. My problem is that I want to do both at the same time to allow me to have the first option in the list a ‘listitem’ saying something like ‘please pick an option’, and then the rest of options coming from the database. <asp:dropdownlist id="fm_Category" runat="server"...
1
1455
by: Xuanly ly via .NET 247 | last post by:
asp:dropdownlist always display infront of everything and I can't make it display behind HELP HELP help!!!. <form id="Form1" method="post" runat="server"> <asp:TextBox id="TextBox1" runat="server" style="Z-INDEX: 1; LEFT: 50px; POSITION: absolute; TOP: 300px" Width="250px"></asp:TextBox> <asp:panel id="pnCal runat="server" Width="156px" Height="128px" style=" LEFT: 50px; POSITION: absolute; TOP: 300px"> <asp:Calendar id="Calendar1"...
1
1557
by: Miguel Dias Moura | last post by:
Hello, I have two questions concerning Asp:DropDownList 1. How to fill a DropDownList from runtime and set its default item? 2. How do use a DropDownList in each datagrid record? All dropdownlist should be filled with the values in field NAME from the same dataset used in the datagrid. The default value of each dropdownlist should be the value of NAME by
2
1317
by: jeffmagill | last post by:
Ive stumbled into one of those infamous problems where something stopped working for apparently no reason - in this case MyDropDownList.SelectedItem.Value. Im not sure exactly which group this should be posted in either so Im just trying general. I have an asp:dropdownlist in a form that is loaded with records from a database. Upon clicking a button, a subroutine SubmitHandler is called. The problem is that no matter which entry in the...
4
3238
by: garyhumf7 | last post by:
I have placed a dropdown list in the edititemtemplate of a formview control and followed specific MSDN instructions on how to bind the list source to a separate SQL lookup data source but have the formview bound to the main data source. If I set the selecteditem value to bind to the formview control I always get the following error: 'DropDownList2' has a SelectedValue which is invalid because it does not exist in the list of items. This is...
0
8375
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
8815
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...
1
8482
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 most users, this new feature is actually very convenient. If you want to control the update process,...
0
7306
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
6161
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
5622
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4294
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2714
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
1593
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.