Notes on the invalid Javascript in __doPostBack (Asp.Net 1.1)

Discussion in 'ASP .Net' started by Granger Godbold, May 12, 2004.

  1. This is in regards to the long-standing bug with the colons in the form-id. I'm not satisfied with
    the main workaround that's been circulating:
    And I'm rather annoyed that Microsoft won't put the fix out for anonymous download (However, one can
    get to it from links in the above forum).;en-us;818058

    Anyhow, after playing around, I found out some interesting stuff:
    HtmlForm.OnRenderChildren() is where the magic happens. It calls the (internal) method
    Page.OnFormRender() passing it the HtmlForm.UniqueID (instead of the ClientID as it ought).

    The OnFormRender method first renders hidden fields (Page.RenderHiddenFields), then renders the
    ViewState field. Next it renders the __doPostBack script as necessary (Page.RenderPostBackScript),
    and finally renders any registered script blocks (Page.RenderPostBackScript). After OnFormRender,
    IIRC, next came Control.OnRenderChildren(), and last was Page.OnFormPostRender().

    Specifically, here's the basic execution flow I saw (minus the "...", which means I didn't care to
    write it down, and I'm too tired to go back and do it again):

    - HtmlForm.OnRenderChildren(...)
    - Page.OnFormRender(HtmlTextWriter writer, string formUniqueID)
    - Page.RenderHiddenFields(...)
    - Page.CreateLosFormatter(...)
    - HtmlTextWriter.WriteLine()
    - HtmlTextWriter.Write(string s) <3 times in a row>
    - LosFormatter.SerializeInternal(...)
    - HtmlTextWriter.WriteLine()
    - Page.RenderPostBackScript(HtmlTextWriter writer, string formUniqueID)
    - Page.RenderScriptBlocks(HtmlTextWriter writer, IDictionary scriptBlocks)
    - Control.OnRenderChildren() <IIRC>
    - Page.OnFormPostRender() <IIRC>

    Lastly, there's an internal Page.RegisterPostBackScript() method which sets a private bool; this
    variable is used to determine if __doPostBack should be rendered. I found that if you set
    AutoPostBack=true in a DropDownList, Page.RegisterPostBackScript will be called in its OnPreRender
    method. I was unable to find a way to determine in my own code (at the Page-level) if that method
    had been called; I don't presenly know how to determine if __doPostBack will be rendered.

    Given the above, I think there's an equally unsightly, but much faster workaround if one doesn't
    want to apply that hotfix. You must derive your own HtmlForm object like below. Basically, it's a
    simple concept: When the buggy code that grabs the form's ID runs, it uses UniqueID instead of
    ClientID. So, if we just "fix" UniqueID for that one call, we're good-to-go. I isolated the
    important calls to HtmlForm.RenderChildren; UniqueID gets called twice from there. I know the first
    call is the important one, but I don't know for certain about the second (it's really late, and I'm

    public class FixedHtmlForm : System.Web.UI.HtmlControls.HtmlForm
    protected bool _fixDoPostBack;
    protected override void RenderChildren(HtmlTextWriter writer)
    _fixDoPostBack = true;
    _fixDoPostBack = false; // assure it gets reset even though I think this isn't needed

    public override string UniqueID
    if (_fixDoPostBack)
    _fixDoPostBack = false; // stop it after the first call
    return base.UniqueID.Replace(":", "_"); // this is what ClientID does
    return base.UniqueID;

    Hope this is helpful/interesting.

    (Note: There aren't actually underscores in my address)
    Granger Godbold, May 12, 2004
    1. Advertisements

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. Bjorn Jensen
    Bjorn Jensen
    Mar 22, 2005
  2. E
    John Saunders
    Jul 31, 2003
  3. =?Utf-8?B?TGFuY2UgQWhsYmVyZw==?=

    ASP.NET 2.0 Page with LinkButton & __doPostBack client side script missing

    =?Utf-8?B?TGFuY2UgQWhsYmVyZw==?=, May 6, 2004, in forum: ASP .Net
    May 6, 2004
  4. SteveM
    Mark Rae [MVP]
    Aug 28, 2007
  5. Eugene
    Ken Cox [Microsoft MVP]
    Nov 11, 2004

Share This Page