Tag Archives: jQuery

Neat confirmation using jQuery

I was looking for ways to do some simple yes/no client-side confirmation and found a nice plugin using jQuery. I am a total noob when it comes to jQuery (in fact this is the first thing I have ever done) so with a little help from a collegue I got it working. Its pretty simple. All you need to do is:

  1. Download the plugin. It is a javascript file.
  2. Import the javascript file to your page.
  3. Write the function which is to be run.

I’m sure some of the code in my example is very specific to my environment. But I know way to little to make a well-explained generic post about it.

My button which will execute the script looks like this:

<asp:Button ID="ButtonDeny" runat="server" Text="Godkänn" OnClick="ButtonDeny_Click" />

Import the plugin to your page like this:

<script type="text/javascript" src="/_layouts/WebParts/jquery.confirm-1.3.js"></script>

I put the jQuery function inside a pageLoad for it to get the correct client-side ID of my button:
NOTE: This pageLoad function is put inside the script tags of my ascx or aspx page.

function pageLoad(sender, args) {

        $('#<%=myButton.ClientID%>').confirm({
            msg: 'Are you sure you want to do this??',
            separator: ' / '
        });
    }

This will prevent the postback and the server-side method being called when I click the button, unless I choose ‘Yes’ when asked. Very neat indeed. I found this plugin here.

Advertisements