javascript checkbox删除提示

本文介绍了一个ASP.NET网页中实现删除确认功能的方法。通过JavaScript函数`DeleteAlert()`来检查表格中的复选框是否被选中,并在用户尝试删除记录之前显示警告提示。如果未选择任何记录,则会弹出提示信息。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

aspx:
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Untitled Page</title>
    <script language="javascript">
     function DeleteAlert()
        {
            var gvtable = document.getElementById('GridView1');
            var counter = 0;
            for (var i = 1; i < gvtable.rows.length; i++)
            {
                var cbx = gvtable.rows(i).cells(0).children(0);
                if (cbx.type == 'checkbox' && cbx.checked == true)
                {
                    counter++;
                }
            }
            if (counter > 0)
            {
                return confirm('Are you sure you want to delete selected records Y/N?');
            }
            else
            {
                window.alert('test');
                return false;
            }
        }
          </script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
     <asp:LinkButton ID="Lb_Delete" runat="server" Font-Underline="False"

OnClientClick="return DeleteAlert();" OnClick="Lb_Delete_Click">Delete</asp:LinkButton>
     <asp:GridView ID="GridView1" runat="server" DataKeyNames="ID">
                        <Columns>
                            <asp:BoundField DataField="ID" HeaderText="ID"

SortExpression="ID">
                                                <ItemStyle HorizontalAlign="Left" />
                                            </asp:BoundField>     
                            <asp:TemplateField HeaderText="Select">
                                <ItemTemplate>
                                    <asp:CheckBox ID="cbSelect" runat="server" />  
                                </ItemTemplate>
                                <ItemStyle HorizontalAlign="Left" />
                                <HeaderStyle HorizontalAlign="Center" />
                            </asp:TemplateField>
                        </Columns>
                       </asp:GridView>
                   
    </div>
    </form>
</body>
</html>

cs:
 protected void Lb_Delete_Click(object sender, EventArgs e)
    {
        string strSQL = "", strPrimaryKeys = String.Empty;
        foreach (GridViewRow r in GridView1.Rows)
        {
            CheckBox cbx = (CheckBox)r.FindControl("cbSelect");
            if (cbx.Checked)
            {
                strPrimaryKeys += Convert.ToInt32(GridView1.DataKeys

[r.RowIndex].Value).ToString() + ",";
            }
        }
       // GridViewRow gvr = (GridViewRow)((LinkButton)sender).NamingContainer;
      //  string id = Convert.ToInt32(GridView1.DataKeys[gvr.RowIndex].Value).ToString();
      // string id=DataBinder.Eval(e.Row.DataItem, "ID");
      
    }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值