0

I am using the below code to create a button in code behind and also create confirm message box for that button.But its not working please help me to create this..

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
}
1
  • 2
    How does the button know where it belongs to? Commented Aug 9, 2012 at 5:41

5 Answers 5

1

You can also use below code:-

btn2.Attributes["onClick"] = "return confirm('Are you sure you want to add this   estimate data??');"
Sign up to request clarification or add additional context in comments.

Comments

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

Comments

1

You can use below code:

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

Comments

1

you can add ajaxcotoroll toolkit reference to your website and dynamically create confirm for every button

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??'";

8 Comments

urgh AjaxControlToolkit! nasty
It is the lazy way for a .net developer to do Client side UI.
@Tim B James whaaaatttt???? there is a reason why ajaxtoolkit is supported to be created from server side, it a lot better than writing a JavaScript on server side, it a lot simpler and cleaner to control, and what you consider a good way ????
I wouldn't do any server side onclick stuff. I would assign the button a data- attribute to hold the text, give it a class confirm-btn, and then keep all the confirm/onclick on the client side. Need another confirm button? just give it a data- attribute and class. Need to add a button client side? just give is a data- attribute and class. Never need to recompile anything if you want to change the client ui.
what if it dynamic and every message is dynamic in the alert and you do not know how many button are going to be generated if you give them all the same class they will most likely implement the same confirm message and if you are using and old framework like 2.0 I am not sure if it even supports attributes and if it going to render in HTML5
|
0

Try this 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??'";

This is using Ajaxcontroll call..

OR

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.