Adding a Javascript Confirm Alert to a .NET Checkbox control in a GridView

One issue I recently ran into dealing with the gridview control in .NET is adding a javascript confirm box when a user selects a checkbox control and have a postback event fired. The dopostback event is tied to the onclick event, so I was unable to add it to the template item itself. I ended up having to manually call the dopostback myself with the javascript below.

function confirmSubmit(mycheckbox) {
 var agree=confirm("Are you sure you want to delete this quote?");
 if (agree) {
 setTimeout('__doPostBack(\'gvQuotes$ctl07$btnDelete\',\'\')', 0)
 return true ;
 }
 else {
  mycheckbox.checked = false;
  return false ;
  }
}

Here is the itemtemplate code that fires the javascript code:

<asp:TemplateField HeaderText="delete">
<ItemTemplate>
<asp:CheckBox ID="btnDelete" runat="server" Text="" 
onclick="confirmSubmit(this)"  
OnCheckedChanged="FireDeleteQuote"  />
<ItemStyle HorizontalAlign="Center" Width="0px" />
</ItemTemplate>
</asp:TemplateField>

If anyone has found an easier way to accomplish this let me know.



Adding code for checkbox control is a good idea. Coding makes everything so much more convenient, from bar code scanners to helpful web code. A symbol scanner can also simplify business processes by reading code. Partnered with a touch screen monitor these barcode readers and scanners become the ultimate in convenience.

Tim

A .NET, PHP, Marketing Guru authority, at least I hope I am. Reach me at tboland@gmail.com

4 thoughts on “Adding a Javascript Confirm Alert to a .NET Checkbox control in a GridView

  1. [...] Adding a Javascript Confirm Alert to a .NET Checkbox control in a … NET is adding a javascript confirm box when a user selects a checkbox control and have a postback event fired. The dopostback event is tied to the … var agree=confirm(“Are you sure you want to delete this quote?”); if (agree) { …Read more: here [...]

  2. fallenrogue says:

    I think I’ve got an easier way! Don’t validate the checkbox at all. Instead, validate the form submission, which is what you’re wanting to validate, right? So, just drop the following on your form tag…

    onsubmit=”return confirm(‘are you sure you want to delete this item?’);”

    and you’re in business. Of couse, you could use a function to look and see if it’s coming from the right control and all that jazz, this is just to illustrate my point. If you’re using a master page, then just use addEventListener or attachEvent on the form. I think that would be a lot cleaner and easier to troubleshoot than manually calling postbacks. That’s just my two cents. :) Thanks!

  3. Dinesh Verma says:

    Same problem is facing …..
    If anyone have solution then give me response.

  4. No work will not have a normal life. I know what labor; labor is all joy and all the beautiful things in the world.

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>