【问题标题】:how to get value of dropdownlist which is inside a gridview on the click of a button?如何通过单击按钮获取网格视图内的下拉列表的值?
【发布时间】:2011-09-12 06:09:59
【问题描述】:

我在 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"  />

【问题讨论】:

    标签: asp.net gridview rowcommand


    【解决方案1】:

    试试这个

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

    【讨论】:

    • 先生,这将是下一步,但我的行命令仍然没有触发..你能告诉我吗?我已经写了我所有的代码,如果你发现错误请告诉我。
    【解决方案2】:

    试试

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

    【讨论】:

      【解决方案3】:

      首先,由于按钮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
      

      现在关于您的原始问题,这是您如何在 RowCommand 事件中获取 DropDownList 的选定值:

      编辑:修复代码以获取引发 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
      

      【讨论】:

      • 它更接近我想要的。但是有一个错误是“无法将'System.EventArgs'类型的对象转换为System.IConvertible类型”。 At ` If e.CommandName = "Select" Then Dim index As Integer = Convert.ToInt32(e.CommandArgument)`
      • 你可以删除这一行: Dim index As Integer = Convert.ToInt32(e.CommandArgument) 它没有用,我忘了删除。检查并告诉我
      • 先生,删除此行后出现错误“无法将'System.Web.UI.WebControls.GridView'类型的对象转换为'System.Web.UI.WebControls.LinkBut​​ton' 。”
      • 我知道问题出在哪里,将 Dim ddl 上方的所有行替换为 .... 内部 Row_CommandEvent 到 Dim btn As Button = CType(e.CommandSource, Button) Dim row As GridViewRow = CType(btn .NamingContainer, GridViewRow) - 另请注意,它仅适用于 gridview 内的按钮,如果您在 gridview 外有按钮,请告诉我
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-01-05
      • 1970-01-01
      • 2021-12-15
      • 2013-06-20
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多