关闭

为DataGrid添加确认删除的对话框(3)

标签: datagridmicrosoftobjectgeneratorasphtml
512人阅读 评论(0) 收藏 举报
分类:

DeleteIt3.aspx

<%@ Page Language="vb" AutoEventWireup="false" Codebehind="DeteleIt3.aspx.vb" Inherits="aspxWeb.DeteleIt3"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
  <HEAD>
    <title>DeteleIt3</title>
    <meta content="Microsoft Visual Studio .NET 7.0" name="GENERATOR">
    <meta content="Visual Basic 7.0" name="CODE_LANGUAGE">
    <meta content="JavaScript" name="vs_defaultClientScript">
    <meta content="http://schemas.microsoft.com/intellisense/ie5" name="vs_targetSchema">
  </HEAD>
  <body MS_POSITIONING="GridLayout">
    <asp:label id="Label1" runat="Server"></asp:label>
    <form id="Form1" runat="server">
      <asp:datagrid id="myDataGrid" runat="server" AutoGenerateColumns="True" DataKeyField="Title">
        <Columns>
          <asp:ButtonColumn CommandName="Delete" ButtonType="PushButton" />
        </Columns>
      </asp:datagrid>
    </form>
  </body>
</HTML>

后代码DeteleIt3.aspx.vb

Imports System.Web
Imports System.Data
Imports System.Data.OleDb

Public Class DeteleIt3
  Inherits System.Web.UI.Page
  Protected WithEvents Label1 As System.Web.UI.WebControls.Label
  Protected WithEvents myDataGrid As System.Web.UI.WebControls.DataGrid

#Region " Web 窗体设计器生成的代码 "

  '该调用是 Web 窗体设计器所必需的。
  <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()

  End Sub

  Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init
    'CODEGEN: 此方法调用是 Web 窗体设计器所必需的
    '不要使用代码编辑器修改它。
    InitializeComponent()
  End Sub

#End Region

  Private Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load
    '在此处放置初始化页的用户代码
    If Not IsPostBack Then
      Dim strCn As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + Server.MapPath("Test.mdb")
      Dim strSql As String = "SELECT Top 15 Title,CreateDate FROM Document Order By CreateDate Desc"
      Dim cn As New OleDbConnection(strCn)
      cn.Open()
      Dim cmd As New OleDbCommand(strSql, cn)
      myDataGrid.DataSource = cmd.ExecuteReader(CommandBehavior.CloseConnection)
      myDataGrid.DataBind()
      cmd.Dispose()
      cmd = Nothing
      cn.Close()
      cn.Dispose()
      cn = Nothing
    End If
    Page.RegisterOnSubmitStatement("OnSubmit", "{return confirm('您真的要删除此行吗??') } ")
  End Sub

  Sub myDataGrid_ItemCreated(ByVal sender As Object, _
   ByVal e As System.Web.UI.WebControls.DataGridItemEventArgs) Handles myDataGrid.ItemCreated
    Select Case e.Item.ItemType
      Case ListItemType.Item, ListItemType.AlternatingItem, ListItemType.EditItem
        Dim myTableCell As TableCell
        myTableCell = e.Item.Cells(0)
        Dim myDeleteButton As Button
        myDeleteButton = myTableCell.Controls(0)
        myDeleteButton.Text = "删除此行"
      Case ListItemType.Header
        e.Item.Font.Bold = True
        e.Item.HorizontalAlign = HorizontalAlign.Center
        e.Item.BackColor = System.Drawing.Color.Ivory
        e.Item.Cells(0).Text = "操作"
        e.Item.Cells(1).Text = "文章标题"
        e.Item.Cells(2).Text = "发布日期"
    End Select
  End Sub

  Private Sub myDataGrid_DeleteCommand(ByVal source As Object, _
   ByVal e As System.Web.UI.WebControls.DataGridCommandEventArgs) Handles myDataGrid.DeleteCommand
    Label1.Text = "您删除的是:<font color=red>" + myDataGrid.DataKeys(CInt(e.Item.ItemIndex)) + "</font>"
    e.Item.BackColor = System.Drawing.Color.Tomato
  End Sub

End Class

 

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:80654次
    • 积分:1035
    • 等级:
    • 排名:千里之外
    • 原创:3篇
    • 转载:81篇
    • 译文:0篇
    • 评论:8条
    最新评论