<% @ Page Language="C#" %>
<% @ Import Namespace="System.Data" %>
<% @ Import Namespace="System.Data.OleDb" %>
<Script Language="C#" runat="server">
OleDbConnection MyConn;
public void Page_Load(Object src,EventArgs e)
{  
    //连接语句
    string MyConnString="Provider=Microsoft.Jet.OLEDB.4.0;Data Source="+Server.MapPath(".")+"..\\data\\data.mdb;";
   MyConn=new OleDbConnection(MyConnString);
   MyConn.Open();
   if (!Page.IsPostBack)
   {
            BindGrid();
   }  
}ICollection CreateTable()
{
    string strSel="select * from admin";
DataSet ds=new DataSet();
OleDbDataAdapter MyAdapter=new OleDbDataAdapter(strSel,MyConn);
MyAdapter.Fill(ds,"usrGrid");
    return ds.Tables["usrGrid"].DefaultView;}public void BindGrid()
{
     usrGrid.DataSource=CreateTable();
 usrGrid.DataBind();
}//处理Edit命令
public void DataGrid_EditCommand(Object sender,DataGridCommandEventArgs e)
 {
    usrGrid.EditItemIndex=(int)e.Item.ItemIndex;
BindGrid();
 }
 
 //取消
  public void DataGrid_CancelCommand(Object sender,DataGridCommandEventArgs e)
 {
     usrGrid.EditItemIndex=-1;
 BindGrid();
 } //处理update命令
 public void DataGrid_UpdateCommand(Object sender,DataGridCommandEventArgs e)
 {  
     //更新数据库中的信息
 TableRow  tr=e.Item;
e.Item.Cells[0].Width=600;
string ID=tr.Cells[0].Text;
    string pwd=((TextBox)e.Item.Cells[2].Controls[0]).Text;
//Response.Write(pwd);
string strUpdate="Update admin Set [pwd]='"+pwd+"' where [id]="+ID;
OleDbCommand MyComm=new OleDbCommand(strUpdate,MyConn);
MyComm.ExecuteNonQuery();
usrGrid.EditItemIndex=-1;
BindGrid();
 }//分页
public void DataGrid_PageChanged(Object sender,DataGridPageChangedEventArgs e)
{
   usrGrid.CurrentPageIndex=e.NewPageIndex;
   BindGrid();
}//删除
public void DataGrid_ItemCommand(Object sender,DataGridCommandEventArgs e)
{
  if(e.CommandName=="aa")
  {
    e.Item.Cells[4].Attributes.Add("onclick","javascript:return window.confirm('are you sure?');");
  
   TableRow  tr=e.Item;
    string ID=tr.Cells[0].Text;
Response.Write(ID);

string strUpdate="Delete from admin  where [id]="+ID;
OleDbCommand MyComm=new OleDbCommand(strUpdate,MyConn);
MyComm.ExecuteNonQuery();
usrGrid.EditItemIndex=-1;
BindGrid();
    
}
}
</Script>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>东莞商务邮系统!</title>
<link href="css/pt9.css" rel="stylesheet" type="text/css">
</head><body leftmargin="0" topmargin="0" marginwidth="0" marginheight="0">
<!--#include file="top.aspx"-->
<table width="100%" height="88%" border="0" cellpadding="0" cellspacing="0" class="pt9">
  <tr> 
    <td width="16%" valign="top" bgcolor="#EEEEEE"><!--#include file="qxfpLeft.aspx"-->    </td>
    <td width="1" bgcolor="#427DB5"><img src="gif/1.gif" width="1" height="1"></td>
    <td width="83%" valign="top"><font color="#000000"><br>
      </font> 
      <table width="80%" border="0" cellspacing="0" cellpadding="0">
        <tr>
          <td class="pt9"> 当前位置:用户查询</td>
        </tr>
        <tr>
          <td></td>
        </tr>
      </table>
      <table width="80%" border="0" cellspacing="0" cellpadding="0">
        <tr>
          <td><div align="center"><form runat="server">
     <asp:DataGrid id="usrGrid" runat="server"
          HeaderStyle-BackColor="#aaaadd"
 AlternatingItemStyle-BackColor="#eeeeee"
 AutoGenerateColumns="False"
  OnEditCommand="DataGrid_EditCommand"
  OnUpdateCommand="DataGrid_UpdateCommand"
 OnCancelCommand="DataGrid_CancelCommand"
 OnItemCommand="DataGrid_ItemCommand"
 AllowPaging="True"
 PageSize="5"
 PagerStyle-Mode="NumericPages"
 OnPageIndexChanged="DataGrid_PageChanged"
 
  > 
                <Columns>
       <asp:BoundColumn HeaderText="id" DataField="id"  ReadOnly="True"/>
                       <asp:BoundColumn HeaderText="用户名" DataField="username"   ReadOnly="True"/> 
   <asp:BoundColumn HeaderText="密码" DataField="pwd"/> 
<asp:EditCommandColumn HeaderText="操作区"
                   EditText="修改密码"
   UpdateText="更新"
               CancelText="取消"
   ButtonType="PushButton"
          />
<asp:ButtonColumn HeaderText="aa" Text="删除"  CommandName="aa"/>
  </Columns>
</asp:DataGrid>
            </form></div></td>
        </tr>
      </table></td>
  </tr>
</table></body>
</html>
大家帮我看看如何加确定删除对话框?谢谢!

解决方案 »

  1.   

    删除button上加onclick属性 confirm('xxx');
      

  2.   

    小case Attributes.Add("onclick","javascript:return window.confirm('are you sure?');");这句放到Page_Load中去
      

  3.   


    数数楼主的<asp:ButtonColumn HeaderText="aa" Text="删除"  CommandName="aa"/>
    在第几列?(从0开始的哦)        Dim delBttn As LinkButton = e.Item.Cells(2).Controls(0)
            delBttn.Attributes.Add("onclick", "javascript:return confirm('確認要刪除  " + e.Item.Cells(1).Text + " 此記錄嗎 ?');")
      

  4.   

    哦,忘了,补充一下,写在 ItemDataBound 事件中哦:Private Sub DataGrid1_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DataGridItemEventArgs) Handles DataGrid1.ItemDataBound    '確定只是數據行,  而並非為頁首或頁尾
        If (e.Item.ItemType = ListItemType.Item) Or (e.Item.ItemType = ListItemType.AlternatingItem) Then
     
            Dim delBttn As LinkButton = e.Item.Cells(2).Controls(0)
            delBttn.Attributes.Add("onclick", "javascript:return confirm('確認要刪除  " + e.Item.Cells(1).Text + " 此記錄嗎 ?');")
            
         End IfEnd Sub
      

  5.   

    Button1.Attributes.Add("OnClick", "Javascript:return confirm('确实要删除这些记录吗?');");
      

  6.   

    hedonister(我本有心向明月) 及sean168(凉风有兴,秋月无边)的正解
      

  7.   

    我按照hedonister(我本有心向明月)的方法不对哦!!!<% @ Page Language="C#" %>
    <% @ Import Namespace="System.Data" %>
    <% @ Import Namespace="System.Data.OleDb" %>
    <Script Language="C#" runat="server">
    OleDbConnection MyConn;
    public void Page_Load(Object src,EventArgs e)
    {  
       连接语句
         aa.Attributes.Add("onclick","javascript:return window.confirm('are you sure?');");

        string MyConnString="Provider=Microsoft.Jet.OLEDB.4.0;Data Source="+Server.MapPath(".")+"..\\data\\data.mdb;";
       MyConn=new OleDbConnection(MyConnString);
       MyConn.Open();
       if (!Page.IsPostBack)
       {
                BindGrid();
       }  
    }ICollection CreateTable()
    {
        string strSel="select * from admin";
    DataSet ds=new DataSet();
    OleDbDataAdapter MyAdapter=new OleDbDataAdapter(strSel,MyConn);
    MyAdapter.Fill(ds,"usrGrid");
        return ds.Tables["usrGrid"].DefaultView;}public void BindGrid()
    {
         usrGrid.DataSource=CreateTable();
     usrGrid.DataBind();
    }//处理Edit命令
    public void DataGrid_EditCommand(Object sender,DataGridCommandEventArgs e)
     {
        usrGrid.EditItemIndex=(int)e.Item.ItemIndex;
    BindGrid();
     }
     
     //取消
      public void DataGrid_CancelCommand(Object sender,DataGridCommandEventArgs e)
     {
         usrGrid.EditItemIndex=-1;
     BindGrid();
     } //处理update命令
     public void DataGrid_UpdateCommand(Object sender,DataGridCommandEventArgs e)
     {  
         //更新数据库中的信息
     TableRow  tr=e.Item;
    e.Item.Cells[0].Width=600;
    string ID=tr.Cells[0].Text;
        string pwd=((TextBox)e.Item.Cells[2].Controls[0]).Text;
    //Response.Write(pwd);
    string strUpdate="Update admin Set [pwd]='"+pwd+"' where [id]="+ID;
    OleDbCommand MyComm=new OleDbCommand(strUpdate,MyConn);
    MyComm.ExecuteNonQuery();
    usrGrid.EditItemIndex=-1;
    BindGrid();
     }//分页
    public void DataGrid_PageChanged(Object sender,DataGridPageChangedEventArgs e)
    {
       usrGrid.CurrentPageIndex=e.NewPageIndex;
       BindGrid();
    }//删除
    public void DataGrid_ItemCommand(Object sender,DataGridCommandEventArgs e)
    {
      if(e.CommandName=="aa")
      {
      //  e.Item.Cells[4].Attributes.Add("onclick","javascript:return window.confirm('are you sure?');");
      
       TableRow  tr=e.Item;
        string ID=tr.Cells[0].Text;
    Response.Write(ID);

    string strUpdate="Delete from admin  where [id]="+ID;
    OleDbCommand MyComm=new OleDbCommand(strUpdate,MyConn);
    MyComm.ExecuteNonQuery();
    usrGrid.EditItemIndex=-1;
    BindGrid();
        
    }
    }</Script>
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=gb2312">
    <title>东莞商务邮系统!</title>
    <link href="css/pt9.css" rel="stylesheet" type="text/css">
    </head><body leftmargin="0" topmargin="0" marginwidth="0" marginheight="0">
    <!--#include file="top.aspx"-->
    <table width="100%" height="88%" border="0" cellpadding="0" cellspacing="0" class="pt9">
      <tr> 
        <td width="16%" valign="top" bgcolor="#EEEEEE"><!--#include file="qxfpLeft.aspx"-->    </td>
        <td width="1" bgcolor="#427DB5"><img src="gif/1.gif" width="1" height="1"></td>
        <td width="83%" valign="top"><font color="#000000"><br>
          </font> 
          <table width="80%" border="0" cellspacing="0" cellpadding="0">
            <tr>
              <td class="pt9"> 当前位置:用户查询</td>
            </tr>
            <tr>
              <td></td>
            </tr>
          </table>
          <table width="80%" border="0" cellspacing="0" cellpadding="0">
            <tr>
              <td><div align="center"><form runat="server">
         <asp:DataGrid id="usrGrid" runat="server"
              HeaderStyle-BackColor="#aaaadd"
     AlternatingItemStyle-BackColor="#eeeeee"
     AutoGenerateColumns="False"
      OnEditCommand="DataGrid_EditCommand"
      OnUpdateCommand="DataGrid_UpdateCommand"
     OnCancelCommand="DataGrid_CancelCommand"
     OnItemCommand="DataGrid_ItemCommand"
     AllowPaging="True"
     PageSize="5"
     PagerStyle-Mode="NumericPages"
     OnPageIndexChanged="DataGrid_PageChanged"
     
      > 
                    <Columns>
           <asp:BoundColumn HeaderText="id" DataField="id"  ReadOnly="True"/>
                           <asp:BoundColumn HeaderText="用户名" DataField="username"   ReadOnly="True"/> 
       <asp:BoundColumn HeaderText="密码" DataField="pwd"/> 
    <asp:EditCommandColumn HeaderText="操作区"
                       EditText="修改密码"
       UpdateText="更新"
                   CancelText="取消"
       ButtonType="PushButton"
              />
    <asp:ButtonColumn HeaderText="aa" Text="删除"  CommandName="aa" />
      </Columns>
    </asp:DataGrid>
                </form></div></td>
            </tr>
          </table></td>
      </tr>
    </table></body>
    </html>
      

  8.   

    soochowgreen(Green)  right!
      

  9.   

    aa.Attributes.Add("onclick","javascript:return window.confirm('are you sure?');");
    不是aa,是你的控件名