“Invalid postback or callback argument” DropDownList and ClientSide Append

I feel this particular issue really deserves a blog post due to the sheer number of bad answers that are out there that don’t really address or explain the underlying issue.

Scenario: Let’s say you have an asp:DropDownList control that you populate with three values in your Page_Load event:

<asp:DropDownList ID="rcbStatus" ToolTip="Status" runat="server" Width="200px">
    <asp:ListItem Value="1" Text="A"></asp:ListItem>
    <asp:ListItem Value="2" Text="B"></asp:ListItem>
    <asp:ListItem Value="3" Text="C"></asp:ListItem>
</asp:DropDownList>

which renders as:

<select>
    <option value="1">A</option>
    <option value="2">B</option>
    <option value="3">C</option>
</select>

Now let’s say you want to manipulate the above using client side javascript and perhaps alter/append or remove options from the above select based on some other selection. So we end up with a select control that looks like:

$("#<%= rcbStatus.ClientID %>").append($('<option></option>').val('4').html('D'));
<select>
    <option value="1">A</option>
    <option value="2">B</option>
    <option value="3">C</option>
    <option value="4">D</option>
</select>

Now go ahead and select option “D” and attempt a submit, you will receive an error as follows (if EnableEventValidation is true, which is the default):

Invalid postback or callback argument. Event validation is enabled using in configuration or <%@ Page EnableEventValidation="true" %> in a page. For security purposes, this feature verifies that arguments to postback or callback events originate from the server control that originally rendered them. If the data is valid and expected, use the ClientScriptManager.RegisterForEventValidation method in order to register the postback or callback data for validation.

Now sites like stackoverflow and several other blogs outline the following as potential fixes for this error:

  • Set EnableEventValidation to false and of course the above submit will work. The reasoning is that you shouldn’t be relying on EventValidation to ensure that input from the user/page is what you expect and is valid input.
  • Use an AJAX Update Panel or a Telerik RadUpdatePanel to handle the change client side.
  • Use a select control natively, instead of an asp:DropDownList and use Request.Form to access it’s selected value on submit.
  • Or finally, do what the error is suggesting and register the possible range of values using ClientScript.RegisterForEventValidation.

I’m going to demonstrate the last option there, as it’s the one that makes the most sense for my particular scenario. Add an override for the Render method in your server side code as follows:

protected override void Render(HtmlTextWriter writer)
{
    Page.ClientScript.RegisterForEventValidation(rcbStatus.UniqueID, "4");
    base.Render(writer);
}

Now for option D in my select, with a value of 4, the EventValidation is aware that 4 is a valid and expected result and the submit will occur without issue. So basically, we’re telling ASP.NET what is valid and to continue on if you encounter these entries that did not exist in the original state of the drop down list when it was rendered.

Please note that specifying “D” as the second parameter in the RegisterForEventValidation method did not work for me, it had to be the option value.

14 Responses to ““Invalid postback or callback argument” DropDownList and ClientSide Append”

  1. Daniel Walker October 13, 2011 at 8:21 am #

    This is great! Is it possible to take this further and perform the following: Have a lookup field in a form that has some values. Using javascript code, update the list where the lookup is pulling from while still on the new item form to add a new value that was not there. When complete then add the new value to the dropdown and allow it to be recognized as valid when posting the form. Does this seem possible? It should be! This is a SharePoint NewForm page.

  2. shereen October 13, 2011 at 9:39 am #

    Hi Daniel,

    That does sound do-able. As long as you ensure you allow it to be recognized on post back, it should be ok. The issue really comes down to your initial load data being different than what it’s posting back as. Did I answer your question?

  3. Daniel Walker October 13, 2011 at 9:49 am #

    Mainly except I am not sure how to do it! Assuming that I am not able to use Visual Studio and I only have SharePoint designer available I am getting stuck passing the updated value in a way for SharePoint to recognize that it is valid. I am not sure if the postback submits the data before I can re-add the option to the dropdown on postback!

  4. SC Vinod March 6, 2012 at 11:33 pm #

    Daniel,

    I’m having the same issue. Did you get a solution for this?

  5. pushpopped September 26, 2012 at 4:09 am #

    Hi shereen,

    I am having a similar problem. I am adding items from one listbox to another using javascript. When I do not select any item on the final listbox and submit it it works fine. But if i select one or more item in the final list box it throws the exception you have mentioned. I tried to add the render method for the entire listbox:
    protected override void Render(HtmlTextWriter writer)
    {

    Page.ClientScript.RegisterForEventValidation(lsbxFinalCandidate.UniqueID);
    base.Render(writer);
    }
    But it is still throwing the error. Pls help

  6. shereen October 19, 2012 at 11:21 am #

    Hi pushpopped,

    I think what you need to do is register those values in your second listbox. I don’t think you can simply register the UniqueID of the list box without supplying the values. ASP.net needs to be aware of the each new entry individually. So what does the html for your listbox look like?

    You should have several select options, each with a different value. You will need to register the ones that didn’t exist on initial page load. Or in other words, the ones you added using javascript.

    Does this help? If you send me your initial page load html with the modified list box html after you added some items via javascript, I can help you with what you need to register.

  7. Ron Picard December 10, 2013 at 1:26 pm #

    This works if you add, but if you remove items I’m getting the error. Do I have to load a blank dropdown and add all values in Javascript?

  8. Shereen Qumsieh December 10, 2013 at 3:09 pm #

    Hi Ron,
    It should still work…as long as the item you’ve selected has been registered. You shouldn’t have to load a blank drop down, no. So if you’re saying that Add works but Remove does not, I wonder if there is a way to ‘unregister’ that option or to reset the registration for that drop down. There’s where I’d start.

  9. Leo August 18, 2014 at 11:04 pm #

    Hallo, maybe this issue is older but finally i face to face with me:
    I’ve nine combobox ,every combo each depends another , when i change the value combobox with javascript and ajax everything ok, when i click a button that submitted the form ,thrown error this error “Invalid postback or callback argument … ”

    do i have all values (about 30 by combo in render method)?

    that’s right?

  10. Shereen Qumsieh September 11, 2014 at 4:26 am #

    Yes you have to add all possible values to avoid this error.

  11. Avinash December 10, 2014 at 9:50 pm #

    I have 2 lists: 1) Country 2) States, country has single line of text column
    State has a single line of text and a lookup column(looking up from Country list–> single line of text)
    So when i choose USA all states that have lookup country as USA need to be populated.
    I am populating it using Javascript(JSOM- Client context) by adding options to the select control on client side(ascx page).
    When i try to submit its not saving the state information even if i add the states ids as follows in my ascx.cs file
    Page.ClientScript.RegisterForEventValidation(ddl_bind.UniqueID, “0”);
    Page.ClientScript.RegisterForEventValidation(ddl_bind.UniqueID, “300”);
    Page.ClientScript.RegisterForEventValidation(ddl_bind.UniqueID, “301”);
    Page.ClientScript.RegisterForEventValidation(ddl_bind.UniqueID, “302”);
    Page.ClientScript.RegisterForEventValidation(ddl_bind.UniqueID, “303”);

    What is the issue?

  12. sacchit July 9, 2015 at 3:06 pm #

    Hi Shereen,
    Thanks for the blog .You have explained it very clearly. that really helped.
    Especially about passing the value that clicked for me :)

  13. sacchit July 9, 2015 at 3:10 pm #

    Great article for programmers writing code without understanding the life cycle

Trackbacks/Pingbacks

  1. Problem with Grid View and the dreaded "Invalid Postback or Callback" exception | ASP Questions & Answers - July 5, 2014

    […] http://blog.qumsieh.ca/2011/03/22/invalid-postback-or-callback-argument-dropdownlist-and-clientside-&#8230; […]

Leave a Reply