asp:dropdownlist - SelectedItem.Value

Discussion in 'ASP .Net' started by HH, Aug 14, 2005.

  1. HH

    HH Guest

    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"/>
     
    HH, Aug 14, 2005
    #1
    1. Advertisements

  2. HH

    John Smith Guest

    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.
     
    John Smith, Aug 14, 2005
    #2
    1. Advertisements

  3. HH

    tom pester Guest

    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
     
    tom pester, Aug 14, 2005
    #3
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.