473,386 Members | 1,752 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,386 software developers and data experts.

asp:dropdownlist - SelectedItem.Value

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(Object 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.DataSource = ds1.Tables["CountriesList"].DefaultView;

ddlCountries1.DataTextField = "Country";
ddlCountries1.DataValueField = "TimeRange");
ddlCountries1.DataBind();

//close the sql connection
myConnection.Close();

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

*** aspx page ****
<asp:DropDownList ID="ddlCountries1" CssClass="ddl" runat="server"
Width="180"></asp:DropDownList>
<br>
<asp:Button ID="btnSubmit" Text="Submit" runat="server"
OnClick="ddlChange" />
</p>

<asp:label Font-Bold="true" text='<%# ddlCountries1.SelectedItem.Text
%>' runat="server"/><br><br>
<br>
<asp:label ID="lblTime" text='<%# ddlCountries1.SelectedItem.Value %>'
runat="server"/>

Nov 19 '05 #1
2 4700
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********************@g47g2000cwa.googlegrou ps.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(Object 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.DataSource = ds1.Tables["CountriesList"].DefaultView;

ddlCountries1.DataTextField = "Country";
ddlCountries1.DataValueField = "TimeRange");
ddlCountries1.DataBind();

//close the sql connection
myConnection.Close();

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

*** aspx page ****
<asp:DropDownList ID="ddlCountries1" CssClass="ddl" runat="server"
Width="180"></asp:DropDownList>
<br>
<asp:Button ID="btnSubmit" Text="Submit" runat="server"
OnClick="ddlChange" />
</p>

<asp:label Font-Bold="true" text='<%# ddlCountries1.SelectedItem.Text
%>' runat="server"/><br><br>
<br>
<asp:label ID="lblTime" text='<%# ddlCountries1.SelectedItem.Value %>'
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(Object 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.DataSource = ds1.Tables["CountriesList"].DefaultView;
ddlCountries1.DataTextField = "Country";
ddlCountries1.DataValueField = "TimeRange");
ddlCountries1.DataBind();
//close the sql connection
myConnection.Close();
}
if (IsPostBack){
Page.DataBind();
}
}
*** aspx page ****
<asp:DropDownList ID="ddlCountries1" CssClass="ddl" runat="server"
Width="180"></asp:DropDownList>
<br>
<asp:Button ID="btnSubmit" Text="Submit" runat="server"
OnClick="ddlChange" />
</p>
<asp:label Font-Bold="true" text='<%# ddlCountries1.SelectedItem.Text
%>' runat="server"/><br><br>
<br>
<asp:label ID="lblTime" text='<%# ddlCountries1.SelectedItem.Value %>'
runat="server"/

Nov 19 '05 #3

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

Similar topics

1
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...
1
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...
2
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...
1
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...
7
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...
1
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"...
1
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...
2
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...
4
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...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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...
0
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,...
0
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...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.