可点击的gridview

时间:2012-08-07 16:01:48

标签: gridview

    protected void GridViewAddress_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        // Make sure the current GridViewRow is a data row.
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            // Make sure the current GridViewRow is either 
            // in the normal state or an alternate row.
            if (e.Row.RowState == DataControlRowState.Normal || e.Row.RowState == DataControlRowState.Alternate)
            {
                // Add client-side confirmation when deleting.
                ((LinkButton)e.Row.Cells[1].Controls[0]).Attributes["onclick"] = "if(!confirm('Are you certain you want to delete this person ?')) return false;";
            }
        }
    }



    }
}

我的问题是,我想让我的网格视图可点击,并希望将用户引导到另一个页面。这是我必须捕获事件并将用户重定向到另一个的代码

1 个答案:

答案 0 :(得分:0)

您需要创建一个可单击行的GridView。我为您提供了链接以及链接中的代码。如果您不熟悉VB,则代码在VB中,有用户在评论部分将其转换为C#。

Imports Microsoft.VisualBasic
Imports System.ComponentModel
Imports System.Web.UI
Imports System.Web.UI.WebControls

Namespace SampleControls


    Public Class MyGridView
        Inherits System.Web.UI.WebControls.GridView

#Region "Properties"

        <DefaultValue(False)> _
        Public Property EnableRowClick() As Boolean
            Get
                Dim ret As Boolean = False
                Dim obj As Object = ViewState("EnableRowClick")
                If obj IsNot Nothing Then
                    ret = CBool(obj)
                End If
                Return ret
            End Get
            Set(ByVal value As Boolean)
                ViewState("EnableRowClick") = value
            End Set
        End Property
#End Region

#Region "Event"


        Private Shared ReadOnly RowClickedEventKey As Object = New Object

        Public Custom Event RowClicked As EventHandler(Of GridViewRowClickedEventArgs) AddHandler(ByVal value As EventHandler(Of GridViewRowClickedEventArgs))
                Events.AddHandler(RowClickedEventKey, value)
            End AddHandler

            RemoveHandler(ByVal value As EventHandler(Of GridViewRowClickedEventArgs))
                Events.RemoveHandler(RowClickedEventKey, value)
            End RemoveHandler

            RaiseEvent(ByVal sender As Object, ByVal e As GridViewRowClickedEventArgs)
                Dim ev As EventHandler(Of GridViewRowClickedEventArgs) = TryCast(Events(RowClickedEventKey), EventHandler(Of GridViewRowClickedEventArgs))
                If ev IsNot Nothing Then
                    ev(sender, e)
                End If
            End RaiseEvent
        End Event

        Protected Overridable Sub OnRowClicked(ByVal e As GridViewRowClickedEventArgs)
            RaiseEvent RowClicked(Me, e)
        End Sub

#End Region

#Region "Postback handling"
        Protected Overrides Sub RaisePostBackEvent(ByVal eventArgument As String)
            If eventArgument.StartsWith("rc") Then
                Dim index As Integer = Int32.Parse(eventArgument.Substring(2))
                Dim args As New GridViewRowClickedEventArgs(Me.Rows(index))
                OnRowClicked(args)
            Else
                MyBase.RaisePostBackEvent(eventArgument)
            End If

        End Sub
#End Region

#Region "Adding the wiring from client-side to server-side, causing the posback when row is clicked"


        Protected Overrides Sub PrepareControlHierarchy()
            MyBase.PrepareControlHierarchy()

            If EnableRowClick Then
                Dim i As Integer
                For i = 0 To Rows.Count - 1
                    Dim argsData As String = "rc" & Rows(i).RowIndex.ToString()
                    Me.Rows(i).Attributes.Add("onclick", Page.ClientScript.GetPostBackEventReference(Me, argsData))
                Next
            End If

        End Sub

#End Region

    End Class

#Region "Custom event argument type"
    Public Class GridViewRowClickedEventArgs
        Inherits EventArgs

        Private _row As GridViewRow
        Public Sub New(ByVal row As GridViewRow)
            _row = row
        End Sub
        Public ReadOnly Property Row() As GridViewRow
            Get
                Return _row
            End Get
        End Property
    End Class
#End Region

End Namespace

Usage example

[aspx]

<%@ Register Namespace="SampleControls" TagPrefix="sc" %>
...
<sc:MyGridView ID="myGridView1" runat="server" AutoGenerateColumns=false EnableRowClick=true>
            <Columns>
                <asp:TemplateField HeaderText="Sample row header">
                    <ItemTemplate>
                    Sample row data
                    </ItemTemplate>
                </asp:TemplateField>
            </Columns> 
 </sc:MyGridView>[code-behind]

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not Page.IsPostBack Then
            Dim arr As New ArrayList()
            arr.Add(1)
            arr.Add(2)
            arr.Add(3)
            myGridView1.DataSource = arr
            myGridView1.DataBind()
        End If
    End Sub   Protected Sub myGridView1_RowClicked(ByVal sender As Object, ByVal e As SampleControls.GridViewRowClickedEventArgs) Handles myGridView1.RowClicked   Response.Write("Index of the clicked row was: " + e.Row.RowIndex.ToString()) 

End Sub

LINK:

http://aspadvice.com/blogs/joteke/archive/2006/01/07/14576.aspx#17887