Blog

ASP.Net AJAX Extensions, GetPostBackEventReference And Custom Controls

If you’ve a piece of code similar to this one, and try to use the control inside an UpdatePanel then you might be surprised to see that triggering that event will in fact do a complete PostBack of the page (read Refresh-like-Postback):

public override void Render(HtmlTextWriter writer)
{
Response.Write("Button", Page.ClientScript.GetPostBackEventReference(this, "onclick"));
}

The reason is simple but not at all obvious, the ASP.Net AJAX framework requires you to setup a valid ID on the element like this:

public override void Render(HtmlTextWriter writer)
{
Response.Write("Button", Page.ClientScript.GetPostBackEventReference(this, "onclick"), ClientID);
}

Nice coding :)

Post to Twitter Post to Delicious Post to Digg Post to Facebook Post to Reddit Post to StumbleUpon

Tags: , , ,

12 Responses to “ASP.Net AJAX Extensions, GetPostBackEventReference And Custom Controls”

  1. Joe says:

    Say
    what? That doesn’t even make sense…

  2. I’ve not poked around with the ASP.NET
    Ajax framework, but that is some hedious HTML code right there. Does it really require an ‘onclick’ attribute? Doesn’t it use addEventListener (or attachEvent in IE) behind the scenes? I
    understand the requirement for an ID attribute, but sprinkling JavaScript all over your HTML code is such a 10 year old solution.

  3. alexmipego says:

    First of all this is an example
    only. Second, you need to understand what we’re talking about specially the context of this code.

    That piece of code belongs to a control written in C# that will generate html/js to a control
    that might or might not run with AJAX. You’ll need to learn some ASP.Net to understand it.

    Lastly, if you have any experience with ASP.Net or other control-based web development platform you
    should know that the onclick (as well as all the other tags for this matter) exist to solve this problems.

    The other way of doing this would be generating 1 more line of Javascript to add an
    event handler but! That get far more complicated when using AJAX to add dynamic controls which would require lines of code and checks to make sure we get and run everything. Its doable, sometimes we
    need to do it, but in this cases we simply use the standard, better and easier “onclick” way.

  4. Thanks for
    elaborating. :)

    I know quite a bit of ASP.NET, it’s just the new Ajax stuff I haven’t played with yet, as I’ve found jQuery to be such a sweet spot that I seriously doubt anything Microsoft
    have cooked up in this apartment can be as light-weight, good, cross-browser compatible and elegant.

    I understand why ‘onclick’ is used, I just wondered if Ajax.NET required it. As far as I
    can tell, it doesn’t. Which is good. There should be some glue between ASP.NET and jQuery, though. Perhaps I’ll write a set of jQuery-enabled controls one day, although I find it’s so easy to just
    write the few lines of jQuery required separately from the controls, which allows me to use plain old HTML elements instead of ‘runat=”server”‘ gunk that messes up your markup.

  5. alexmipego says:

    ASP.Net Ajax, former
    Atlas project, is a nice Ajax implementation that actually makes use of ASP.Net power. In fact, people tend to abuse because its so cool and easy.

    Just so you know, there are no special Ajax
    controls, its all the same and old WebControls and thats where my post fits in, if you had an old WebControl that didn’t work as supposed (postback via ajax that is) the id could be the reason.

    I
    know jQuery is very good, but I suggest you to take a look at some videos and quick tutorials at http://www.asp.net/ajax you’ll find it quite refreshing, full of new possibilities.

    Just try not to abuse
    UpdatePanels as everyone else does! Just joking :)

  6. Since I rarely use the old WebControls
    anymore (I rather use plain old HTML markup with inline -code) I don’t think ASP.NET Ajax has much to offer me. With ASP.NET MVC, the old WebControls become even more obsolete, which probably
    obsoletes ASP.NET Ajax too(?). Anyhoo, I’ll have a look at those videos to see if they can convince me. 😉

  7. alexmipego says:

    I understand your point of view and I’m
    sure you know what you’re doing. I just believe WebControls have a place which is the code reuse instead of handcoding things everytime. If you need small ajustments or extensions you add a few
    properties and thats the true appeal of ASP.Net versus ASP3 or even PHP and alike.

    I must confess I’m not an expert in MVC but I’ve seen the videos and know the concepts. It is not a replacement
    for the existing ASP.Net model, its an extension and has some uses in specific case scenarios. Even beeing so, I’m sure you can see uses for AJAX in MVC projects and so to ASP.Net AJAX altough you
    might choose not to use that brand :)

    Cheers mate, nice discussion :)

  8. Erik R says:

    Thanks! Saved my day :)

  9. danvo says:

    Nice piece of code! You should submit this to aspunity.com.
    Thanks!

  10. xabrewulf says:

    thx for this post, helped me out a lot!!

  11. Munna Kumar Singh says:

    That is correct. I had the similar issue with dropdown box inside update panel. After passing the unique id, it started working.

    __doPostBack(“”, ‘SelectedIndexChanged’);

  12. Munna Kumar Singh says:

    That is correct. I had the similar issue with dropdown box inside update panel. After passing the unique id, it started working.

    __doPostBack(“me.cboCompanyStoreGroup.UniqueID “, ‘SelectedIndexChanged’);

    we can use server side tag to get the uniqueu id of the control.

Leave a Reply

For spam filtering purposes, please copy the number 4895 to the field below: