使用C#中的Code behind在按钮单击时创建Javascript警报

时间:2012-08-09 05:36:37

标签: c# javascript asp.net button

我使用下面的代码在后面的代码中创建一个按钮,并为该按钮创建确认消息框。但是它不起作用请帮我创建这个..

Button btn2 = new Button();
btn2.Attributes.Add(
    "OnClientClick", 
    "if (confirm(''Are you sure you want to add this estimate data?')) return;");

if(true) 
{
    //Do something
}

5 个答案:

答案 0 :(得分:1)

btn2.Attributes.Add("onclick", "javascript:return confirm('Are you sure you want to add this    estimate data??')"); 

答案 1 :(得分:1)

您可以使用以下代码:

btn2.Attributes.Add("onclick", "return confirm('Are you sure you want to add this estimate data?');")

答案 2 :(得分:1)

您可以将ajaxcotoroll工具包引用添加到您的网站,并为每个按钮动态创建确认

Button btn2 = new Button();
btn.ID="buttonID";
ConfirmButtonExtender confirm = new ConfirmButtonExtender();
confirm.TargetControlID = "buttonID";
confirm.ConfirmText="Are you sure you want to add this    estimate data??'";

答案 3 :(得分:1)

您还可以使用以下代码: -

btn2.Attributes["onClick"] = "return confirm('Are you sure you want to add this   estimate data??');"

答案 4 :(得分:0)

试试这个Cod:

 Button yourbtnid = new Button();
 yourbtnid.ID="Button1";
 ConfirmButtonExtender conf = new ConfirmButtonExtender();
 conf.TargetControlID = "Button1";
 conf.ConfirmText="Are you sure you want to add this    estimate data??'";

这是使用Ajaxcontroll调用..

OR

Button yourbtnid = new Button();
yourbtnid.Attributes.Add(
     "OnClientClick", 
     "return confirm('Are you sure you want to add this    estimate data??');");