我的grid view例子
<%@ Page Language="VB" MasterPageFile="~/MasterPage.master" Title="分类页面" AutoEventWireup="false"
CodeFile="Category.aspx.vb" Inherits="Category" %>
<asp:Content ID="Content" ContentPlaceHolderID="ContentPlaceHolder" runat="Server">
<asp:ScriptManager ID="ScriptManager" runat="server">
</asp:ScriptManager>
<asp:UpdatePanel ID="UpdatePanel" runat="server">
<ContentTemplate>
<div id="searchZone" runat="server">
<div>
<asp:Label runat="server" ID="chineseLabel" Text="中文种类名称:" CssClass="firstLabel"></asp:Label>
<asp:TextBox runat="server" ID="chineseTextBox" CssClass="firstTextBox"></asp:TextBox>(不填,搜索全部;填,搜索包含)
</div>
<div>
<asp:Label runat="server" ID="englishLabel" Text="英语种类名称:" CssClass="firstLabel"></asp:Label>
<asp:TextBox runat="server" ID="englishTextBox" CssClass="firstTextBox"></asp:TextBox>(不填,搜索全部;填,搜索包含)
</div>
<div>
<asp:Button runat="server" CssClass="btnSearch" ID="btnSearch" Text="搜索" />
</div>
</div>
<div id="resultZone" class="resultZone" runat="server">
<asp:SqlDataSource CancelSelectOnNullParameter="false" SelectCommand="SELECT * FROM Category WHERE ((@ChineseDescription IS NULL) OR (ChineseDescription LIKE '%'+@ChineseDescription+'%')) AND ((@EnglishDescription IS NULL) OR (EnglishDescription LIKE '%'+@EnglishDescription+'%'))"
DeleteCommand="DELETE FROM [Category] WHERE [CategoryID] = @CategoryID" UpdateCommand="UPDATE [Category] SET [ChineseDescription] = @ChineseDescription, [EnglishDescription] = @EnglishDescription WHERE [CategoryID] = @CategoryID"
ConnectionString="<%$ ConnectionStrings:TNTConnectionString %>" runat="server"
ID="sdsResult">
<SelectParameters>
<asp:Parameter Name="ChineseDescription" Type="String" />
<asp:Parameter Name="EnglishDescription" Type="String" />
</SelectParameters>
<UpdateParameters>
<asp:Parameter Name="ChineseDescription" Type="String" />
<asp:Parameter Name="EnglishDescription" Type="String" />
<asp:Parameter Name="CategoryID" Type="Int32" />
</UpdateParameters>
<DeleteParameters>
<asp:Parameter Name="CategoryID" Type="Int32" />
</DeleteParameters>
</asp:SqlDataSource>
<div>
<asp:ValidationSummary ID="vsCheck" EnableClientScript="true" HeaderText="请更正以下错误:"
ValidationGroup="vgGridView" runat="server" />
</div>
<asp:GridView DataSourceID="sdsResult" runat="server" ID="gvResult" AllowPaging="True"
AllowSorting="True" BackColor="LightGoldenrodYellow" BorderColor="Tan" BorderWidth="1px"
CellPadding="2" EnableSortingAndPagingCallbacks="True" DataKeyNames="CategoryID"
ForeColor="Black" GridLines="None" AutoGenerateColumns="False" Width="800px">
<FooterStyle BackColor="Tan" />
<RowStyle HorizontalAlign="Center" VerticalAlign="Middle" />
<EmptyDataRowStyle HorizontalAlign="Center" VerticalAlign="Middle" />
<Columns>
<asp:CommandField ShowEditButton="true" ValidationGroup="vgGridView" EditText="修改" CausesValidation="true" />
<asp:CommandField ShowDeleteButton="true" DeleteText="删除(将同时删除该种类下的所有物品)" />
<asp:TemplateField HeaderText="中文种类名称" SortExpression="ChineseDescription">
<ItemTemplate>
<asp:Label runat="server" Text='<%# Eval("ChineseDescription") %>' ID="lblChineseDescription"></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox runat="server" Text='<%# Bind("ChineseDescription") %>' ID="txtChineseDescription"></asp:TextBox>
<asp:RequiredFieldValidator runat="server" ID="rfvChineseDescription" ValidationGroup="vgGridView"
ControlToValidate="txtChineseDescription" Text="*" ErrorMessage="中文种类名称不能为空"></asp:RequiredFieldValidator>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="英文种类名称" SortExpression="EnglishDescription">
<ItemTemplate>
<asp:Label runat="server" Text='<%# Eval("EnglishDescription") %>' ID="lblEnglishDescription"></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox runat="server" Text='<%# Bind("EnglishDescription") %>' ID="txtEnglishDescription"></asp:TextBox>
<asp:RequiredFieldValidator runat="server" ID="rfvEnglishDescription" ValidationGroup="vgGridView"
ControlToValidate="txtEnglishDescription" Text="*" ErrorMessage="英文种类名称不能为空"></asp:RequiredFieldValidator>
</EditItemTemplate>
</asp:TemplateField>
</Columns>
<PagerStyle BackColor="PaleGoldenrod" ForeColor="DarkSlateBlue" HorizontalAlign="Center" />
<SelectedRowStyle BackColor="DarkSlateBlue" ForeColor="GhostWhite" />
<HeaderStyle BackColor="Tan" Font-Bold="True" HorizontalAlign="Center" VerticalAlign="Middle" />
<EditRowStyle HorizontalAlign="Center" VerticalAlign="Middle" />
<AlternatingRowStyle BackColor="PaleGoldenrod" HorizontalAlign="Center" VerticalAlign="Middle" />
</asp:GridView>
</div>
<div id="addZone" runat="server">
<div>
<asp:ValidationSummary ID="vsValidation" EnableClientScript="true" HeaderText="请更正以下错误:"
ValidationGroup="vgCategory" runat="server" />
</div>
<div>
<asp:Label runat="server" ID="lblChineseDescription" Text="中文种类名称:" CssClass="firstLabel"></asp:Label>
<asp:TextBox runat="server" ID="txtChineseDescription" CssClass="firstTextBox"></asp:TextBox>
<asp:RequiredFieldValidator ID="rfvChineseDescription" Text="*" ValidationGroup="vgCategory"
ControlToValidate="txtChineseDescription" runat="server" ErrorMessage="中文种类名称不能为空"></asp:RequiredFieldValidator>
</div>
<div>
<asp:Label runat="server" ID="lblEnglishDescription" Text="英语种类名称:" CssClass="firstLabel"></asp:Label>
<asp:TextBox runat="server" ID="txtEnglishDescription" CssClass="firstTextBox"></asp:TextBox>
<asp:RequiredFieldValidator ID="rfvEnglishDescription" Text="*" ValidationGroup="vgCategory"
ControlToValidate="txtEnglishDescription" runat="server" ErrorMessage="英文种类名称不能为空"></asp:RequiredFieldValidator>
</div>
<div>
<asp:Button runat="server" CausesValidation="true" ValidationGroup="vgCategory" ID="btnAdd"
Text="添加" />
</div>
</div>
</ContentTemplate>
</asp:UpdatePanel>
</asp:Content>
CodeFile="Category.aspx.vb" Inherits="Category" %>
<asp:Content ID="Content" ContentPlaceHolderID="ContentPlaceHolder" runat="Server">
<asp:ScriptManager ID="ScriptManager" runat="server">
</asp:ScriptManager>
<asp:UpdatePanel ID="UpdatePanel" runat="server">
<ContentTemplate>
<div id="searchZone" runat="server">
<div>
<asp:Label runat="server" ID="chineseLabel" Text="中文种类名称:" CssClass="firstLabel"></asp:Label>
<asp:TextBox runat="server" ID="chineseTextBox" CssClass="firstTextBox"></asp:TextBox>(不填,搜索全部;填,搜索包含)
</div>
<div>
<asp:Label runat="server" ID="englishLabel" Text="英语种类名称:" CssClass="firstLabel"></asp:Label>
<asp:TextBox runat="server" ID="englishTextBox" CssClass="firstTextBox"></asp:TextBox>(不填,搜索全部;填,搜索包含)
</div>
<div>
<asp:Button runat="server" CssClass="btnSearch" ID="btnSearch" Text="搜索" />
</div>
</div>
<div id="resultZone" class="resultZone" runat="server">
<asp:SqlDataSource CancelSelectOnNullParameter="false" SelectCommand="SELECT * FROM Category WHERE ((@ChineseDescription IS NULL) OR (ChineseDescription LIKE '%'+@ChineseDescription+'%')) AND ((@EnglishDescription IS NULL) OR (EnglishDescription LIKE '%'+@EnglishDescription+'%'))"
DeleteCommand="DELETE FROM [Category] WHERE [CategoryID] = @CategoryID" UpdateCommand="UPDATE [Category] SET [ChineseDescription] = @ChineseDescription, [EnglishDescription] = @EnglishDescription WHERE [CategoryID] = @CategoryID"
ConnectionString="<%$ ConnectionStrings:TNTConnectionString %>" runat="server"
ID="sdsResult">
<SelectParameters>
<asp:Parameter Name="ChineseDescription" Type="String" />
<asp:Parameter Name="EnglishDescription" Type="String" />
</SelectParameters>
<UpdateParameters>
<asp:Parameter Name="ChineseDescription" Type="String" />
<asp:Parameter Name="EnglishDescription" Type="String" />
<asp:Parameter Name="CategoryID" Type="Int32" />
</UpdateParameters>
<DeleteParameters>
<asp:Parameter Name="CategoryID" Type="Int32" />
</DeleteParameters>
</asp:SqlDataSource>
<div>
<asp:ValidationSummary ID="vsCheck" EnableClientScript="true" HeaderText="请更正以下错误:"
ValidationGroup="vgGridView" runat="server" />
</div>
<asp:GridView DataSourceID="sdsResult" runat="server" ID="gvResult" AllowPaging="True"
AllowSorting="True" BackColor="LightGoldenrodYellow" BorderColor="Tan" BorderWidth="1px"
CellPadding="2" EnableSortingAndPagingCallbacks="True" DataKeyNames="CategoryID"
ForeColor="Black" GridLines="None" AutoGenerateColumns="False" Width="800px">
<FooterStyle BackColor="Tan" />
<RowStyle HorizontalAlign="Center" VerticalAlign="Middle" />
<EmptyDataRowStyle HorizontalAlign="Center" VerticalAlign="Middle" />
<Columns>
<asp:CommandField ShowEditButton="true" ValidationGroup="vgGridView" EditText="修改" CausesValidation="true" />
<asp:CommandField ShowDeleteButton="true" DeleteText="删除(将同时删除该种类下的所有物品)" />
<asp:TemplateField HeaderText="中文种类名称" SortExpression="ChineseDescription">
<ItemTemplate>
<asp:Label runat="server" Text='<%# Eval("ChineseDescription") %>' ID="lblChineseDescription"></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox runat="server" Text='<%# Bind("ChineseDescription") %>' ID="txtChineseDescription"></asp:TextBox>
<asp:RequiredFieldValidator runat="server" ID="rfvChineseDescription" ValidationGroup="vgGridView"
ControlToValidate="txtChineseDescription" Text="*" ErrorMessage="中文种类名称不能为空"></asp:RequiredFieldValidator>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="英文种类名称" SortExpression="EnglishDescription">
<ItemTemplate>
<asp:Label runat="server" Text='<%# Eval("EnglishDescription") %>' ID="lblEnglishDescription"></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox runat="server" Text='<%# Bind("EnglishDescription") %>' ID="txtEnglishDescription"></asp:TextBox>
<asp:RequiredFieldValidator runat="server" ID="rfvEnglishDescription" ValidationGroup="vgGridView"
ControlToValidate="txtEnglishDescription" Text="*" ErrorMessage="英文种类名称不能为空"></asp:RequiredFieldValidator>
</EditItemTemplate>
</asp:TemplateField>
</Columns>
<PagerStyle BackColor="PaleGoldenrod" ForeColor="DarkSlateBlue" HorizontalAlign="Center" />
<SelectedRowStyle BackColor="DarkSlateBlue" ForeColor="GhostWhite" />
<HeaderStyle BackColor="Tan" Font-Bold="True" HorizontalAlign="Center" VerticalAlign="Middle" />
<EditRowStyle HorizontalAlign="Center" VerticalAlign="Middle" />
<AlternatingRowStyle BackColor="PaleGoldenrod" HorizontalAlign="Center" VerticalAlign="Middle" />
</asp:GridView>
</div>
<div id="addZone" runat="server">
<div>
<asp:ValidationSummary ID="vsValidation" EnableClientScript="true" HeaderText="请更正以下错误:"
ValidationGroup="vgCategory" runat="server" />
</div>
<div>
<asp:Label runat="server" ID="lblChineseDescription" Text="中文种类名称:" CssClass="firstLabel"></asp:Label>
<asp:TextBox runat="server" ID="txtChineseDescription" CssClass="firstTextBox"></asp:TextBox>
<asp:RequiredFieldValidator ID="rfvChineseDescription" Text="*" ValidationGroup="vgCategory"
ControlToValidate="txtChineseDescription" runat="server" ErrorMessage="中文种类名称不能为空"></asp:RequiredFieldValidator>
</div>
<div>
<asp:Label runat="server" ID="lblEnglishDescription" Text="英语种类名称:" CssClass="firstLabel"></asp:Label>
<asp:TextBox runat="server" ID="txtEnglishDescription" CssClass="firstTextBox"></asp:TextBox>
<asp:RequiredFieldValidator ID="rfvEnglishDescription" Text="*" ValidationGroup="vgCategory"
ControlToValidate="txtEnglishDescription" runat="server" ErrorMessage="英文种类名称不能为空"></asp:RequiredFieldValidator>
</div>
<div>
<asp:Button runat="server" CausesValidation="true" ValidationGroup="vgCategory" ID="btnAdd"
Text="添加" />
</div>
</div>
</ContentTemplate>
</asp:UpdatePanel>
</asp:Content>
Imports System.Data.SqlClient
Imports System.Data
Partial Class Category
Inherits System.Web.UI.Page
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If (Me.Session("Admin") <> "admin") Then
Me.Response.Redirect("login.aspx")
End If
End Sub
Protected Sub gvResult_RowDeleting(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewDeleteEventArgs) Handles gvResult.RowDeleting
Dim CategoryID As Integer = e.Keys("CategoryID")
Dim conn As New SqlConnection
conn.ConnectionString = ConfigurationManager.ConnectionStrings("TNTConnectionString").ConnectionString
conn.Open()
Dim comm As New SqlCommand
comm.Connection = conn
comm.CommandText = "DELETE FROM Item WHERE CategoryID=@CategoryID"
comm.Parameters.AddWithValue("@CategoryID", CategoryID)
comm.ExecuteNonQuery()
comm.Dispose()
conn.Close()
conn.Dispose()
End Sub
Protected Sub btnAdd_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnAdd.Click
Dim conn As New SqlConnection
conn.ConnectionString = ConfigurationManager.ConnectionStrings("TNTConnectionString").ConnectionString
conn.Open()
Dim comm As New SqlCommand
comm.Connection = conn
comm.CommandText = "INSERT INTO Category(ChineseDescription,EnglishDescription) VALUES(@ChineseDescription,@EnglishDescription)"
comm.Parameters.AddWithValue("@ChineseDescription", Me.txtChineseDescription.Text)
comm.Parameters.AddWithValue("@EnglishDescription", Me.txtEnglishDescription.Text)
comm.ExecuteNonQuery()
comm.Dispose()
conn.Close()
conn.Dispose()
Me.gvResult.DataBind()
End Sub
Protected Sub sdsResult_Selecting(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.SqlDataSourceSelectingEventArgs) Handles sdsResult.Selecting
Try
If (Not String.IsNullOrEmpty(Me.chineseTextBox.Text)) Then
e.Command.Parameters("@ChineseDescription").Value = Me.chineseTextBox.Text
Else
e.Command.Parameters("@ChineseDescription").Value = DBNull.Value
End If
Catch ex As Exception
e.Command.Parameters("@ChineseDescription").Value = DBNull.Value
End Try
Try
If (Not String.IsNullOrEmpty(Me.englishTextBox.Text)) Then
e.Command.Parameters("@EnglishDescription").Value = Me.englishTextBox.Text
Else
e.Command.Parameters("@EnglishDescription").Value = DBNull.Value
End If
Catch ex As Exception
e.Command.Parameters("@EnglishDescription").Value = DBNull.Value
End Try
End Sub
Protected Sub btnSearch_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSearch.Click
Me.gvResult.DataBind()
End Sub
'Protected Sub gvResult_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles gvResult.RowDataBound
' If (e.Row.RowType = DataControlRowType.DataRow) Then
' If (e.Row.RowState = DataControlRowState.Edit) Or (e.Row.RowState = DataControlRowState.Alternate + DataControlRowState.Edit) Then
' Dim drv As DataRowView = CType(e.Row.DataItem, DataRowView)
' Dim txtChineseDescription As TextBox = CType(e.Row.FindControl("txtChineseDescription"), TextBox)
' txtChineseDescription.Text = drv("ChineseDescription")
' Dim txtEnglishDescription As TextBox = CType(e.Row.FindControl("txtEnglishDescription"), TextBox)
' txtEnglishDescription.Text = drv("EnglishDescription")
' ElseIf (e.Row.RowState = DataControlRowState.Normal) Or (e.Row.RowState = DataControlRowState.Normal + DataControlRowState.Alternate) Then
' Dim drv As DataRowView = CType(e.Row.DataItem, DataRowView)
' Dim lblChineseDescription As Label = CType(e.Row.FindControl("lblChineseDescription"), Label)
' lblChineseDescription.Text = drv("ChineseDescription")
' Dim lblEnglishDescription As Label = CType(e.Row.FindControl("lblEnglishDescription"), Label)
' lblEnglishDescription.Text = drv("EnglishDescription")
' End If
' End If
'End Sub
End Class
Imports System.Data
Partial Class Category
Inherits System.Web.UI.Page
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If (Me.Session("Admin") <> "admin") Then
Me.Response.Redirect("login.aspx")
End If
End Sub
Protected Sub gvResult_RowDeleting(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewDeleteEventArgs) Handles gvResult.RowDeleting
Dim CategoryID As Integer = e.Keys("CategoryID")
Dim conn As New SqlConnection
conn.ConnectionString = ConfigurationManager.ConnectionStrings("TNTConnectionString").ConnectionString
conn.Open()
Dim comm As New SqlCommand
comm.Connection = conn
comm.CommandText = "DELETE FROM Item WHERE CategoryID=@CategoryID"
comm.Parameters.AddWithValue("@CategoryID", CategoryID)
comm.ExecuteNonQuery()
comm.Dispose()
conn.Close()
conn.Dispose()
End Sub
Protected Sub btnAdd_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnAdd.Click
Dim conn As New SqlConnection
conn.ConnectionString = ConfigurationManager.ConnectionStrings("TNTConnectionString").ConnectionString
conn.Open()
Dim comm As New SqlCommand
comm.Connection = conn
comm.CommandText = "INSERT INTO Category(ChineseDescription,EnglishDescription) VALUES(@ChineseDescription,@EnglishDescription)"
comm.Parameters.AddWithValue("@ChineseDescription", Me.txtChineseDescription.Text)
comm.Parameters.AddWithValue("@EnglishDescription", Me.txtEnglishDescription.Text)
comm.ExecuteNonQuery()
comm.Dispose()
conn.Close()
conn.Dispose()
Me.gvResult.DataBind()
End Sub
Protected Sub sdsResult_Selecting(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.SqlDataSourceSelectingEventArgs) Handles sdsResult.Selecting
Try
If (Not String.IsNullOrEmpty(Me.chineseTextBox.Text)) Then
e.Command.Parameters("@ChineseDescription").Value = Me.chineseTextBox.Text
Else
e.Command.Parameters("@ChineseDescription").Value = DBNull.Value
End If
Catch ex As Exception
e.Command.Parameters("@ChineseDescription").Value = DBNull.Value
End Try
Try
If (Not String.IsNullOrEmpty(Me.englishTextBox.Text)) Then
e.Command.Parameters("@EnglishDescription").Value = Me.englishTextBox.Text
Else
e.Command.Parameters("@EnglishDescription").Value = DBNull.Value
End If
Catch ex As Exception
e.Command.Parameters("@EnglishDescription").Value = DBNull.Value
End Try
End Sub
Protected Sub btnSearch_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSearch.Click
Me.gvResult.DataBind()
End Sub
'Protected Sub gvResult_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles gvResult.RowDataBound
' If (e.Row.RowType = DataControlRowType.DataRow) Then
' If (e.Row.RowState = DataControlRowState.Edit) Or (e.Row.RowState = DataControlRowState.Alternate + DataControlRowState.Edit) Then
' Dim drv As DataRowView = CType(e.Row.DataItem, DataRowView)
' Dim txtChineseDescription As TextBox = CType(e.Row.FindControl("txtChineseDescription"), TextBox)
' txtChineseDescription.Text = drv("ChineseDescription")
' Dim txtEnglishDescription As TextBox = CType(e.Row.FindControl("txtEnglishDescription"), TextBox)
' txtEnglishDescription.Text = drv("EnglishDescription")
' ElseIf (e.Row.RowState = DataControlRowState.Normal) Or (e.Row.RowState = DataControlRowState.Normal + DataControlRowState.Alternate) Then
' Dim drv As DataRowView = CType(e.Row.DataItem, DataRowView)
' Dim lblChineseDescription As Label = CType(e.Row.FindControl("lblChineseDescription"), Label)
' lblChineseDescription.Text = drv("ChineseDescription")
' Dim lblEnglishDescription As Label = CType(e.Row.FindControl("lblEnglishDescription"), Label)
' lblEnglishDescription.Text = drv("EnglishDescription")
' End If
' End If
'End Sub
End Class