【问题标题】:Get Id of a row from an image button in a gridview in vb.net从vb.net的gridview中的图像按钮获取行的ID
【发布时间】:2009-05-08 15:04:28
【问题描述】:

我有一个网格视图,在该网格视图中,我使用 addhandler 以编程方式创建了一个图像按钮列表,如下所示:

Dim deletecshr As New ImageButton
deletecshr.ImageUrl = "\images\bttnDeletemini.gif"
deletecshr.ToolTip = "This Will Delete All Cashiers"
AddHandler deletecshr.Click, AddressOf deletecshr_Click
deletecshr.Attributes.Add("onclick", "javascript: if(confirm('Are you sure you want to delete all of these cashiers?')==false) return false;")

    If e.Row.Cells.Count > 2 And e.Row.RowType <> DataControlRowType.Header And e.Row.RowType <> DataControlRowType.Footer Then
        e.Row.Cells(3).Controls.Add(deletecshr)
    End If

我的问题是在单击处理程序中获取单击的图像按钮行的值。我怎么得到它?

【问题讨论】:

    标签: vb.net gridview imagebutton


    【解决方案1】:

    我建议分配

    imageButton.CommandName = "Delete"
    imageButton.CommandArgument = Your_Row_ID_You_Want_To_Get
    

    然后使用 imageButton.OnCommand 完成您的工作。

    编辑: 以下是要遵循的步骤:

    1. 在 RowCreated 中创建图像按钮
    2. 在 RowDataBound 中指定图像按钮的 CommandName 和 CommandArgument
    3. 在 RowCommand 中,检查该行的图像按钮 CommandArgument,您应该会看到之前存储在那里的 ID。

    【讨论】:

    • 我应该更具体。我查看了示例,但我无法获得 ID,因为该按钮不在我的源代码中,因为按钮是在 Gridview_rowcreated 上以编程方式生成的。这就是我必须创建和 Addhandler 的原因感谢您的快速响应。
    • 尝试使用 GridView RowDataBound 事件为按钮创建与行 ID 的绑定,因为您将无法在 RowCreated 中访问该信息。
    • 亚当做得很好。我无法使用我创建的 addhandler,但我可以使用 Gridview_RowCommand 来编写我的命令。但是我如何获取选择删除数据库中该记录的行的 ID。我已经尝试过 Gridview.SelectedDataKey 并设置了 datakeynames 并且我已经尝试过 Gridview.SelectedValue ......都没有奏效。再次感谢。
    • 亚当,感谢您的大力帮助。我试图为你投票,但我没有足够高的声誉。你的步骤对我有帮助。
    • 不用担心。很高兴有帮助。祝项目顺利!
    猜你喜欢
    • 1970-01-01
    • 2013-12-03
    • 1970-01-01
    • 1970-01-01
    • 2020-09-20
    • 2021-07-11
    • 1970-01-01
    • 2017-03-31
    • 1970-01-01
    相关资源
    最近更新 更多