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 2 comments.
Read more articles on asp.net.