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

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

  1. 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!

  2. How to install IMO on Pc free, Download IMO for Pc Windows 7/8 and 10 and Enjoy this Amazing IMO app.100% safe and virus free. More than 12454 downloads this month. Download BlueStacks App.

Leave a Reply

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