1

我在gridview中有一个下拉列表。现在我想当我点击一个按钮然后我可以检查下拉列表的值。我已经为它触发了gridview的rowcommand事件,但是调试器无法到达那里..请帮助我..我的代码是

  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

我的源代码是

   <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"  />
4

3 回答 3

1

试试这个

DropDownList ddl = (DropDownList)GridView1.Rows[e.RowIndex].Cells[0].FindControl("drp_UnAssignProp");
string val = ddl.SelectedValue;
于 2011-09-12T06:14:10.400 回答
1

尝试

  string val = (DropDownList)GridView1.Rows[e.RowIndex].Cells[0]
               .FindControl("drp_UnAssignProp").SelectedValue;
于 2014-05-09T15:43:50.943 回答
0

首先,由于按钮btn_Save不在 内部GridView,单击它不会提升grd_Test_RowCommand,要么将按钮移动到内部,要么GridView您必须像这样手动提升它:

从asp.net论坛复制:

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

现在关于您最初的问题,这是您如何获得DropDownList内部RowCommand事件的选定值:

编辑:修复了获取当前行的代码,为此引发了 RowCommand 事件

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
于 2011-09-12T06:21:47.510 回答