How to get value of DropDownList inside a GridView

Last post 07-07-2009 2:47 PM by gumbeaux70714. 10 replies.

Sort Posts:

  • How to get value of DropDownList inside a GridView

    07-07-2009, 11:47 AM
    • Member
      21 point Member
    • gumbeaux70714
    • Member since 07-21-2008, 2:33 PM
    • Baker, LA
    • Posts 73

    I posted this before and thought my problem was solved but it wasn't.  Can anyone out ther tell me how to get the value of a DropDownList within a GridView.  I can't use the e.CommandArgument like I thought because I'm already passing information through it.  My code for my template field is as follows:

    <ItemTemplate>
        <table>
            <tr>
                <td align="center">
                    <asp:DropDownList ID="cboRank" runat="server" AutoPostBack="False"
                             DataSourceID="SqlDataSourceRank" DataTextField="rank" DataValueField="rank">
                    </asp:DropDownList>
                                        
                    <asp:SqlDataSource ID="SqlDataSourceRank" runat="server"
                             ConnectionString="<%$ ConnectionStrings:MySchoolBizDB %>"


                             SelectCommand="SELECT DISTINCT [rank] FROM [collegefavs]
                                                          WHERE ([memberid] = @memberid) ORDER BY [rank]">
                                                           
                              <SelectParameters>
                                  <asp:ControlParameter ControlID="lblMemberID" Name="memberid" PropertyName="Text" />
                               </SelectParameters>
                       </asp:SqlDataSource>
                   </td>
               </tr>
               <tr>
                   <td>
                       <asp:Button ID="Update" runat="server" CommandName="UpdateClick" Text="Update Rank" CommandArgument='<%# Eval("collegeid") %>'/>
                   </td>
               </tr>
           </table>                       
    </ItemTemplate>

    I need to find the value for my update statement which is as follows:

    If e.CommandName = "UpdateClick" Then
                Dim sqlGetInfo As New SqlCommand("SELECT * FROM collegefavs " & _
                                                 "WHERE memberid = '" & lblMemberID.Text & "' " & _
                                                 "AND collegeid = '" & e.CommandArgument.ToString & "'", _
                                                 dbConnection)
                Dim rdrGetInfo As SqlDataReader, newRank As Integer = 0

                Try
                    dbConnection.Open()
                    rdrGetInfo = sqlGetInfo.ExecuteReader

                    Do While rdrGetInfo.Read
                        dbDataSource.UpdateCommandType = SqlDataSourceCommandType.Text
                        dbDataSource.UpdateCommand = "UPDATE collegefavs SET rank = @ rank " & _
                                                     "WHERE memberid = '" & lblMemberID.Text & "' " & _
                                                     "AND collegeid = '" & e.CommandArgument.ToString & "'"

                        'dbDataSource.UpdateParameters.Add("rank", GridView2.FindControl("cboRank").
                    Loop


    What is the correct syntax for the above line dbDataSource.UpdateParameter.Add("rank", ????)

    Someone please help me.  I'm fairly new to this and I've been working on this for 3 or 4 days now with no success.

    Dat Baker Boy Gumbeaux
  • Re: How to get value of DropDownList inside a GridView

    07-07-2009, 12:22 PM
    • Member
      21 point Member
    • gumbeaux70714
    • Member since 07-21-2008, 2:33 PM
    • Baker, LA
    • Posts 73

    I need to be able to distinguish which row the button was one that I click.  Someone PLEASE help me. PLEEEEEASE!!!!!!!!!!!!

    Dat Baker Boy Gumbeaux
  • Re: How to get value of DropDownList inside a GridView

    07-07-2009, 12:27 PM
    • All-Star
      26,894 point All-Star
    • PeteNet
    • Member since 01-21-2009, 1:15 PM
    • Posts 3,834

    use this :

    Dim btn As Button = DirectCast(sender, Button)
    Dim gvr As GridViewRow = DirectCast(btn.NamingContainer, GridViewRow)

    that will give you the row. gvr.RowIndex will give you the index.

    and you could do a gvr.Cells[index].Text to get BoundField values whereas gvr.FindControl("someID") will give you the template fields.



    Regards,
    Peter
  • Re: How to get value of DropDownList inside a GridView

    07-07-2009, 12:33 PM
    • Member
      21 point Member
    • gumbeaux70714
    • Member since 07-21-2008, 2:33 PM
    • Baker, LA
    • Posts 73

    Thanks for the post PeteNet. Can you use my example?  I'm not sure about the sender or the NamingContainer as I'm fairly new to this.  Plus I've never seen DirectCast.  Can you explain it in more detail please.

    Dat Baker Boy Gumbeaux
  • Re: How to get value of DropDownList inside a GridView

    07-07-2009, 12:33 PM
  • Re: How to get value of DropDownList inside a GridView

    07-07-2009, 1:11 PM
    • Member
      21 point Member
    • gumbeaux70714
    • Member since 07-21-2008, 2:33 PM
    • Baker, LA
    • Posts 73

    I need to get the row on which the button was clicked and be able to find the DropDownList value (cboRank) of the DropDownList on that row.  I can't use the e.CommandArgument becuase I'm sending the collegeid through that. 

    All I need to know is how I can find the value of cboRank on the row that I click so I can get the correct syntax for the dbDataSource.UpdateParameters.Add line below.


    dbDataSource.UpdateCommandType = SqlDataSourceCommandType.Text
    dbDataSource.UpdateCommand = "UPDATE collegefavs SET rank = @ rank " & _
                                                           "WHERE memberid = '" & lblMemberID.Text & "' " & _
                                                           "AND collegeid = '" & e.CommandArgument.ToString & "'"

    dbDataSource.UpdateParameters.Add("rank", GridView2.FindControl("cboRank")???????

    Dat Baker Boy Gumbeaux
  • Re: How to get value of DropDownList inside a GridView

    07-07-2009, 1:14 PM
    • All-Star
      26,894 point All-Star
    • PeteNet
    • Member since 01-21-2009, 1:15 PM
    • Posts 3,834

    ..seems like you're using the RowCommand event, right? write up the code I gave you to get the row in that event.

    If e.CommandName = "UpdateClick" Then

         Dim btn As Button = DirectCast(sender, Button)
         Dim gvr As GridViewRow = DirectCast(btn.NamingContainer, GridViewRow)

         Dim sqlGetInfo As New SqlCommand("SELECT * FROM collegefavs " & _
                                                 "WHERE memberid = '" & lblMemberID.Text & "' " & _            'etc........


         gvr.Cells(index).Text will give you the BoundField values where index is the cell they're in and gvr.FindControl("controlID") will find the a 'templated' control (any inputs such as textbox you define in ItemTemplate)

         DirectCast is used to convert the type. so, for example, to get a textbox you'd do something like this:  Dim txt As TextBox = DirectCast(gvr.FindControl("TextBox1"), TextBox) ...it is good practice to check if the control found is not nothing before you access its properties, like:
         If
    txt IsNot Nothing Then

              'txt.Text     etc
         End
    If

         NamingContainer documentation: http://msdn.microsoft.com/en-us/library/system.web.ui.control.namingcontainer.aspx


    Regards,
    Peter
  • Re: How to get value of DropDownList inside a GridView

    07-07-2009, 1:25 PM
    • Member
      21 point Member
    • gumbeaux70714
    • Member since 07-21-2008, 2:33 PM
    • Baker, LA
    • Posts 73

    Yes, I am using the RowCommand event

    Dat Baker Boy Gumbeaux
  • Re: How to get value of DropDownList inside a GridView

    07-07-2009, 1:29 PM
    • Member
      21 point Member
    • gumbeaux70714
    • Member since 07-21-2008, 2:33 PM
    • Baker, LA
    • Posts 73

    On this line "Dim btn As Button = DirectCast(sender, Button)" I get the following error:

    Unable to cast object of type 'System.Web.UI.WebControls.GridView' to type 'System.Web.UI.WebControls.Button'.

    Dat Baker Boy Gumbeaux
  • Re: How to get value of DropDownList inside a GridView

    07-07-2009, 2:23 PM
    Answer
    • All-Star
      26,894 point All-Star
    • PeteNet
    • Member since 01-21-2009, 1:15 PM
    • Posts 3,834

    try this?

    Dim row As GridViewRow = DirectCast((DirectCast(e.CommandSource, Button).NamingContainer), GridViewRow)

    ...will also give you the row

    does this line work for you: If e.CommandName = "UpdateClick" Then ??


    Regards,
    Peter
  • Re: How to get value of DropDownList inside a GridView

    07-07-2009, 2:47 PM
    • Member
      21 point Member
    • gumbeaux70714
    • Member since 07-21-2008, 2:33 PM
    • Baker, LA
    • Posts 73

    PeteNet, I don't how to thank you.  Everything is working perfectly now.  I updated my code as follows:

    Dim btn As Button = DirectCast(e.CommandSource, Button)
    Dim gvr As GridViewRow = DirectCast(btn.NamingContainer, GridViewRow)


    And as far as getting the value from my DropDownList I used the following:

    Dim cboRank As DropDownList = TryCast(gvr.FindControl("cboRank"), DropDownList)
    dbDataSource.UpdateParameters.Add("rank", Trim(cboRank.Text))


    Thanks again PeteNet, your effort and time is greatly appreciated.

    Dat Baker Boy Gumbeaux
Page 1 of 1 (11 items)