像HyperLinkField一样在网格中创建绑定字段

时间:2016-11-25 14:51:59

标签: html vb.net

我有一个嵌套的gridview,如下所示。我从DAL / BLL层中的存储过程和方法填充gridview。这很好,数据会像我期望的那样返回到嵌套的gridview。

我想做的是将嵌套网格视图中的boundfield(VisitTypeId)作为超链接,因此用户可以点击它并重定向到患者的相关“访问”(换句话说) ,'其中ID = {0}和VisitTypeId = {0}')。

我确信这一定是可能的,但无法找到办法。有人可以帮忙吗?

标记:

<asp:GridView ID="gvPatients" runat="server" AutoGenerateColumns="False" DataSourceID="odsPatients" OnRowDataBound="gvVisits_RowDataBound"  
    DataKeyNames="ID" AllowPaging="True" CssClass="interactTable" CellPadding="4" 
    AllowSorting="True" meta:resourcekey="gvPersonsResource1">
     <HeaderStyle CssClass="header" />
        <RowStyle CssClass="row"/>
        <Columns>
         <asp:TemplateField Visible="False">
            <ItemTemplate>
            <asp:Label ID="lblID" runat="server" Text='<%# Eval("ID")%>'></asp:Label>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:Boundfield DataField="ID" HeaderText="ID" />
        <asp:Boundfield DataField="Forename" HeaderText="Forename" />
        <asp:Boundfield DataField="Surname" HeaderText="Surname" />
        <asp:Boundfield DataField="DoB" HeaderText="DoB" DataFormatString="{0:dd/MM/yyyy}" />
        <asp:TemplateField HeaderText="Visits">
            <ItemTemplate>
                <asp:GridView ID="gvVisits" runat="server" AutoGenerateColumns="false" BorderWidth="0" ShowHeader="false" >
                    <Columns>
                    <%-- <asp:BoundField DataField="PatientID" Visible="false" /> --%>
                    <asp:BoundField DataField="VisitTypeID" />
                    </Columns>
                </asp:GridView>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:HyperLinkField DataNavigateUrlFields="ID" DataNavigateUrlFormatString="~/AddEditPatient.aspx?ID={0}" HeaderText="Edit" Text="Edit"  />
        </Columns>
    </asp:GridView>

DAL:

Public Shared Function GetVisitType(PatientId As Integer) As DataSet
Using myConnection As New SqlConnection(AppConfiguration.ConnectionString)
        Using myCommand As New SqlCommand("SPGetVisitType", myConnection)
            myCommand.CommandType = CommandType.StoredProcedure
            myCommand.Parameters.AddWithValue("PatientID", PatientId)

            myConnection.Open()
            Dim ds As New DataSet()
            Using myAdapter As New SqlDataAdapter(myCommand)
                myAdapter.Fill(ds)
                Return ds
            End Using
            myConnection.Close()
        End Using
    End Using

End Function

BLL:

Public Shared Function GetVisitType(ByVal PatientId As Integer) As DataSet
Dim ds As DataSet = ScreeningTestDAL.Visit.GetVisitType(PatientId)
Return ds
End Function

代码隐藏:

    Protected Sub gvVisits_RowDataBound(ByVal sender As Object, ByVal e As GridViewRowEventArgs)

    If e.Row.RowType = DataControlRowType.DataRow Then
        Dim gv As GridView = DirectCast(e.Row.FindControl("gvVisits"), GridView)
        Dim PatientId As Integer = Convert.ToInt32(e.Row.Cells(1).Text)
        Dim ds1 As DataSet = ScreeningTestBLL.VisitManager.GetVisitType(PatientId)
        gv.DataSource = ds1
        gv.DataBind()
    End If

End Sub

0 个答案:

没有答案