Results 1 to 10 of 10

Thread: [RESOLVED] Not getting value from my dropdownlist on RowUpdating event

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Mar 2008
    Posts
    513

    Resolved [RESOLVED] Not getting value from my dropdownlist on RowUpdating event

    Hello: I have a gridview that has 3 dropdowns. And when I am in edit mode, when updating via the RowUpdating event,
    I am not getting a value from my 2nd or 3rd drop downlists.

    Here's the code from RowUpdating:

    Code:
      Protected Sub gridOutdoor_RowUpdating(ByVal sender As Object, ByVal e As GridViewUpdateEventArgs)
    'WHEN DEBBUGING, the text for ddBodyColor.Text is an empty string.  I had selected a value prior to this event firing." 
            Dim ddBodyColor As DropDownList = DirectCast(gridOutdoor.Rows(e.RowIndex).FindControl("ddBodyColor"), DropDownList)
            
               End Sub
    Here's the source code for ddBodyColor:
    Code:
    <EditItemTemplate> 
                    <asp:DropDownList ID="ddBodyColor"   runat="server"  TabIndex="2" AppendDataBoundItems="true"  Enabled="true"  DataTextField="bodyColor" DataValueField="bodyColor"><asp:ListItem Text="Select" Value="" /></asp:DropDownList>
                </EditItemTemplate>
    Any help is truly appreciated.

    Thanks,
    Proctor

  2. #2
    Frenzied Member brin351's Avatar
    Join Date
    Mar 2007
    Location
    Land Down Under
    Posts
    1,293

    Re: Not getting value from my dropdownlist on RowUpdating event

    You comment that "'WHEN DEBBUGING, the text for ddBodyColor.Text is an empty string." have you tried ddBodyColor.selectedValue, I think it's best to get/set the value of items in the drop down.

    Also when are dropdown 2,3 populated? are you sure there are items that can be selected.
    The problem with computers is their nature is pure logic. Just once I'd like my computer to do something deluded.

  3. #3

    Thread Starter
    Fanatic Member
    Join Date
    Mar 2008
    Posts
    513

    Re: Not getting value from my dropdownlist on RowUpdating event

    Hi Brin351: Thank you for your reply. Yes, I've tried using .selectValue also it still gives me no value.

    Can you please explain more to me - when you say, "I think it's best to get/set the value of items in the drop down"

    Can you please explain to me why you think it's better?

    When in edit mode, after the user selects from cbo1 (ddProduct), cbo2(ddBodyColor) and cbo3(ddPower) populate. Here's the code I use to do that:

    Code:
     Protected Sub ddProductdg_SelectedIndexChanged(sender As Object, e As EventArgs)
    
            'since this event is raised by the control in the gridview row
            'we could do some reverse engineerign to get reference to the gridview row
            Dim gvrow As GridViewRow = DirectCast(DirectCast(sender, DropDownList).NamingContainer, GridViewRow)
            'get reference to the categories & subcategories dropdownlist
            Dim ddProduct As DropDownList = DirectCast(gvrow.FindControl("ddProduct"), DropDownList)
            'BODY COLOR
            Dim ddBodyColor As DropDownList = DirectCast(gvrow.FindControl("ddBodyColor"), DropDownList)
            ddBodyColor.Items.Clear()
            ddBodyColor.Items.Insert(0, "Select")
            ddBodyColor.Items(0).Value = ""
            ddBodyColor.SelectedValue = ""
            ddBodyColor.DataSource = RetrieveBodyColor(ddProduct.SelectedValue)
            ddBodyColor.DataTextField = "bodyColor"
            ddBodyColor.DataValueField = "bodyColor"
            ddBodyColor.DataBind()
            'POWER
            Dim ddPower As DropDownList = DirectCast(gvrow.FindControl("ddPower"), DropDownList)
            ddPower.Items.Clear()
            ddPower.Items.Insert(0, "Select")
            ddPower.Items(0).Value = ""
            ddPower.SelectedValue = ""
            ddPower.DataSource = RetrievePower(ddProduct.SelectedValue)
            ddPower.DataTextField = "power"
            ddPower.DataValueField = "power"
            ddPower.DataBind()
        End Sub
    Here's the code for RetrieveBodyColor:

    Code:
     Private Function RetrieveBodyColor(Product As String) As DataTable
            'fetch the connection string from web.config
            Dim connString As String = ConfigurationManager.ConnectionStrings("SloanLEDLightingSystemsQuoteToolConnectionString").ConnectionString()
    
            'SQL statement to fetch prodyct subcategories
            Dim sql As String = "select distinct cat2 as bodyColor from QuoteItems WHERE cat1 ='" & Product & "'"
    
            Dim dtBodyColor As New DataTable()
            'Open SQL Connection
            Using conn As New SqlConnection(connString)
                conn.Open()
                'Initialize command object
                Using cmd As New SqlCommand(sql, conn)
                    Dim adapter As New SqlDataAdapter(cmd)
                    'Fill the result set
                    adapter.Fill(dtBodyColor)
    
                End Using
            End Using
            Return dtBodyColor
        End Function
    Thank you again for your help.
    Proctor

  4. #4
    Frenzied Member brin351's Avatar
    Join Date
    Mar 2007
    Location
    Land Down Under
    Posts
    1,293

    Re: Not getting value from my dropdownlist on RowUpdating event

    Your code looks like it should work so I'm thinking your binding the grid onpage load everytime therefore resetting dropdown 2,3. I'm guessing here but if you loose values on postback check when your binding controls. If viewstate is enabled then the state of controls should be maintained.

    I can't say I've done cascading dropdowns in a grid in edit mode.... maybe there is a catch to it let us know.

    dropdown list Items can have text and value, the text is displayed to the user and the value is generally the data your code/database work with. You don't have to set the value but personally I think it's good practise to consider text for the user and value for code. Dropdown controls are rendered as select elements in html, the "text" is just whats between the <option> tags value is a attribute on the tag.

    Code:
    <select>
      <option value="3212">Mary</option>
      <option value="9873">Bob</option>
    </select>
    The problem with computers is their nature is pure logic. Just once I'd like my computer to do something deluded.

  5. #5

    Thread Starter
    Fanatic Member
    Join Date
    Mar 2008
    Posts
    513

    Re: Not getting value from my dropdownlist on RowUpdating event

    I noticed something today when testing it more thouroughly.....

    If (while in edit mode), I select from cbo1 and during rowupdating...this is when i don't see a value in cbo2 or cbo3;

    however, if i select from cbo2 or cbo3, during rowupdating and not from cbo1, i see a value for both cbo2 and cbo3;

    cbo1 is the only combo that has an OnSelectedIndexChanged event, so I put a breakpoint in this event and noticed that it's firing when clicking the update button.
    there's code in there that sets the selectedvalue="" I don't want it going in there when I'm updating.


    Do you know how I can prevent this event from firing when updating?

    thanks again .....

    Proctor

  6. #6
    Frenzied Member brin351's Avatar
    Join Date
    Mar 2007
    Location
    Land Down Under
    Posts
    1,293

    Re: Not getting value from my dropdownlist on RowUpdating event

    Hmm seems strange that the method ddProductdg_SelectedIndexChanged is being raised on grid update if you're not calling it. Without seeing all the code I really can't say why. I googled and found tthe following example. The first part is just setting up the datasource after that the code and explanation for events and methods to produce what you need is shown. It seems a good walkthrough so I'd be examining what they and you are doing in detail and hopefully the little light bulb goes off.... the way they are binding dropdowns in grid_rowdatabound may be key.
    The problem with computers is their nature is pure logic. Just once I'd like my computer to do something deluded.

  7. #7

    Thread Starter
    Fanatic Member
    Join Date
    Mar 2008
    Posts
    513

    Re: Not getting value from my dropdownlist on RowUpdating event

    Hi brin351: did you mean to insert an url to an example? I'm not seeing it.....

    Thanks again for your help,

    Proctor

  8. #8
    Frenzied Member brin351's Avatar
    Join Date
    Mar 2007
    Location
    Land Down Under
    Posts
    1,293

    Re: Not getting value from my dropdownlist on RowUpdating event

    The problem with computers is their nature is pure logic. Just once I'd like my computer to do something deluded.

  9. #9

    Thread Starter
    Fanatic Member
    Join Date
    Mar 2008
    Posts
    513

    Re: Not getting value from my dropdownlist on RowUpdating event

    Quote Originally Posted by brin351 View Post
    Thanks for sending. Looks like a bit of work, but worth learning. I'm still stuck so I'm going to start a test page over using this as a guideline.

    Hopefully, it will resolve the issue.

    Thanks again.

    proctor

  10. #10

    Thread Starter
    Fanatic Member
    Join Date
    Mar 2008
    Posts
    513

    Re: Not getting value from my dropdownlist on RowUpdating event

    Brin351: I just wanted to tell you that in working through your example, I was able to make it work! The bad news is that I'm not really sure what part made it work; however, it works and I'm happy!!!

    Thanks again for sending it to me.

    Proctor

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width