更新超链接在Gridview中无法正常工作

时间:2014-04-17 02:38:33

标签: asp.net vb.net gridview

当用户点击链接时,

Gridview无效。用户应该在点击链接时被定向到另一个页面,在第二页上,有一个允许用户更新的文本框。

这是我的网格视图代码:

 <asp:BoundField DataField = "status" HeaderText = "Status"  HtmlEncode = "true"  ItemStyle-Width="150px" > 
       <ItemStyle Width="50px" />
 </asp:BoundField>
 <asp:ButtonField ButtonType="Link" Text="Click"  CommandName="Select"  HeaderText="Details"   />     
 <asp:ButtonField HeaderText="Update" Text="update?" CommandName="GridView1_RowUpdated" />
代码背后的代码:

 Protected Sub GridView1_RowUpdated(sender As Object, e As GridViewUpdatedEventArgs)
    Dim cr_number As String = GridView1.SelectedRow.Cells(0).Text
    Response.Redirect("upddetail.aspx?id=" + cr_number)
End Sub

2 个答案:

答案 0 :(得分:0)

您使用的是ButtonField而不是实际的超链接。当用户点击此页面时,将会回发到原始页面,并且不会导航到其他页面。尝试使用HyperLink

答案 1 :(得分:0)

由于您将CommandName设置为GridView1_RowUpdated,因此可以使用GridView1_RowCommand事件,如下所示

  Sub GridView1_RowCommand(ByVal sender As Object, ByVal e As GridViewCommandEventArgs)

    If e.CommandName = "GridView1_RowUpdated" Then
       Dim index As Integer = Convert.ToInt32(e.CommandArgument)
      Dim cr_number As String = GridView1.Rows(index).Cells(0).Text
      Response.Redirect("upddetail.aspx?id=" + cr_number)
    End If

  End Sub

在您的aspx页面集onrowcommand中,如下所示

 <asp:gridview id="GridView" onrowcommand="GridView1_RowCommand"

而不是以上所有内容,您可以使用hyperlinkfield

<asp:hyperlinkfield text="Update?"
            datanavigateurlfields="Id"
            datanavigateurlformatstring="~\upddetail.aspx?id={0}"          
            headertext="Update"
            target="_blank" />