关闭

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

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

DeleteIt2.aspx

<%@ Page Language="vb" AutoEventWireup="false" Codebehind="DeleteIt2.aspx.vb"
 Inherits="aspxWeb.DeleteIt2"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
  <HEAD>
    <title>DeleteIt2</title>
    <meta name="GENERATOR" content="Microsoft Visual Studio .NET 7.0">
    <meta name="CODE_LANGUAGE" content="Visual Basic 7.0">
    <meta name="vs_defaultClientScript" content="JavaScript">
    <meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5">
  </HEAD>
  <body MS_POSITIONING="GridLayout">
    <form id="Form1" method="post" runat="server">
    <asp:Label id="Label1" runat="server"></asp:Label>
      <asp:DataGrid id="myDataGrid" OnItemCreated="DataGrid_ItemCreated" runat="server"
    OnDeleteCommand="MyDataGrid_Delete" DataKeyField="Title" AutoGenerateColumns="False">
        <Columns>
          <asp:ButtonColumn CommandName="Delete" />
          <asp:TemplateColumn>
            <ItemTemplate>
              <asp:Label runat="server"  Text='<%# DataBinder.Eval(Container.DataItem, "Title") %>' />
            </ItemTemplate>
          </asp:TemplateColumn>
          <asp:TemplateColumn>
            <ItemTemplate>
              <asp:Label runat="server" Text='<%# DataBinder.Eval(Container.DataItem,_
      "CreateDate","{0:yyyy-M-d}") %>' />
            </ItemTemplate>
          </asp:TemplateColumn>
        </Columns>
      </asp:DataGrid>     
    </form>
  </body>
</HTML>

DeleteIt2.aspx.vb

Imports System
Imports System.Web
Imports System.Data
Imports System.Data.OleDb
Imports System.Web.UI.WebControls

Public Class DeleteIt2
  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 System.Object, ByVal e As System.EventArgs)_
   Handles MyBase.Load
    '在此处放置初始化页的用户代码
    myDataGrid.HeaderStyle.HorizontalAlign = HorizontalAlign.Center
    myDataGrid.HeaderStyle.Font.Bold = True
    myDataGrid.HeaderStyle.BackColor = System.Drawing.Color.Ivory
    myDataGrid.Columns(0).HeaderText = "操作"
    myDataGrid.Columns(1).HeaderText = "标题"
    myDataGrid.Columns(2).HeaderText = "创建日期"

    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 objectGuid,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
  End Sub

  Public Sub DataGrid_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 LinkButton
        myDeleteButton = myTableCell.Controls(0)
        myDeleteButton.Attributes.Add("onclick", "return confirm('您真的要删除此行吗?');")
        myDeleteButton.Text = "删除此行"
    End Select
  End Sub
  Sub MyDataGrid_Delete(ByVal Sender As Object, ByVal E As DataGridCommandEventArgs)
    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

猜你在找
【直播】计算机视觉原理及实战—屈教授
【套餐】深度学习入门视频课程—唐宇迪
【套餐】Hadoop生态系统零基础入门--侯勇蛟
【套餐】嵌入式Linux C编程基础--朱有鹏
【套餐】2017软考系统集成项目——任铄
【套餐】Android 5.x顶级视频课程——李宁
【直播】广义线性模型及其应用——李科
【直播】从0到1 区块链的概念到实践
【直播】机器学习之凸优化——马博士
【套餐】微信订阅号+服务号Java版 v2.0--翟东平
查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:79473次
    • 积分:1011
    • 等级:
    • 排名:千里之外
    • 原创:3篇
    • 转载:81篇
    • 译文:0篇
    • 评论:8条
    最新评论