简体   繁体   English

在Asp.Net页面上单击“删除”按钮时创建确认消息

[英]Creating Confirmation message when a Delete button clicked on Asp.Net Page

i use this codes for a Delete button for a confirmation message : 我使用此代码作为确认消息的删除按钮:

<asp:Button runat="server" ID="btnDelete" 
 OnClick="btnDelete_Click" 
 OnClientClick="return confirm('Do you want to delete the record ? ');" />

Here is the way to add confirm client script at server side : 以下是在服务器端添加确认客户端脚本的方法:

btnDelete.Attributes.Add("onclick", 
    "return confirm('Do you want to delete the record ? ');")

but I'm getting the (Type Expected) error when debugging could anyone help please? 但是我在调​​试时遇到(预期的类型)错误,任何人都可以帮忙吗? Thanks 谢谢

If you already assigned OnClientClick="return confirm('Do you want to delete the record ? ');" 如果您已经分配了 OnClientClick="return confirm('Do you want to delete the record ? ');" in your markup you don't need add an attribute again. 在您的标记中,您不需要再次添加属性。 It may be the reason. 可能是原因。

Comment out from server 从服务器注释掉

//btnDelete.Attributes.Add("onclick", 
    "return confirm('Do you want to delete the record ? ');")

The client side markup you have is correct and must work. 您拥有的客户端标记是正确的,必须正常工作。

If you want to generate the JavaScript code from server side, then remove the related markup OnClientClick. 如果要从服务器端生成JavaScript代码,请删除相关标记OnClientClick。 You're duplicating client event handling. 您正在复制客户端事件处理。

Note: Instead of "onclick" attribute, consider using event attaching on page load (ie: using jquery). 注意:考虑在页面加载时使用事件附加(即:使用jquery),而不是“onclick”属性。

Example: 例:

$(".btn-delete").click(function() { /*do something here*/ });

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e) protected void GridView1_RowDataBound(object sender,GridViewRowEventArgs e)

    {
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                foreach (Button button in e.Row.Cells[12].Controls.OfType<Button>())

                {
                    if (button.CommandName == "Delete")
                    {
                        button.Attributes["onclick"] = "if(!confirm('Do you want to delete')){return false;};";
                    }

                }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM