Postback Issues

Discussion in 'ASP .Net' started by Mike, Oct 18, 2007.

  1. Mike

    Mike Guest

    I'm having some problems with "Invalid postback or callback argument" on an
    ASP.NET page. It appears to only happen on Firefox browsers, but I could be
    wrong about that.

    At any rate, the exception is raised when the user clicks on a postback link
    on the page before the page has completely loaded. Some of these pages are
    rather long, so the chances of that happening is increased. And these pages
    have a Repeater control and each line on the Repeater contains a postback
    link that is built 'on the fly'.

    Is there someway to prevent this exception or handle it more gracefully?
    For instance, is it possible to disable postbacks until the page has
    completed loading? Is there a best practice for trapping the exception?

    Thanks,

    Mike
    Mike, Oct 18, 2007
    #1
    1. Advertising

  2. Hi Mike,

    Welcome to MSDN Managed Newsgroup.

    This is a known issue of ASP.NET postback with event validation enabled,
    and this is not directly related to Firefox. Let me explain the root cause
    the possible workarounds.

    First, let me explain what is event validation in ASP.NET: this is a
    security feature that ensures the postback actions only come from events
    allowed and created by the server to help prevent spoofed postbacks. This
    feature is implemented by having controls register valid events when they
    render (as in, during their actual Render() methods). The end result is
    that at the bottom of your rendered <form> tag, you'll see something like
    this: <input type="hidden" name="__EVENTVALIDATION" id="__EVENTVALIDATION"
    value="..." />. When a postback occurs, ASP.NET uses the values stored in
    this hidden field to ensure that the button you clicked invokes a valid
    event. If it's not valid, you get the exception that you've been seeing.

    The problem happens specifically when you postback before the
    EventValidation field
    has been rendered (i.e., before the page is fully loaded). If
    EventValidation is enabled (which it is, by default), but ASP.net doesn't
    see the hidden field when you postback, you also get the exception. If you
    submit a form before it has been entirely rendered, then chances are the
    EventValidation field has not yet been rendered, and thus ASP.net cannot
    validate your click.

    There are many workarounds for the above mentioned problem, one of them is
    that disabling postback until the page is fully loaded as you mentioned:

    1. Set enableEventValidation to false:

    <pages enableEventValidation="false" ...

    2. One way around the problem is to mark the form as disabled and then
    enable it in script once the load is complete.
    Snippet 1:
    ========
    function enableForm() {
    document.getElementById("form").disabled = false;
    }
    window.onLoad = enableForm();

    Snippet 2:
    ========
    function disableElements(elements) {
    for (var i = elements.length - 1; i >= 0; i--) {
    var elmt = elements;
    if (!elmt.disabled) {
    elmt.disabled = true;
    }
    else {
    elmt._wasDisabled = true;
    }
    }
    }
    function disableFormElements() {
    disableElements(_form.getElementsByTagName("INPUT"));
    disableElements(_form.getElementsByTagName("SELECT"));
    disableElements(_form.getElementsByTagName("TEXTAREA"));
    disableElements(_form.getElementsByTagName("BUTTON"));
    disableElements(_form.getElementsByTagName("A"));
    }
    function enableElements(elements) {
    for (var i = elements.length - 1; i >= 0; i--) {
    var elmt = elements;
    if (!elmt._wasDisabled) {
    elmt.disabled = false;
    }
    else {
    elmt._wasDisabled = null;
    }
    }
    }
    function enableFormElements() {
    enableElements(_form.getElementsByTagName("INPUT"));
    enableElements(_form.getElementsByTagName("SELECT"));
    enableElements(_form.getElementsByTagName("TEXTAREA"));
    enableElements(_form.getElementsByTagName("BUTTON"));
    enableElements(_form.getElementsByTagName("A"));
    }
    Make sure that the variable _form is set to the ASP.net form on the page.
    Then just call enableFormElements() and disableFormElements().


    Hope this helps. Please feel free to let me know if there's anything else I
    can help. Thanks.



    Sincerely,
    Walter Wang (, remove 'online.')
    Microsoft Online Community Support

    ==================================================
    For MSDN subscribers whose posts are left unanswered, please check this
    document: http://blogs.msdn.com/msdnts/pages/postingAlias.aspx

    Get notification to my posts through email? Please refer to
    http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
    ications. If you are using Outlook Express/Windows Mail, please make sure
    you clear the check box "Tools/Options/Read: Get 300 headers at a time" to
    see your reply promptly.

    Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
    where an initial response from the community or a Microsoft Support
    Engineer within 1 business day is acceptable. Please note that each follow
    up response may take approximately 2 business days as the support
    professional working with you may need further investigation to reach the
    most efficient resolution. The offering is not appropriate for situations
    that require urgent, real-time or phone-based interactions or complex
    project analysis and dump analysis issues. Issues of this nature are best
    handled working with a dedicated Microsoft Support Engineer by contacting
    Microsoft Customer Support Services (CSS) at
    http://msdn.microsoft.com/subscriptions/support/default.aspx.
    ==================================================

    This posting is provided "AS IS" with no warranties, and confers no rights.
    Walter Wang [MSFT], Oct 19, 2007
    #2
    1. Advertising

  3. Mike

    Mike Guest

    Walter,

    Thanks for the explanation.

    I would like to avoid the security issues raised by workaround 1. Could you
    point me to a detailed example of using script code to enable and disable
    the form?

    Mike


    On 10/19/07 12:00 AM, in article ,
    ""Walter Wang [MSFT]"" <> wrote:

    > Hi Mike,
    >
    > Welcome to MSDN Managed Newsgroup.
    >
    > This is a known issue of ASP.NET postback with event validation enabled,
    > and this is not directly related to Firefox. Let me explain the root cause
    > the possible workarounds.
    >
    > First, let me explain what is event validation in ASP.NET: this is a
    > security feature that ensures the postback actions only come from events
    > allowed and created by the server to help prevent spoofed postbacks. This
    > feature is implemented by having controls register valid events when they
    > render (as in, during their actual Render() methods). The end result is
    > that at the bottom of your rendered <form> tag, you'll see something like
    > this: <input type="hidden" name="__EVENTVALIDATION" id="__EVENTVALIDATION"
    > value="..." />. When a postback occurs, ASP.NET uses the values stored in
    > this hidden field to ensure that the button you clicked invokes a valid
    > event. If it's not valid, you get the exception that you've been seeing.
    >
    > The problem happens specifically when you postback before the
    > EventValidation field
    > has been rendered (i.e., before the page is fully loaded). If
    > EventValidation is enabled (which it is, by default), but ASP.net doesn't
    > see the hidden field when you postback, you also get the exception. If you
    > submit a form before it has been entirely rendered, then chances are the
    > EventValidation field has not yet been rendered, and thus ASP.net cannot
    > validate your click.
    >
    > There are many workarounds for the above mentioned problem, one of them is
    > that disabling postback until the page is fully loaded as you mentioned:
    >
    > 1. Set enableEventValidation to false:
    >
    > <pages enableEventValidation="false" ...
    >
    > 2. One way around the problem is to mark the form as disabled and then
    > enable it in script once the load is complete.
    > Snippet 1:
    > ========
    > function enableForm() {
    > document.getElementById("form").disabled = false;
    > }
    > window.onLoad = enableForm();
    >
    > Snippet 2:
    > ========
    > function disableElements(elements) {
    > for (var i = elements.length - 1; i >= 0; i--) {
    > var elmt = elements;
    > if (!elmt.disabled) {
    > elmt.disabled = true;
    > }
    > else {
    > elmt._wasDisabled = true;
    > }
    > }
    > }
    > function disableFormElements() {
    > disableElements(_form.getElementsByTagName("INPUT"));
    > disableElements(_form.getElementsByTagName("SELECT"));
    > disableElements(_form.getElementsByTagName("TEXTAREA"));
    > disableElements(_form.getElementsByTagName("BUTTON"));
    > disableElements(_form.getElementsByTagName("A"));
    > }
    > function enableElements(elements) {
    > for (var i = elements.length - 1; i >= 0; i--) {
    > var elmt = elements;
    > if (!elmt._wasDisabled) {
    > elmt.disabled = false;
    > }
    > else {
    > elmt._wasDisabled = null;
    > }
    > }
    > }
    > function enableFormElements() {
    > enableElements(_form.getElementsByTagName("INPUT"));
    > enableElements(_form.getElementsByTagName("SELECT"));
    > enableElements(_form.getElementsByTagName("TEXTAREA"));
    > enableElements(_form.getElementsByTagName("BUTTON"));
    > enableElements(_form.getElementsByTagName("A"));
    > }
    > Make sure that the variable _form is set to the ASP.net form on the page.
    > Then just call enableFormElements() and disableFormElements().
    >
    >
    > Hope this helps. Please feel free to let me know if there's anything else I
    > can help. Thanks.
    >
    >
    >
    > Sincerely,
    > Walter Wang (, remove 'online.')
    > Microsoft Online Community Support
    >
    > ==================================================
    > For MSDN subscribers whose posts are left unanswered, please check this
    > document: http://blogs.msdn.com/msdnts/pages/postingAlias.aspx
    >
    > Get notification to my posts through email? Please refer to
    > http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
    > ications. If you are using Outlook Express/Windows Mail, please make sure
    > you clear the check box "Tools/Options/Read: Get 300 headers at a time" to
    > see your reply promptly.
    >
    > Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
    > where an initial response from the community or a Microsoft Support
    > Engineer within 1 business day is acceptable. Please note that each follow
    > up response may take approximately 2 business days as the support
    > professional working with you may need further investigation to reach the
    > most efficient resolution. The offering is not appropriate for situations
    > that require urgent, real-time or phone-based interactions or complex
    > project analysis and dump analysis issues. Issues of this nature are best
    > handled working with a dedicated Microsoft Support Engineer by contacting
    > Microsoft Customer Support Services (CSS) at
    > http://msdn.microsoft.com/subscriptions/support/default.aspx.
    > ==================================================
    >
    > This posting is provided "AS IS" with no warranties, and confers no rights.
    >
    Mike, Oct 19, 2007
    #3
  4. Hi Mike,

    Please refer to following thread for more information:

    http://forums.asp.net/t/955145.aspx?PageIndex=1

    Eilon from ASP.Net team posted two replies in the thread to describe
    workarounds.

    Also, on the 10th page of the thread:
    http://forums.asp.net/t/955145.aspx?PageIndex=10, there's also some other
    workaround that to override the Render() method of the Page class to move
    the __EVENTVALIDATION to the beginning of the page.

    As Eilon already pointed out:

    <quote>Unfortunately there's no "perfect" solution for this just
    yet.</quote>

    It's really difficult to get over this issue perfectly.

    Regards,
    Walter Wang (, remove 'online.')
    Microsoft Online Community Support

    ==================================================
    When responding to posts, please "Reply to Group" via your newsreader so
    that others may learn and benefit from your issue.
    ==================================================

    This posting is provided "AS IS" with no warranties, and confers no rights.
    Walter Wang [MSFT], Oct 22, 2007
    #4
  5. Mike pinged me offline about an issue that if an anchor was disabled at
    first, the previous javascript code doesn't work to enable it back.

    Here's my reply to him in case anyone else need it:

    Yes the Anchor elements are special since if it's initially disabled, the
    ASP.NET engine will not render out its href attribute, therefore even after
    it's enabled, you cannot click on it since it doesn't have href attribute.

    The workaround is:

    1) During runtime, in your case, in Repeater's ItemDataBound event, add a
    custom attribute "href_disabled" to save the href used to postback, and
    register the LinkButton for event validation. Since the ASP.NET engine
    requires RegisterForEventValidation must be called during Render, we save
    the LinkButtons in a list and deal with it in Render.

    private List<LinkButton> m_links = new List<LinkButton>();

    protected void repeater1_ItemDataBound(object sender,
    RepeaterItemEventArgs e)
    {
    if (e.Item.ItemType == ListItemType.AlternatingItem ||
    e.Item.ItemType == ListItemType.Item)
    {
    LinkButton link1 = (LinkButton)e.Item.FindControl("link1");
    link1.Attributes.Add("href_disabled",
    ClientScript.GetPostBackClientHyperlink(link1, ""));
    m_links.Add(link1);
    }
    }

    protected override void Render(HtmlTextWriter writer)
    {
    foreach (LinkButton link in m_links)
    {
    ClientScript.RegisterForEventValidation(link.UniqueID, "");
    }
    base.Render(writer);
    }

    2) In client-side javascript:

    function enableFormElements() {
    enableElements(document.getElementsByTagName("input"));
    enableElements(document.getElementsByTagName("select"));
    enableElements(document.getElementsByTagName("textarea"));
    enableElements(document.getElementsByTagName("button"));
    enableAnchors(document.getElementsByTagName("a"));
    }

    function enableAnchors(anchors) {
    for (var i = anchors.length - 1; i >= 0; i--) {
    var anchor = anchors;
    anchor.href = anchor.getAttribute("href_disabled");
    anchor.removeAttribute("href_disabled");
    anchor.disabled = false;
    }
    }


    Hope this helps.


    Regards,
    Walter Wang (, remove 'online.')
    Microsoft Online Community Support

    ==================================================
    When responding to posts, please "Reply to Group" via your newsreader so
    that others may learn and benefit from your issue.
    ==================================================

    This posting is provided "AS IS" with no warranties, and confers no rights.
    Walter Wang [MSFT], Oct 24, 2007
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Kevin Spencer

    Re: Viewstate issues... no values on postback

    Kevin Spencer, Aug 27, 2003, in forum: ASP .Net
    Replies:
    0
    Views:
    534
    Kevin Spencer
    Aug 27, 2003
  2. Paul

    Postback issues

    Paul, Dec 24, 2003, in forum: ASP .Net
    Replies:
    1
    Views:
    337
    Victor Garcia Aprea [MVP]
    Dec 27, 2003
  3. Bob Trabucco

    Datagrid Postback issues

    Bob Trabucco, Jan 16, 2004, in forum: ASP .Net
    Replies:
    2
    Views:
    1,685
    Bob Trabucco
    Jan 19, 2004
  4. =?Utf-8?B?am9obiBzbWl0aGVycw==?=

    Postback issues

    =?Utf-8?B?am9obiBzbWl0aGVycw==?=, Aug 29, 2004, in forum: ASP .Net
    Replies:
    0
    Views:
    430
    =?Utf-8?B?am9obiBzbWl0aGVycw==?=
    Aug 29, 2004
  5. macduder83

    Inheritance + Postback issues

    macduder83, Apr 14, 2005, in forum: ASP .Net
    Replies:
    2
    Views:
    363
    macduder83
    Apr 15, 2005
Loading...

Share This Page