主从报表:标签与网格的使用;窗体身份验证






3.64/5 (8投票s)
2005年3月28日
5分钟阅读

59070

1195
一篇关于创建 ASP.NET 页面的入门指南,该页面使用窗体身份验证提供的 User.Identity.Name,允许客户/员工登录、检索他们的特定记录,并将信息格式化为主从记录。
引言
客户/员工信息中心为个人提供了在线查看其记录的机会,以查询订单状态、发票、账单和其他个人信息。在每种情况下,后台运行的 SQL 都通过匹配其登录名与数据库中存储的登录名来检索客户/员工记录。
在我们的示例中,我们使用了自我信息中心的概念,通过结合使用标签和 DataGrid
来演示如何在 ASP.NET 中创建更自然的业务视图。典型的教科书示例使用下拉框来选择正确的报表/表单,然后提供详细信息在一个 DataGrid
中。结果的表单很笨拙,并不总是反映用户在商业表单中看到的订单、销售等信息。
有许多不同的方法可以处理此类应用程序中的数据。我选择使用 DataSet
/缓存的 DataView
来演示行索引的使用。在我们的示例中,SQL 只返回一行 - 索引 0 - 与员工或客户登录名匹配的那一行。我们也可以返回多行,然后通过一个按钮来浏览这些行(每行代表一个员工/客户),该按钮使行索引增加 +1。各种状态方法将保留从一次点击到另一次点击的当前行索引。
我们通过捕获使用窗体身份验证的用户登录名来自动化记录检索过程。在此示例中,我们将 User.Identity.Name
中的值与存储在客户/员工记录中的登录名列进行匹配。这使得系统能够仅返回属于登录人员的记录。
该示例使用标签显示员工的个人信息(主信息),并使用 DataGrid
显示其销售(详细信息)。员工可以浏览他们的销售记录,按客户对销售记录进行排序等。如前所述,屏幕也可以用作模板,通过简单的修改允许主管查看员工的销售情况。
解决方案概览
该解决方案需要三个基本步骤
- 员工使用窗体身份验证登录系统,登录名存储在
User.Identity.Name
中。 - 主从报表的主部分是通过以下方式创建的
- 将
User.Identity.Name
与存储在员工/客户表中的值进行匹配。 - 检索信息并将其放入缓存的
DataView
中。 - 使用标签显示数据。
- 将
- 通过自定义查询(仅为好玩)检索报表的详细部分,并将其呈现在
DataGrid
中。除了使用自定义 SQL 语句外,还有许多其他方法可以检索数据,包括创建适当的视图、存储过程等。
代码准备步骤
演示中使用的 Northwind Employees 表必须进行修改,添加一个列来存储登录名。在此示例中,我们使用了名称 u_UserName
,类型为 varchar (30)
。
在演示中,为了保持简单,我们没有添加任何错误检查...
Using the Code
此时,我们可以将注意力集中在创建主从报表的 ASP.NET 代码上。已包含注释来解释代码
' Add the appropriate Namespaces for our project
<%@ Page Language="VB" %>
<%@ import Namespace="System.Data" %>
<%@ import Namespace="System.Data.SqlClient" %>
<script runat="server">
'--------------------------------------------------------------
' Create & manage the cache that handles the Master section
'--------------------------------------------------------------
'---------------------------------------------------------
'con for Connection 'dad for DataAdapter 'dst for DataSet
'dvw for DataView 'cmd for command 'lbl for label
'---------------------------------------------------------
Dim MyCmd as String
Sub Page_Load ( s As Object, e As EventArgs )
If Not Page.IsPostBack Then
BindGrid( )
End If
' Create our Variables
Dim conNorthwind As SqlConnection
Dim dadEmployees As SqlDataAdapter
Dim dstEmployees As DataSet
Dim dvwEmployees As DataView
' Set up the DataView cache
dvwEmployees = Cache( "Employees" )
If dvwEmployees Is Nothing Then
conNorthwind = New SqlConnection( "server=(local);_
database=Northwind;trusted_connection=true" )
dadEmployees = New SqlDataAdapter( "Select * From Employees _
where u_UserName = '" & User.Identity.Name & "' ", conNorthwind )
dstEmployees = New DataSet()
dadEmployees.Fill( dstEmployees, "Employees" )
dvwEmployees = dstEmployees.Tables( "Employees" ).DefaultView()
Cache( "Employees" ) = dvwEmployees
End If
' Display the information using labels
lblEmpID.Text = dvwEmployees( 0 ).Row( "EmployeeID" )
lblFirstName.Text = dvwEmployees( 0 ).Row( "FirstName" )
lblLastName.Text = dvwEmployees( 0 ).Row( "LastName" )
lblPhone.Text = dvwEmployees( 0 ).Row( "HomePhone" )
lblEmail.Text = dvwEmployees( 0 ).Row( "EmailAddress" )
lblNotes.Text = dvwEmployees( 0 ).Row( "Notes" )
End Sub
步骤 1
我们创建一个指定所需数据库的连接对象。DataAdapter
指定要执行的工作,即使用连接对象执行我们的 SQL 语句。
名称 conNorthwind
指的是连接对象,并使用受信任的连接设置为 Northwind 数据库。在连接字符串 "server=(local);database=Northwind..." 中,您需要将 local 替换为您的服务器名称,将 Northwind 替换为您的数据所在数据库的名称。
第二步
我们指定 DataSet
,用请求的数据填充它,命名 DataView
并缓存检索到的行。
步骤 3
通过选择索引为 0 的行来显示数据。由于只检索一行,我们不指定循环机制来遍历行。同样,我们也不指定任何错误处理,例如行边界检查,但在生产环境中肯定会添加这些。
然后 ...
'--------------------------------------------------------------
' Begin the Datagrid section that will handle Report Details
'--------------------------------------------------------------
' Set up Page Index functions
'--------------------------------------------------------------
Sub DataGrid_SetPage(Sender As Object, e As DataGridPageChangedEventArgs)
DataGrid1.CurrentPageIndex = e.NewPageIndex
BindGrid()
End Sub
Sub DataGrid_IndexChanged(sender As Object, e As EventArgs)
End Sub
' Set up application to handle sorting
'---------------------------------------------------------------
Property Sort_Field() As String
Get
Dim obj As Object = ViewState("Sort_Field")
If obj Is Nothing Then
Return String.Empty
End If
Return CStr(obj)
End Get
Set(ByVal Value As String)
ViewState("Sort_Field") = Value
End Set
End Property
Sub DataGrid_Sort(Sender As Object, e As DataGridSortCommandEventArgs)
DataGrid1.CurrentPageIndex = 0
Sort_Field = e.SortExpression
BindGrid()
End Sub
'---------------------------------------------------------------
' Bind the Datagrid
'---------------------------------------------------------------
Sub BindGrid()
' Create a custom SQL statement to get the data
Dim CmdText As String
If Sort_Field = String.Empty Then
CmdText = "select * from Northwind.dbo.Orders" & _
" where exists (Select * From Employees where " & _
"Employees.EmployeeID = Orders.EmployeeID and " & _
"Employees.u_UserName = '" & User.Identity.Name & _
"' ) order by CustomerID"
MyCmd = CmdText
Else
CmdText = "select * from Northwind.dbo.Orders" & _
" where exists (Select * From Employees where " & _
"Employees.EmployeeID = Orders.EmployeeID and " & _
"Employees.u_UserName = '" & User.Identity.Name & _
"' ) order by " & Sort_Field
MyCmd = CmdText
End If
'Set the SQLDataAdapter to connect to the Northwind Order table
' & use our custom SQL Statement
Dim conNorthwind2 As New SqlConnection("server=(local);" & _
"database=Northwind;trusted_connection=true")
Dim dadOrders As New SqlDataAdapter(MyCmd, conNorthwind2)
'Create dstOrders (the Order Dataset), fill it, and bind it.
Dim dstOrders As New DataSet()
dadOrders.Fill(dstOrders)
DataGrid1.DataSource = dstOrders
DataGrid1.DataBind()
End Sub
'-----------------------------------------------------------------------
'End of Script, Begin HTML Section Below
'-----------------------------------------------------------------------
如何使用
源代码 ZIP 文件包含一个 ASP.NET 页面,该页面已在我们的服务器上预先测试过... 然而,在使用该代码之前,必须进行一些更改
- 修改 Employees 表以添加
u_UserName
列。 - 设置您自己的登录页面,或使用前面确定的那个。
- Sales-Report.aspx 页面应放置在您的 Web 目录中。
- 一个名为 shim.gif 的间隔 GIF(包含在 ZIP 文件中)应放置在 images 子目录中。
结论
该应用程序使用户能够登录,并查看其个人信息以及他们所承担的订单的主从报表。主级别数据放置在标签中以创建自然外观的表单,详细级别数据放置在 DataGrid
中。
此示例仅用于说明目的... 从 SQL Server 数据库检索数据有许多方法。将数据下载到客户端允许许多员工在不维护活动游标的情况下处理其销售。
我们试图在概述和构建此站点所需的详细信息之间取得平衡。根据经验,我们可能对某些人来说过于详细,而对另一些人来说则不够。