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.

Written by Tim on November 28th, 2006 with 4 comments.
Read more articles on asp.net.

Related articles

4 comments

Read the comments left by other users below, or:

Trackback Mention from Confirm.pierresccservice.com
#1. November 29th, 2006, at 9:48 AM.

Pierres Service » Blog Archive » Adding a Javascript Confirm Alert to a .NET Checkbox control in a …: Adding a Javascript Confirm Alert to a .NET Checkbox control in a … NET is adding ...

Get your own gravatar by visiting gravatar.com fallenrogue
#2. November 30th, 2006, at 10:28 PM.

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!

Get your own gravatar by visiting gravatar.com Dinesh Verma
#3. November 19th, 2008, at 5:14 AM.

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

Get your own gravatar by visiting gravatar.com Patent Attorneys
#4. April 30th, 2013, at 9:28 AM.

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 your comment...

If you want to leave your comment on this article, simply fill out the next form:




You can use these XHTML tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> .