asp.net AJAX: Writing components, using JavaScript to update UpdatePanel

Source: Internet
Author: User

As is known to all, UpdatePanel is updated by trigger. The control that is set to trigger is intercepted by the client after postback, and uses XMLHttpRequest object to send content, then server driven by ScriptManager cooperate, change Page object's output, finally get partial refresh effect. But sometimes we may need to use JavaScript to refresh UpdatePanel, which is inconvenient.

Of course, one of the ways we belong to workaround is to use JavaScript to simulate the click of a button. We tend to set a button to a UpdatePanel trigger, and then simulate its clicks on the client (I'll mention later, in fact, this is a rather bad practice, not necessary) to make the UpdatePanel update. But such a practice is too troublesome, and quite not elegant.

Now we're going to write a component to solve this problem. The name of this component, called Javascriptupdater, seems to have been very bad--I've never been good at naming.

Let's set a demand first.

Our goal is actually to generate a JavaScript proxy on the client, providing a way to refresh the page after invoking it. If a UpdatePanel UpdateMode is always, then it will definitely be updated. If you need to update UpdateMode for conditional UpdatePanel, you need to configure which UpdatePanel will be updated by writing tag in the page. We need to minimize programming as much as possible.

Let's consider the way we use it, the javascriptupdater we write can be used in this page:

How to use Javascriptupdater

   ResolveUpdatePanel="OnResolveUpdatePanel" Enabled="True">
   <UpdatePanels>
          ...
   </UpdatePanels>

Javascriptupdater has a simple property methodname that indicates the name of the proxy method generated at the client. In the example above, this property is Refresh, indicating that we call the Updatepanels.refresh () method for UpdatePanel update. Updatepanels is a collection property that you can specify which updatemode to update with conditional UpdatePanel. If a updatepanelid is not found, the Resolveupdatepanel event is invoked, allowing the user to specify a UpdatePanel. There is also a enabled property that controls whether the javascriptupdater is in effect.

Multiple javascriptupdater can be placed in a single page, which allows multiple JavaScript proxy methods to be generated. Such a setting should have been sufficient.

Implement Javascriptupdater

Naturally, we first define the simplest two classes, the Updatepanelhelper.updatepanel class, and the Resolveupdatepaneleventargs class. Because it's so simple, just post the code directly:

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.