按钮和ConfirmButtonExtender动态添加

时间:2010-08-11 12:03:48

标签: c# asp.net jquery ajax

我在运行时向我的表单添加了很多按钮。当我按下这些按钮时,我想要一个确认窗口弹出并询问我是否确定要删除此项目。

我将如何做到这一点。 每个按钮都是动态添加的,所以我希望还能以这种方式添加确认按钮。 我是否必须为每个按钮添加扩展器,或者我应该在aspx文件中创建一个扩展器然后在运行时更改targetID?

更新

Panel div = new Panel();
div.CssClass = "BulletDiv";
content.Controls.Add(div);

HyperLink picture = new HyperLink();
picture.ImageUrl = "~/Icons/Ny_mappa.png";
picture.NavigateUrl = "?navID=" + item.Nav_ID + "&ddlID=" + ddlID;
div.Controls.Add(picture);

div.Controls.Add(new LiteralControl("<br/>"));

HyperLink description = new HyperLink();
description.CssClass = "BulletDiv_Text";
description.Text = item.Nav_Name;
description.ID = item.Nav_ID.ToString();
description.NavigateUrl = "?navID=" + item.Nav_ID + "&ddlID=" + ddlID;
div.Controls.Add(description);

Button eyda = new Button();
eyda.Text = "Eyda";
eyda.CommandArgument = item.Nav_ID.ToString();
eyda.Command += new CommandEventHandler(eyda_Command);
div.Controls.Add(eyda);

1 个答案:

答案 0 :(得分:2)

你试过一个简单的javascript吗?

onclick='return confirm("Are you sure you would like to delete this item?");'

您可以在添加按钮时添加此项:

Button edya = new Button();
edya.Attributes.Add("onclick","return confirm(\"Are you sure you would like to delete this item?\");");
//... set other fields and add to page

或者,您可以使用 JQuery

eyda.CssClass = "confirm";  //When adding button on server side

然后在客户端添加以下javascript(当然也添加了jquery库)。

$(document).ready(function() {
   $('.confirm').click(function() {
       if (!confirm("Are you sure etc?")) {
           return false; //prevent button event propogating
       }
   });
});

如果你想真正聪明并包含更多信息......

eyda.CssClass = "confirm";  //When adding button on server side
eyda.Attributes.Add("SomeProperty","Some value, specific to this button");


  $(document).ready(function() {
   $('.confirm').click(function() {
       var someValue= $(this).attr("SomeProperty");

       if (!confirm("Are you sure you want to delete " + someValue)) {
           return false; //prevent button event propogating
       }
   });
});

更新:更改背景图片......

eyda.CssClass = "confirm";  //When adding button on server side

样式表

.confirm {
    background-image: url('Images/ClickMe.gif');
    background-repeat:no-repeat;
}
.clicked {
    background-image: url('Images/Clicked.gif');
}

使用Javascript:

$(document).ready(function() {
   $('.confirm').click(function() {
       if (!confirm("Are you sure etc?")) {
           $(this).addClass("clicked");
           return false; //prevent button event propogating
       }
       //Not meaningful to change style here as a postback is about to occur... 
       //$(this).addClass("clicked"); 
   });
});