1

I have a dropdownlist inside the gridview. Now i want when i click on a button then i can check the value of dropdownlist. I have fired rowcommand event of gridview for it but debugger is not able to go reach there..Please help me..My Code is

  Protected Sub grd_Test_RowCommand(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewCommandEventArgs) Handles grd_Test.RowCommand
    If e.CommandName = "Select" Then


     End If
End Sub

my Source code is

   <asp:GridView ID="grd_UnAssignProperties" runat="server" AutoGenerateColumns="False"><Columns>
                    <asp:TemplateField HeaderText="Assign To">
                        <ItemTemplate>
                            <asp:DropDownList ID="drp_UnAssignProp" runat="server">

                            <asp:ListItem  Value="" >Default</asp:ListItem>
                            </asp:DropDownList>
                        </ItemTemplate>
                    </asp:TemplateField>
                </Columns></asp:GridView><tr><td><asp:Button ID="btn_Save" runat="server" CommandName="Select" Text="Submit"  />
Ram Singh
  • 6,664
  • 35
  • 100
  • 166

3 Answers3

1

try

  string val = (DropDownList)GridView1.Rows[e.RowIndex].Cells[0]
               .FindControl("drp_UnAssignProp").SelectedValue;
Bellash
  • 21
  • 2
1

try this

DropDownList ddl = (DropDownList)GridView1.Rows[e.RowIndex].Cells[0].FindControl("drp_UnAssignProp");
string val = ddl.SelectedValue;
i100
  • 4,529
  • 1
  • 22
  • 20
  • sir this will be the next step but still my rowcommand is also not firing..can u tell me y? i have wrote all my code please if u identify the error please tell me. – Ram Singh Sep 12 '11 at 06:16
0

First of all, since the button btn_Save isn't inside the GridView, clicking on it won't raise the grd_Test_RowCommand, either move the button inside GridView or you have to raise it manually like this:

Copied from asp.net forum:

Protected Sub Button1_Click(sender As Object, e As EventArgs)
    Dim commandArgs As New CommandEventArgs("Command Name Here", "Your Command Argument Here")
    'You can pass any row
    'You can also skip the row parameter and get the row from Command Argument
    Dim eventArgs As New GridViewCommandEventArgs(GridView1.Rows(0), GridView1, commandArgs)
    GridView1_RowCommand(GridView1, eventArgs)
End Sub

Now regarding your original question, this is how you can get the selected value of DropDownList inside RowCommand event:

Edit: Fixed code to get the current row, for which the RowCommand event was raised

Protected Sub grd_Test_RowCommand(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewCommandEventArgs) Handles grd_Test.RowCommand
    If e.CommandName = "Select" Then
        Dim index As Integer = Convert.ToInt32(e.CommandArgument)
        Dim row As GridViewRow = DirectCast(DirectCast(e.CommandSource, LinkButton).NamingContainer, GridViewRow)
        Dim ddl as DropDownList = CType(row.FindControl("drp_UnAssignProp", DropDownList)
        Dim selectedValue as String = ddl.Text
     End If
End Sub
Waqas
  • 6,812
  • 2
  • 33
  • 50
  • its much closer to what i want. But there is a error that is "Unable to cast object of type 'System.EventArgs' to type System.IConvertible". At ` If e.CommandName = "Select" Then Dim index As Integer = Convert.ToInt32(e.CommandArgument)` – Ram Singh Sep 12 '11 at 06:49
  • you can remove this line: Dim index As Integer = Convert.ToInt32(e.CommandArgument) its of no use and i forgot to remove. Check and let me know – Waqas Sep 12 '11 at 06:51
  • sir after removing this line there is an error that is "Unable to cast object of type 'System.Web.UI.WebControls.GridView' to type 'System.Web.UI.WebControls.LinkButton'." – Ram Singh Sep 12 '11 at 06:56
  • i see what the problem is, replace all lines above Dim ddl as .... inside Row_CommandEvent to Dim btn As Button = CType(e.CommandSource, Button) Dim row As GridViewRow = CType(btn.NamingContainer, GridViewRow) - also note that it will only work for button inside gridview, do let me know if you have button outside gridview – Waqas Sep 12 '11 at 07:09