开发者

how to get value of dropdownlist which is inside a gridview on the click of a button?

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:ListIt开发者_StackOverflow中文版em>
                            </asp:DropDownList>
                        </ItemTemplate>
                    </asp:TemplateField>
                </Columns></asp:GridView><tr><td><asp:Button ID="btn_Save" runat="server" CommandName="Select" Text="Submit"  />


try this

DropDownList ddl = (DropDownList)GridView1.Rows[e.RowIndex].Cells[0].FindControl("drp_UnAssignProp");
string val = ddl.SelectedValue;


try

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


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
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜