GridView控件功能演示

时间:2006/4/24 9:43:05      阅读:2520     附件: GridViewConfirmWhenDelet.rar (经验值:0)     下载:22
介绍
在使用GridView控件处理数据的时候,很多朋友都遇到过同样的问题,当删除一条数据时我们如何让按下删除键时提示是否要删除该记录呢?本文章将介绍使用js代码控制,让我们看 GridView 的 HTML 部份:

<%@ Page Language="VB" AutoEventWireup="false" CodeFile="GridViewConfirmWhenDelet.aspx.vb" Inherits="GridViewConfirmWhenDelet" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>踢西工作室--.NET2.0--GridView控件功能演示</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:GridView DataKeyNames="CategoryID" ID="GridView1" runat="server" AutoGenerateColumns="False" OnRowCommand="GridView1_RowCommand" OnRowDataBound="GridView1_RowDataBound" OnRowDeleted="GridView1_RowDeleted" OnRowDeleting="GridView1_RowDeleting">
<Columns>
<asp:BoundField DataField="CategoryID" HeaderText="CategoryID" />
<asp:BoundField DataField="CategoryName" HeaderText="CategoryName" />
<asp:TemplateField HeaderText="Select">
<ItemTemplate>
<asp:LinkButton ID="LinkButton1" CommandArgument='<%# Eval("CategoryID") %>' CommandName="Delete" runat="server">Delete</asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Add">
<ItemTemplate>
<asp:Button ID="Button1" runat="server" Text="Button" CommandName="Delete" />
</ItemTemplate>
</asp:TemplateField>
<asp:CommandField ShowDeleteButton="True" />
</Columns>
</asp:GridView>
</div>
</form>
</body>
</html>


从上面的代码可以看到,只要按下LinkButton时,可以通过RowDataBound的事件进行删除前的提示。 现在,让我们看代码:
Imports System.Data
Imports System.Data.SqlClient
Partial Class GridViewConfirmWhenDelet
Inherits System.Web.UI.Page

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Not Page.IsPostBack Then
BindData()
End If
End Sub


Private ReadOnly Property ConnectionString() As String
Get
Return "Server=localhost;Database=Northwind;Trusted_Connection=true"
End Get
End Property

Private Sub BindData()
Dim query As String = "SELECT * FROM Categories"
Dim myConnection As SqlConnection = New SqlConnection(ConnectionString)
Dim ad As SqlDataAdapter = New SqlDataAdapter(query, myConnection)
Dim ds As DataSet = New DataSet()
ad.Fill(ds,
"Categories")
GridView1.DataSource
= ds
GridView1.DataBind()

End Sub


Private Sub DeleteRecordByID(ByVal categoryID As Integer)
' 链接到数据库
' 参数
' 删除记录
End Sub


Protected Sub GridView1_RowCommand(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewCommandEventArgs) Handles GridView1.RowCommand
If e.CommandName = "Delete" Then
Dim categoryID As Integer = Convert.ToInt32(e.CommandArgument)
' 删除记录
DeleteRecordByID(categoryID)

End If
End Sub


Protected Sub GridView1_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridView1.RowDataBound
If e.Row.RowType = DataControlRowType.DataRow Then
Dim l As LinkButton = CType(e.Row.FindControl("LinkButton1"), LinkButton)

l.Attributes.Add(
"onclick", "confirm('你确定你想要删除这一笔记录吗? " & DataBinder.Eval(e.Row.DataItem, "CategoryID") & "')")

End If
End Sub


Protected Sub GridView1_RowDeleting(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewDeleteEventArgs) Handles GridView1.RowDeleting
Dim categoryID As Integer = CType(GridView1.DataKeys(e.RowIndex).Value, Integer)
DeleteRecordByID(categoryID)
End Sub


End Class


评论
  • Re:GridView控件功能演示  (2006/8/16 14:08:58) by 飘萍 
        学了一招,谢谢。
  • Re:GridView控件功能演示  (2007/7/4 22:18:58) by fish 
    谢谢!!!
标 题:
 
姓 名:
 
主 页:

验证码:

评论:
 

Because of the cache,you may see your comments several minutes later.