0
votes

I'm new to this area of programming (ASP.NET VB) and i'm wondering if anyone has a kick start for me so that I can get things moving on my end which I would greatly appreciate! I'm currently using code to select an entire row (PatientName & Room ) in a Gridview without a "select" button (Below). I would then like to pass these from the row to the next page. The receiving page would have the two in labels. This is where i'm lost.

I know there are examples out there but i can not find an example that fits my case unless someone can point me in the right direction. Thank you

    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

2 Answers

0
votes

I would highly suggest including a Select button. The Select Event that it fires would give you all the information you need to find the data you want to display on the next page.

As for the passing of the data, QueryString is the best. Session would be my second choice if you don't want to have it available in the URL (and don't want to encrypt or otherwise obfuscate it).

0
votes

You can accomplish this in a couple of ways. This is just one way of getting what you need. Say you have the following Data structure.

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

We need a PatientID to easily find a Patient from a list, an array, etc. In your GridView you can add a HiddenField with the ID of your patients, like this:

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

That will make the ID of your patient available for your use. You may also access the other information from your patien (PatientName, PatientRoom), I am using PatientID for the sake of this example. The way you access this data from code behind, is by implemmenting the RowDataBound event of your GridView control, like this:

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

Simple, by using the row's FindControl function, you can access the HiddenField's data, which is in this case your patient's id. You can use this ID in the next page to get to the patient (getPatientById method?) or you could just use the patient's data directly.