如何通过单击按钮获取网格视图内的下拉列表的值?

发布于 2024-12-03 23:58:30 字数 1089 浏览 1 评论 0原文

我在网格视图中有一个下拉列表。现在我希望当我单击按钮时我可以检查下拉列表的值。我已经为其触发了 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"  />

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

旧人九事 2024-12-10 23:58:30

试试这个

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

try this

DropDownList ddl = (DropDownList)GridView1.Rows[e.RowIndex].Cells[0].FindControl("drp_UnAssignProp");
string val = ddl.SelectedValue;
陈独秀 2024-12-10 23:58:30

尝试

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

try

  string val = (DropDownList)GridView1.Rows[e.RowIndex].Cells[0]
               .FindControl("drp_UnAssignProp").SelectedValue;
与风相奔跑 2024-12-10 23:58:30

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

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文