Donate

Invalid postback or callback argument in ASP.NET (_doPostBack in jQuery UI Confirmation Dialog)

Hello,

I'm currently working with ASP.NET GridView control that shows a confirmation dialog using jQuery UI instead of the classic JavaScript confirm dialog that prompts a user to delete a certain record. However, integrating that with the GridView control was slightly tricky in the sense that I was stuck with Invalid postback or callback argument error. Since this is ASP.NET Webforms, you have to deal with Postback hell issues. :)
jQuery UI Dialog that trigger's postback
buttons: {
 "Yes": function () {
  __doPostBack(btnUniqueID, '');
  $(this).dialog("close");
 },
 "No": function () {
  $(this).dialog("close");
  return false;
 }
}

After doing some research, I came up with two solutions. The first one is to set EnableEventValidation of Page directive to false for page level effect. Or you can set it in web.config for project scope.
Page Directive Solution
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="GridViewjQueryUI.Default" EnableEventValidation="false" %>
However, the solution on setting EventValidation will also affect other settings and code in your webform page. The second solution, will isolate the problem by registering the UniqueID of a control that will trigger the PostBack through ClientScript.RegisterForEventValidation() method. This is the preferred one.
PagePage.ClientScript.RegisterForEventValidation Solution
protected override void Render(HtmlTextWriter writer)
{
 foreach (GridViewRow r in gvCustomers.Rows)
 {
  if (r.RowType == DataControlRowType.DataRow)
  {
   LinkButton lbtnSelect = (LinkButton)r.FindControl("lbtnDelete");
   Page.ClientScript.RegisterForEventValidation(lbtnSelect.UniqueID, "Select$" + r.RowIndex);
  }
 }            

 base.Render(writer);
}

Cheers! :)

Comments

Donate

Popular Posts From This Blog

WPF CRUD Application Using DataGrid, MVVM Pattern, Entity Framework, And C#.NET

How To Insert Or Add Emojis In Microsoft Teams Status Message

TypeScript Error Or Bug: The term 'tsc' is not recognized as the name of a cmdlet, function, script file, or operable program.