469,625 Members | 1,211 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,625 developers. It's quick & easy.

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 4554
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 discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by Marius | last post: by
2 posts views Thread by amessimon | last post: by
1 post views Thread by Urmal Patel via .NET 247 | last post: by
7 posts views Thread by Lastie | last post: by
1 post views Thread by Xuanly ly via .NET 247 | last post: by
1 post views Thread by Miguel Dias Moura | last post: by
2 posts views Thread by jeffmagill | last post: by
reply views Thread by gheharukoh7 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.