PC Review


Reply
Thread Tools Rating: Thread Rating: 5 votes, 2.60 average.

asp:dropdownlist - SelectedItem.Value

 
 
HH
Guest
Posts: n/a
 
      14th Aug 2005
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 ****
<aspropDownList ID="ddlCountries1" CssClass="ddl" runat="server"
Width="180"></aspropDownList>
<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"/>

 
Reply With Quote
 
 
 
 
John Smith
Guest
Posts: n/a
 
      14th Aug 2005
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" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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 ****
> <aspropDownList ID="ddlCountries1" CssClass="ddl" runat="server"
> Width="180"></aspropDownList>
> <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"/>
>



 
Reply With Quote
 
tom pester
Guest
Posts: n/a
 
      14th Aug 2005
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 ****
> <aspropDownList ID="ddlCountries1" CssClass="ddl" runat="server"
> Width="180"></aspropDownList>
> <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"/



 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
asp:DropDownList SelectedItem problem jeffmagill@gmail.com Microsoft Dot NET 2 20th Dec 2005 03:53 PM
DropDownList: SelectedItem is always top item Sam C Microsoft ASP .NET 3 15th May 2005 06:45 PM
DropDownList SelectedItem Problem Ivan Microsoft ASP .NET 4 25th Nov 2003 03:14 PM
Re: Dropdownlist and SelectedItem in ASP.Net Jos Branders Microsoft ASP .NET 6 25th Jun 2003 09:15 PM
Re: Dropdownlist and SelectedItem in ASP.Net Marina Microsoft ASP .NET 0 25th Jun 2003 02:40 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 11:17 AM.