我算是菜吧:刚才有十几位兄台帮我回答了.都没有搞定; 不是十几位大侠不会:是我自己的问题.
现将代码贴出来.也加上注释.就劳烦大侠们在我代码的基础上加上代码.这个小问题:如果搞不定,真是怀疑自己是否适合做程序员!
private void deleteUser_Click(object sender, System.EventArgs e)
{
int count=0;
for(int u=0;u<this.datagridShow.Items.Count;u++)
{
if(((System.Web.UI.WebControls.CheckBox)datagridShow.Items[u].Cells[0].Controls[1]).Checked)
{
count++;   这个变量是确认用户选中项。
}
}
if(count!=0)     以此来判断是否执行删除操作。
{
for(int u=0;u<this.datagridShow.Items.Count;u++)
{     循环要删除的项操作
if(((System.Web.UI.WebControls.CheckBox)datagridShow.Items[u].Cells[0].Controls[1]).Checked)
{
int x=Convert.ToInt32(this.datagridShow.Items[u].Cells[1].Text);
Users obj =new Users();
obj.deleteUser("deleteUser",x);
                                                       上面这个对象调用中间层。第一个参数是存储过程名。第二个参数大家知道的:DataGrid里当前选中行的单元格的值。
}
}
this.fun();
}
else
{
this.RegisterStartupScript("key","<script language=jscript>alert('请选中要删除的账户选项!')</script>");
}
}
这是整个删除按钮的代码:注明一点:按钮不在DataGrid里面,是一个独立的按钮.
this.deleteUser.Attributes.Add("onclick","return confirm (\"确定要删除此项记录吗?\");");
这段代码我试过出现的问题:
如果加在删除按钮里:会出现第一次点击的时候根本就不谈出确认对话框.加在载入页面事件里:会出现每次点击的时候都先出现对话框.再开始执行删除事件里的代码:为什么提出先执行呢:因为我在DataGrid事件里有判断用户是否选中了项。页现在我想实现的是:用户在点击删除按钮事件的时候:想先判断用户是否选中了项。如果没选中:报告让用户选中项后再删除。如果选中:就直按谈出确认对话框,让用户确认是否选中。

解决方案 »

  1.   

    刚才看到这个问题 照上面说的就可以呀
    this.deleteUser.Attributes.Add("onclick","return confirm (\"确定要删除此项记录吗?\");");
    把这句加在 page——load里
    就可以了呀
    如果你点确定 就执行 deleteUser——onclick
    如果点击取消 就不执行onclick事件  
    我试过的 可以的呀
      

  2.   

    http://www.cnblogs.com/goody9807/archive/2005/03/14/118317.html
      

  3.   

    aspx 页面中 
    <asp:TemplateColumn HeaderText=" 删    除">
    <ItemTemplate>
    <asp:ImageButton CausesValidation="False" id="Imagebutton2" runat="server" ImageUrl="../../images/delete.gif"
    AlternateText="删除" Runat="server" CommandName="delete"></asp:ImageButton>
    </ItemTemplate>
    </asp:TemplateColumn>  
    Cs 中
      
    private void gd_xx_ItemDataBound(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
    {
    if (e.Item.ItemType == ListItemType.Item ||
    e.Item.ItemType == ListItemType.AlternatingItem) 
    {
    ImageButton button = (ImageButton) e.Item.FindControl("Imagebutton2");
    button.Attributes.Add ("onclick",
    "return confirm (\"确定要删除此项记录吗?\");");
    }
    }
    private void gd_xx_DeleteCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
    {
    //写你的删除  
    }
      

  4.   

    加在载入页面事件里:会出现每次点击的时候都先出现对话框.再开始执行删除事件里的代码:为什么提出先执行呢:因为我在DataGrid事件里有判断用户是否选中了项。那就加在你判断用户选中的代码里
      

  5.   

    if (workflowhelperfacade.GetContent_Type(mPBOID) < 1)
                            {
                                //                                if(MessageBox.Show("您拟制的文档没有主要内容,您确定要完成拟制任务吗?","提示",MessageBoxButtons.YesNo,MessageBoxIcon.Question) == DialogResult.No)
                                //                                {
                                //                                    return;
                                //                                }
                                string script=@"<script>
                                                    function sureOver()
                                                    {
                                                        if(confirm('您拟制的文档没有主要内容,您确定要完成拟制任务吗?')==true)
                                                            __doPostBack('btnOverTask','');
                                                    }
                                                    </script>";
                                Page.RegisterClientScriptBlock("BeSure",script);
                                Page.RegisterStartupScript("msgOver","<script>sureOver();</script>");
                                return;
                            }
      

  6.   

    boytomato(深爱一人叫颖的女孩!) 这位大哥:您好!
    上面有注明:我的删除按钮是一个单独的控件.不在DataGrid里面
      

  7.   

    button.Attributes.Add ("onclick",
    "return confirm (\"确定要删除此项记录吗?\");");
    那你把它直接加在 load 中就行了....
      

  8.   

    你写个js 当有选择时,删除按钮 才是可用的...在checkbox 添加 js 事件   和下边这个类似......private void gd_xx_ItemDataBound(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
    {
    if (e.Item.ItemType == ListItemType.Item ||
    e.Item.ItemType == ListItemType.AlternatingItem) 
    {
    ImageButton button = (ImageButton) e.Item.FindControl("Imagebutton2");
    button.Attributes.Add ("onclick",
    "return confirm (\"确定要删除此项记录吗?\");");
    }
    }
      

  9.   

    楼上的大哥:我都说了不是DataGrid里的按钮.
    是一个独立的按钮.
      

  10.   

    你写个js 当有选择时,删除按钮 才是可用的...在checkbox 添加 js 事件   和下边这个类似......private void gd_xx_ItemDataBound(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
    {
    if (e.Item.ItemType == ListItemType.Item ||
    e.Item.ItemType == ListItemType.AlternatingItem) 
    {
    ImageButton button = (ImageButton) e.Item.FindControl("Imagebutton2");
    button.Attributes.Add ("onclick",
    "return confirm (\"确定要删除此项记录吗?\");");
    }
    }  
     
    Top  
     
     回复人: libinguest(师欣) ( ) 信誉:100  2005-06-08 18:06:00  得分: 0  
     
     
       楼上的大哥:我都说了不是DataGrid里的按钮.
    是一个独立的按钮.  
     
    Top  
     
      

  11.   

    <%@ Page language="c#" Codebehind="WebForm2.aspx.cs" AutoEventWireup="false" Inherits="ReportTest.WebForm2" %>
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
    <HTML>
    <HEAD>
    <title>WebForm2</title>
    <meta name="GENERATOR" Content="Microsoft Visual Studio .NET 7.1">
    <meta name="CODE_LANGUAGE" Content="C#">
    <meta name="vs_defaultClientScript" content="JavaScript">
    <meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5">
    <script>
    function get_value(id)
     {
      var  s=document.getElementById("Button1");
        
           s.disabled=!id.checked;
         
       
        
            
      
     }
    </script>
    </HEAD>
    <body MS_POSITIONING="GridLayout">
    <form id="Form1" method="post" runat="server">
    <FONT face="宋体">
    <asp:DataGrid id="DataGrid1" style="Z-INDEX: 101; LEFT: 152px; POSITION: absolute; TOP: 24px"
    runat="server" Width="368px" Height="208px" DataSource="<%# dataSet11 %>" DataMember="authors" AutoGenerateColumns="False">
    <Columns>
    <asp:BoundColumn DataField="Expr1" SortExpression="Expr1" HeaderText="Expr1"></asp:BoundColumn>
    <asp:BoundColumn DataField="Expr2" SortExpression="Expr2" HeaderText="Expr2"></asp:BoundColumn>
    <asp:TemplateColumn>
    <ItemTemplate>
    <asp:CheckBox ID="check" Runat="server"></asp:CheckBox>
    </ItemTemplate>
    </asp:TemplateColumn>
    </Columns>
    </asp:DataGrid>
    <asp:Button id="Button1" style="Z-INDEX: 102; LEFT: 592px; POSITION: absolute; TOP: 120px" runat="server"
    Text="Button" Enabled="False"></asp:Button></FONT>
    </form>
    </body>
    </HTML>using System;
    using System.Collections;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Web;
    using System.Web.SessionState;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.UI.HtmlControls;namespace ReportTest
    {
    /// <summary>
    /// WebForm2 的摘要说明。
    /// </summary>
    public class WebForm2 : System.Web.UI.Page
    {
    protected System.Web.UI.WebControls.DataGrid DataGrid1;
    protected System.Data.SqlClient.SqlDataAdapter sqlDataAdapter1;
    protected System.Data.SqlClient.SqlCommand sqlSelectCommand1;
    protected ReportTest.DataSet1 dataSet11;
    protected System.Web.UI.WebControls.Button Button1;
    protected System.Data.SqlClient.SqlConnection sqlConnection1;

    private void Page_Load(object sender, System.EventArgs e)
    {
    // 在此处放置用户代码以初始化页面
     this.sqlDataAdapter1.Fill (this.dataSet11 ); this.DataGrid1.DataSource=this.dataSet11.Tables[0];
     this.DataGrid1 .DataBind ();
    this.Button1.Attributes.Add ("onclick",
    "return confirm (\"确定要删除此项记录吗?\");");
    } #region Web 窗体设计器生成的代码
    override protected void OnInit(EventArgs e)
    {
    //
    // CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
    //
    InitializeComponent();
    base.OnInit(e);
    }

    /// <summary>
    /// 设计器支持所需的方法 - 不要使用代码编辑器修改
    /// 此方法的内容。
    /// </summary>
    private void InitializeComponent()
    {    
    this.sqlDataAdapter1 = new System.Data.SqlClient.SqlDataAdapter();
    this.sqlSelectCommand1 = new System.Data.SqlClient.SqlCommand();
    this.sqlConnection1 = new System.Data.SqlClient.SqlConnection();
    this.dataSet11 = new ReportTest.DataSet1();
    ((System.ComponentModel.ISupportInitialize)(this.dataSet11)).BeginInit();
    // 
    // sqlDataAdapter1
    // 
    this.sqlDataAdapter1.SelectCommand = this.sqlSelectCommand1;
    this.sqlDataAdapter1.TableMappings.AddRange(new System.Data.Common.DataTableMapping[] {
      new System.Data.Common.DataTableMapping("Table", "authors", new System.Data.Common.DataColumnMapping[] {
     new System.Data.Common.DataColumnMapping("Expr1", "Expr1"),
     new System.Data.Common.DataColumnMapping("Expr2", "Expr2")})});
    // 
    // sqlSelectCommand1
    // 
    this.sqlSelectCommand1.CommandText = "SELECT au_id AS Expr1, au_lname AS Expr2 FROM authors";
    this.sqlSelectCommand1.Connection = this.sqlConnection1;
    // 
    // sqlConnection1
    // 
    this.sqlConnection1.ConnectionString = "workstation id=GW;packet size=4096;user id=sa;data source=GW;persist security inf" +
    "o=True;initial catalog=pubs;password=";
    // 
    // dataSet11
    // 
    this.dataSet11.DataSetName = "DataSet1";
    this.dataSet11.Locale = new System.Globalization.CultureInfo("zh-CN");
    this.DataGrid1.ItemDataBound += new System.Web.UI.WebControls.DataGridItemEventHandler(this.DataGrid1_ItemDataBound);
    this.Load += new System.EventHandler(this.Page_Load);
    ((System.ComponentModel.ISupportInitialize)(this.dataSet11)).EndInit(); }
    #endregion private void DataGrid1_ItemDataBound(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
    {
    if (e.Item.ItemType == ListItemType.Item ||
    e.Item.ItemType == ListItemType.AlternatingItem) 
    {
    CheckBox cb=(CheckBox)e.Item .FindControl ("check");

    cb.Attributes .Add("onclick","get_value(this)");


        
    }
    }

    }
    }
      

  12.   

    看看我的,我是 datalist中要删除,写成一个方法,只要调用就可以了,其他啥都不用管
    #region * -----ConfirmDel----- *
    private void ConfirmDel()
    {
    foreach(DataListItem Item in dbList.Items)
    {
    LinkButton lb = (LinkButton)Item.FindControl("LnkDelete");

    lb.Attributes["onclick"] = "return confirm('你是否确定删除')"; }
    }
    #endregion
      

  13.   

    其实最简单的往往是最好用的,你只要找到要用的控件,就是botton or linkbotton
    botton1.Attributes["onclick"] = "return confirm('你是否确定删除')";
    就行了。有句话叫keep it simple stupid
      

  14.   

    <%@ Page language="c#" Codebehind="WebForm2.aspx.cs" AutoEventWireup="false" Inherits="ReportTest.WebForm2" %>
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
    <HTML>
    <HEAD>
    <title>WebForm2</title>
    <meta name="GENERATOR" Content="Microsoft Visual Studio .NET 7.1">
    <meta name="CODE_LANGUAGE" Content="C#">
    <meta name="vs_defaultClientScript" content="JavaScript">
    <meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5">
    <script>
    function get_value(dataid)
     {
      var  s=document.getElementsByTagName("input")
      var  flag=false;
            for (i=0;i<s.length;i++)
            {
               if(s[i].type=="checkbox" &&  s[i].checked && s[i].id.substring (0,dataid.length)==dataid) //判断是否为checkbox 类型,判断是否被选中,判断是不是 datagrid 的 checkbox .
                                                                                                          //此处注意命名时不能以 datagrid 命名开头的  例如  datagrid id 为 abc, 就不能再命名以 abc 命名开头的了 比如命名为 abcd,abce,abcdefb,
               { 
                flag=true;
                 
               }
            }
            if (flag)
            return  window.confirm("删除吗");
             else
             {
             window.alert ("你没有选择数据")
             return false;
              }
      
     
        
            
      
     }
    </script>
    </HEAD>
    <body MS_POSITIONING="GridLayout">
    <form id="Form1" method="post" runat="server">
    <FONT face="宋体">
    <asp:DataGrid id="DataGrid1" style="Z-INDEX: 101; LEFT: 152px; POSITION: absolute; TOP: 24px"
    runat="server" Width="368px" Height="208px" DataSource="<%# dataSet11 %>" DataMember="authors" AutoGenerateColumns="False">
    <Columns>
    <asp:BoundColumn DataField="Expr1" SortExpression="Expr1" HeaderText="Expr1"></asp:BoundColumn>
    <asp:BoundColumn DataField="Expr2" SortExpression="Expr2" HeaderText="Expr2"></asp:BoundColumn>
    <asp:TemplateColumn>
    <ItemTemplate>
    <asp:CheckBox ID="check" Runat="server"></asp:CheckBox>
    </ItemTemplate>
    </asp:TemplateColumn>
    </Columns>
    </asp:DataGrid>
    <asp:Button id="Button1" style="Z-INDEX: 102; LEFT: 592px; POSITION: absolute; TOP: 120px" runat="server"
    Text="Button"></asp:Button></FONT>
    </form>
    </body>
    </HTML>using System;
    using System.Collections;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Web;
    using System.Web.SessionState;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.UI.HtmlControls;namespace ReportTest
    {
    /// <summary>
    /// WebForm2 的摘要说明。
    /// </summary>
    public class WebForm2 : System.Web.UI.Page
    {
    protected System.Web.UI.WebControls.DataGrid DataGrid1;
    protected System.Data.SqlClient.SqlDataAdapter sqlDataAdapter1;
    protected System.Data.SqlClient.SqlCommand sqlSelectCommand1;
    protected ReportTest.DataSet1 dataSet11;
    protected System.Web.UI.WebControls.Button Button1;
    protected System.Data.SqlClient.SqlConnection sqlConnection1;

    private void Page_Load(object sender, System.EventArgs e)
    {
    // 在此处放置用户代码以初始化页面
     this.sqlDataAdapter1.Fill (this.dataSet11 ); this.DataGrid1.DataSource=this.dataSet11.Tables[0];
     this.DataGrid1 .DataBind ();

    this.Button1.Attributes.Add ("onclick",
    "return get_value('"+this.DataGrid1.ID .ToString ()+"')"); } #region Web 窗体设计器生成的代码
    override protected void OnInit(EventArgs e)
    {
    //
    // CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
    //
    InitializeComponent();
    base.OnInit(e);
    }

    /// <summary>
    /// 设计器支持所需的方法 - 不要使用代码编辑器修改
    /// 此方法的内容。
    /// </summary>
    private void InitializeComponent()
    {    
    this.sqlDataAdapter1 = new System.Data.SqlClient.SqlDataAdapter();
    this.sqlSelectCommand1 = new System.Data.SqlClient.SqlCommand();
    this.sqlConnection1 = new System.Data.SqlClient.SqlConnection();
    this.dataSet11 = new ReportTest.DataSet1();
    ((System.ComponentModel.ISupportInitialize)(this.dataSet11)).BeginInit();
    // 
    // sqlDataAdapter1
    // 
    this.sqlDataAdapter1.SelectCommand = this.sqlSelectCommand1;
    this.sqlDataAdapter1.TableMappings.AddRange(new System.Data.Common.DataTableMapping[] {
      new System.Data.Common.DataTableMapping("Table", "authors", new System.Data.Common.DataColumnMapping[] {
     new System.Data.Common.DataColumnMapping("Expr1", "Expr1"),
     new System.Data.Common.DataColumnMapping("Expr2", "Expr2")})});
    // 
    // sqlSelectCommand1
    // 
    this.sqlSelectCommand1.CommandText = "SELECT au_id AS Expr1, au_lname AS Expr2 FROM authors";
    this.sqlSelectCommand1.Connection = this.sqlConnection1;
    // 
    // sqlConnection1
    // 
    this.sqlConnection1.ConnectionString = "workstation id=GW;packet size=4096;user id=sa;data source=GW;persist security inf" +
    "o=True;initial catalog=pubs;password=";
    // 
    // dataSet11
    // 
    this.dataSet11.DataSetName = "DataSet1";
    this.dataSet11.Locale = new System.Globalization.CultureInfo("zh-CN");
    this.DataGrid1.ItemDataBound += new System.Web.UI.WebControls.DataGridItemEventHandler(this.DataGrid1_ItemDataBound);
    this.DataGrid1.SelectedIndexChanged += new System.EventHandler(this.DataGrid1_SelectedIndexChanged);
    this.Button1.Click += new System.EventHandler(this.Button1_Click);
    this.Load += new System.EventHandler(this.Page_Load);
    ((System.ComponentModel.ISupportInitialize)(this.dataSet11)).EndInit(); }
    #endregion private void DataGrid1_ItemDataBound(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
    {

    } private void DataGrid1_SelectedIndexChanged(object sender, System.EventArgs e)
    {

    } private void Button1_Click(object sender, System.EventArgs e)
    {

    }

    }
    }
      

  15.   

    上边是 
    注意一下 javascript  语句 按你原来的要求写的,可以直接运行..改一下 sql 联接 .....
      

  16.   

    各位大哥.我还是把贴子给结了吧.现在还是搞不定.所以放弃不搞了.可是心里还是不服.
    楼上在"深爱一人叫颖的女孩"如果你有时间的话.我可以加你QQ和你简单的聊一下可以吗?
    我的QQ是:107424627
      

  17.   

    深爱一人叫颖的女孩的方法是正确的,你早说清楚嘛,其实就是遍历一下Checkbox,看看有没有选中,如果没有就弹出提示窗口罗。