views:

29

answers:

1

I'm adding alot of buttons at runtime to my form. When i push these buttons i want a confirm window to pop up and ask if i'm sure to delete this item.

How would i go by doing this. Every button is added dynamically so i expect that the confirmbuttonextender should also be added that way. Do i have to add a extender for each button or should i make one in the aspx file then change the targetID at runtime ?

UPDATE

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);
+2  A: 

have you tried a simple javascript?

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

You would add this when adding the Button like this:

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

Alternatively, you could use JQuery:

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

Then on the client side, add the following javascript (having also added the jquery library of course).

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

If you want to get real clever and include more info...

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
       }
   });
});

UPDATE: Change the background image like this...

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

Stylesheet

.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"); 
   });
});
Daniel Dyson
I'm looking for something easy. The javascript did the trick but can i customize it ? Change Icon, the text on the buttons and so on ?
eski
Yes, JQuery allows you to do that easily: You can set any attribute by using the .attr("attributeName", "valueToSetItTo"); technique... http://api.jquery.com/attr/
Daniel Dyson
Post a snippet of the rendered button and I will change the JQuery above to show you.
Daniel Dyson
see updated question.
eski
Is it just the Eyda button that you want the behavioiur on or also the links?
Daniel Dyson
The beauty of the JQuery approach is that you have a very small amount of javascript, but it gets applied to every item identified in the selector (in this case '.confirm'). You don't have to go wiring up inline scripts and extenders for everything. Then you can build on what happens in the event handler, like in my last example.
Daniel Dyson
Yeah, its just the Eyda button. The snippet is in a foreach loop so there are couple of them. So i have to have them delete right object from the database. I have never used Jquery before. Should i install some library so this will work and also the javascript, i should put it in a <script language="javascript" type="text/javascript"> tag right ?
eski
Correct. You can download the JQuery script from here: http://docs.jquery.com/Downloading_jQuery I suggest you take a good look at the examples. I have been writing Javascript for years and I am very excited about what is acheiveable with JQuery.
Daniel Dyson
There seems to be some problem with VS2008 and the jquery library. I have installed the hotfix but VS2008 complains that it cant update the intellisense for the library, any thoughts ?
eski
http://weblogs.asp.net/scottgu/archive/2008/11/21/jquery-intellisense-in-vs-2008.aspx I know, this seems like a pain to implement, just to add some simple javascript functionality. However, bear in mind that Microsoft are including JQuery by default in its ASP.NET MVC templates and they are actively supporting it. If Scott Guthrie is blogging about it, it is the future. This article might help you to explore how to improve the Intellisense experience.
Daniel Dyson