选择Gridview Row - 发送到下一页

时间:2013-09-23 19:08:28

标签: asp.net vb.net gridview

我是这个编程领域的新手(ASP.NET VB),我想知道是否有人为我开始,以便我可以让我的事情在我的最后,我将不胜感激!我目前正在使用代码在Gridview中选择整行(PatientName& Room)而没有“选择”按钮(下图)。然后我想将这些从行传递到下一页。接收页面将有两个标签。这就是我迷失的地方。

我知道有一些例子,但除非有人能指出我正确的方向,否则我找不到符合我要求的例子。谢谢

    Private Sub GridView1_RowCreated(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridView1.RowCreated
    'Allows you to "select"/Highlight a row without a select button

    If e.Row.RowType = DataControlRowType.DataRow Then
        e.Row.Attributes("onmouseover") = "this.style.cursor='pointer';this.style.backgroundColor = '#87CEFF';"
        e.Row.Attributes("onmouseout") = "this.style.textDecoration='none';this.style.backgroundColor = '#FFFFFF';"
        e.Row.ToolTip = "Click to select row"
        e.Row.Attributes("onclick") = Me.Page.ClientScript.GetPostBackClientHyperlink(Me.GridView1, "Select$" & e.Row.RowIndex)
    End If

End Sub

2 个答案:

答案 0 :(得分:0)

我强烈建议包括一个选择按钮。它触发的选择事件将为您提供查找要在下一页显示的数据所需的所有信息。

至于数据的传递,QueryString是最好的。 Session如果您不想在URL中提供它(并且不想加密或以其他方式混淆),那么我将是我的第二选择。

答案 1 :(得分:0)

您可以通过几种方式实现这一目标。这只是获得所需内容的一种方式。假设您有以下数据结构。

Public Class Patient
    Private patient_Id As Integer
    Public ReadOnly Property PatientID As Integer
        Get
            Return patient_Id
        End Get
    End Property
    Public Property PatientName As String
    Public Property Room As Integer
    Public Sub New(_patientId As Integer, ByVal _PatientName As String, ByVal _Room As Integer)
        patient_Id = _patientId
        PatientName = _PatientName
        Room = _Room
    End Sub
End Class

我们需要PatientID从列表,数组等中轻松找到患者。在GridView中,您可以添加HiddenField患者的ID,如下所示:

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False">
    <Columns>
        <asp:TemplateField HeaderText="Patient Name">
            <ItemTemplate>
                <asp:HiddenField runat="server" ID="hiddenPatientID" Value='<%# Eval("PatientID")%>' />
                <asp:Label ID="lblPatientName" runat="server" Text='<%# Eval("PatientName")%>'></asp:Label>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Room">
            <ItemTemplate>
                <asp:Label ID="lblPatientRoom" runat="server" Text='<%# Eval("Room")%>'></asp:Label>
            </ItemTemplate>
        </asp:TemplateField>
    </Columns>
</asp:GridView>

这将使您的患者的ID可供您使用。您也可以从您的病人(PatientName,PatientRoom)访问其他信息,我为了这个例子使用PatientID。从后面的代码访问此数据的方式是通过实现RowDataBound控件的GridView事件,如下所示:

Protected Sub GridView1_RowDataBound(sender As Object, e As GridViewRowEventArgs) Handles GridView1.RowDataBound
    If e.Row.RowType = DataControlRowType.DataRow Then
        Dim selectedPatient As Integer = -16

        selectedPatient = DirectCast(e.Row.Cells(0).FindControl("hiddenPatientID"), HiddenField).Value

        If selectedPatient > 0 Then
            e.Row.Attributes("onclick") = Response.Redirect("~/MyOtherPage.aspx?&PatientID=" & selectedPatient)
        End If
    End If
End Sub

简单来说,通过使用行的FindControl功能,您可以访问HiddenField的数据,在这种情况下,您的患者是id。您可以在下一页中使用此ID来获取患者(getPatientById方法?),或者您可以直接使用患者的数据。

相关问题