SelectedItem.Value

Discussion in 'ASP .Net' started by Martyn Fewtrell, May 23, 2005.

  1. I want to update a database record with a number of fields - doesn't sound
    to difficult at this stage!

    I have a system to select the correct record from the database and load the
    data into a form across a number of text boxes and a single drop down list
    (the idea being that the text boxes can be edited and the drop down listed
    altered to reflect a new value if necessary). The drop down list in the form
    is loaded from the appropriate database table with the various options and
    then set to the correct value to match the record.

    daGroups.Fill(dsLocalInfo, "tblGroups")
    ddlGroup.DataBind
    ddlGroup.SelectedItem.Text = litGroup.Text
    ddlGroup.Visible = True
    txtName.Text = litName.Text
    txtDescription.Text = litName.Text
    etc etc etc

    I then use an update command to update the record in the database

    cmdUpdateLocalInfo.Parameters("GroupID").Value = ddlGroup.SelectedItem.Value
    cmdUpdateLocalInfo.Parameters("Name").Value = txtName.Text
    etc etc

    The bit I don't understand is that if I call the update without having first
    used the drop down list control the update succeeds but updates the GroupID
    to be the value which naturally would be at the top of the list if I hadn't
    set it in the earlier statement (i.e. ddlGroup.SelectedItem.Text =
    litGroup.Text). Where as if I call the update after having changed the
    selection on the drop down list control the update works and accepts the
    correct value from the drop down list control. Almost as if the selected
    item has to be selected for it to register, where as I have selected in the
    code previously (i.e. most records wont require changing groups on the drop
    down list).

    Hopefully this might ring a bell with somebody as I have done this (or
    similar) many times before but cant work out what is wrong here!

    Martyn Fewtrell
     
    Martyn Fewtrell, May 23, 2005
    #1
    1. Advertisements

  2. Martyn Fewtrell

    Guest Guest

    Are you checking that the page has not been posted back before selecting the
    DDL item?
     
    Guest, May 23, 2005
    #2
    1. Advertisements

  3. Thanks Saleek but I'm not quite sure where you are coming from with that
    one.

    When the page originally loads, the record is displayed using literals and
    then the user has the option to Delete the Record, Update the Record etc. If
    the user selects the Update option the page posts back, the values from the
    literal are loaded into the text boxes and the drop down list, visibility is
    turned off on the literals and on for the text boxes and the drop down list.
    The value for the drop down list is set to the correct value for that
    record.

    The user can then change any of the values before calling the Update routine
    with another button.

    The drop down list isn't loaded with any data until the user elect to Update
    the record.

    What are you thinking?

    Martyn Fewtrell
     
    Martyn Fewtrell, May 23, 2005
    #3
  4. Martyn Fewtrell

    Guest Guest

    Okay, that rules out what I was thinking. And you have viewstate turned on
    for the DDL yes?

    I would edit out your execution of the query and output the DDL value to a
    label on potsback to check which values are being selected.
     
    Guest, May 23, 2005
    #4
  5. I've worked it out and as usual its human error!!!!

    ddlGroup.SelectedItem.Text = litGroup.Text - changes the value of the text
    on the SelectedItem rather than Selecting the item to the required text
    value.

    I now use ddlGroup.SelectedValue = Session("GroupID") - where
    Session("GroupID") holds the value of the GroupID.

    This sets the drop down list correctly and therefore saves the correct value
    during the update!

    Thanks anyway

    Martyn Fewtrell
     
    Martyn Fewtrell, May 24, 2005
    #5
    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.