简体   繁体   中英

Asychnronous Message Send Asp.net

I'm trying to learn about how AJAX stuff works under the hood. Say I have an asp.net form with a button on it. I then have a database with a single column DateTime . When I click the button on the asp.net form I want to insert a column in the database with the current time.

I'll have a C# event handler:

protected void btnButton_OnClick(Eventargs e, object sender)
{
    //insert DateTime.now into DB
    //This part is easy
}

What I would like to happen on the form itself is to have the button click NOT reload the form. It would just send a message to the server; there is no reason for the page to reload. What are some ways I could accomplish this?

  1. I've looked into the AJAX framework a little bit and it seems like this could be done within an update panel, but the page would still reload, it would just be less noticable to the user.

  2. Use the __doPostBack call in javascript. I have no idea how this works.

It sounds like you're partial to Microsoft's provided javascript libraries, which is fine. The UpdatePanel and the like have their place, but I've found that when you need to do something very simple and straightforward like what you're describing it is easier and cleaner to get it done with a direct call to a server resource from Javascript.

There are many ways to do this, but I like using jquery's $.ajax() method to invoke webservice methods (or MVC actions more recently). If you're willing to examine some slightly different technologies here's a blog post that will give you a taste of what I'm talking about.

ASP.NET Ajax has 2 major parts:

  • server side controls based
  • client side framework (which is called Microsoft Ajax )

With the first you would opt to make use of an UpdatePanel control. This however still sends a lot of unneeded data over the wire like the full viewstate, your pages follow almost the full page life cycle like with a normal postback. Mostly I recommend against using that in production code.

The second part is an ajax library based on pure javascript with a touch of Microsoft sauce over it. It has similarities with the page life cycle like a pageLoad function and such and is quite easy to learn.

__doPostBack

That function gets inserted when you place certain server controls on your page like a LinkButton control. You can call that from javascript directly but know that it'll cause a full page postback to the server so if you want to avoid that don't go that path.

During the last 2 years however I've become a big fan of jQuery which works quite well together with ASP.NET and ASP.NET MVC.

I suggest that you read this fine article to get more information about it: Using jQuery with ASP.NET Part 2: Making an AJAX Callback to ASP.NET .
jQuery itself has also been adopted and favored by Microsoft. I strongly suggest you take a look at it and its power.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM