自定义 GridView





1.00/5 (3投票s)
通过文件或数据库配置的 Gridview
引言
这个项目是一个使用 GridView 的用户控件,可以通过数据库或文件进行配置。
基本上,用户控件在容器页面(这里是 Default.aspx)发送两个数据集之前,对数据一无所知。
该用户控件包含一个 GridView、一个文本框和两个按钮。
描述
用户控件 "ucCustomDataGrid.ascx" 的 HTML 部分如下所示
<%@ Control Language="VB" AutoEventWireup="false" CodeFile="ucCustomDataGrid.ascx.vb"
Inherits="ucCustomDataGrid" %> <asp:GridView ID="grdvwGeneric" runat="server">
<table>
<tbody>
<tr>
<td colspan="1"><asp:GridView ID="grdvwGeneric" runat="server">
<asp:TextBox ID="TextBox1" runat="server"></td>
</tr>
<tr id="trButtonRow" runat="server"/>
<tr>
<td colspan="1"><asp:Button ID="btnDelete" runat="server" Text="Delete" /></td>
<td colspan="1"><asp:Button ID="btnSubmit" runat="server" Text="Submit" /></td>
</tr>
</tbody>
</table>
用户控件 VB 代码:ucCustomDataGrid.ascx.vb
GridView 所需的一切都必须在用户控件的属性中定义。
以下代码显示了对用户控件最重要的两个数据集。
Private dsCustom As DataSet
Private dsCONFIG As DataSet
Public WriteOnly Property SetDataSource() As DataSet
Set(ByVal value As DataSet)
dsCustom = value
End Set
End Property
Public WriteOnly Property SetConfigSource() As DataSet
Set(ByVal value As DataSet)
dsCONFIG = value
End Set
End Property
此外,在用户控件的 Page Load 部分,将使用以下数据集。
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)
Handles Me.Load
If Not Page.IsPostBack Then
ViewState("DataSet") = dsCustom
ViewState("ConfigSet") = dsCONFIG
Else
dsCustom = ViewState("DataSet")
dsCONFIG = ViewState("ConfigSet")
End If
BindDataGrid()
End Sub
另一个值得一提的重要函数是 RowBound,它将动态配置行和列。
Protected Sub grdvwGeneric_RowDataBound(ByVal sender As Object,
ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs)
Handles grdvwGeneric.RowDataBound
If e.Row.RowType = DataControlRowType.DataRow Then
GetCustomDataGridColumn(e)
End If
End Sub
Private Sub GetCustomDataGridColumn(ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs)
//Read from the Configuration DataSet
Dim btnCustom As New Control
For iType As Integer = 0 To dsCONFIG.Tables(0).Rows.Count - 1
If dsCONFIG.Tables(0).Rows(iType)("ColumnType") = "LinkButton" Then
Dim btn As New LinkButton
btn.Text = e.Row.Cells(0).Text
btn.CommandName = e.Row.Cells(1).Text
btn.CommandArgument = e.Row.Cells(1).Text
AddHandler btn.Command, AddressOf Checkin_Command
btnCustom = btn
ElseIf dsCONFIG.Tables(0).Rows(iType)("ColumnType") = "Button" Then
Dim btn1 As New Button
btn1.Text = e.Row.Cells(1).Text
btn1.CommandName = e.Row.Cells(1).Text
btn1.CommandArgument = e.Row.Cells(1).Text
AddHandler btn1.Command, AddressOf Checkin_Command
btnCustom = btn1
ElseIf dsCONFIG.Tables(0).Rows(iType)("ColumnType") = "DropDownList" Then
Dim btn2 As New DropDownList
btn2.Text = e.Row.Cells(2).Text
btn2.Items.Add("Select")
btn2.Items.Add(e.Row.Cells(2).Text)
btn2.AutoPostBack = True
AddHandler btn2.SelectedIndexChanged, AddressOf Dropdown
btnCustom = btn2
ElseIf dsCONFIG.Tables(0).Rows(iType)("ColumnType") = "CheckBox" Then
Dim btn3 As New CheckBox
AddHandler btn3.CheckedChanged, AddressOf Check_Changed
btnCustom = btn3
End If
e.Row.Cells(iType).Controls.Add(btnCustom)
Next
End Sub
除此之外,Default.aspx 将包含以下代码来设置用户控件的属性
Dim dsCustom As DataSet
Dim dsCONFIG As DataSet
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Not Page.IsPostBack Then
GetDataSet(dsCustom, Server.MapPath("~/DataFile/Book.xml"))
GetDataSet(dsCONFIG, Server.MapPath("~/DataFile/Configuration.xml"))
//This file is the container or test file, which does the following task.
//Passes the configuration and data to the user control.
UcCustomDataGrid1.SetDataSource = dsCustom
UcCustomDataGrid1.SetConfigSource = dsCONFIG
UcCustomDataGrid1.SetGridStyle = ucCustomDataGrid.NamedStyle.AUTUMN
End If
End Sub
//This subroutine reads the xml file and bind it to the dataset
Private Sub GetDataSet(ByRef ds As DataSet, ByVal filename As String)
ds = New DataSet
Dim xmlMenuDoc As XmlDocument = New XmlDocument()
xmlMenuDoc.Load(filename)
Dim xmlString As StringReader = New StringReader(xmlMenuDoc.InnerXml)
ds.ReadXml(xmlString)
End Sub //Get the relevant XML file and populate a DataSet
背景
我正在为一个项目工作,该项目使用了大量的 GridView。每个 GridView 都有不同的功能。例如,有些只读,有些只有复选框,有些可以排序,有些需要分页功能等等。需求是所有内容都应该是可配置的(属性从文件中读取,例如 web.config)或数据库驱动的。GridView 的使用者事先不知道 GridView 的任何信息,例如标题、页脚、列名、字段名等等;所有信息都来自数据库。甚至 GridView 子控件中的客户端功能也将由数据库驱动。
使用代码
该代码是在 Visual Studio 2005 中使用 ASP 2.0 和 VB.NET 开发的。该项目包含以下文件
- 在 DataFile 文件夹下,有两个文件
- Book.xml
- 2) Configuration.xml
- 网页
Default.aspx, Default.aspx.vb - Web 用户控件
ucCustomDataGrid.ascx ucCustomDataGrid.ascx.vb
如何执行该项目
将 Default.aspx 设置为启动页,然后从 Visual Studio.Net 运行它,或者从 IIS 创建一个虚拟目录并运行 Default.aspx
执行 default.aspx 后,输出将如下所示
