Avoid ASP.NET postbacks - asp.net

How to Avoid ASP.NET postbacks?

Use AJAX, UpdatePanels, JQuery:
http://www.asp.net/ajax/ should give you a good start.

Asp.NET is entirely based on Postbacks.
May be are you looking for partial page rendering and client side stuffs. If this is the case, you can have a look at Asp.NET Ajax.

Try using html controls if you want to avoid postbacks. ie., it doesn't post back to the server.
What is your exact requirement? You don't want to postback the control or you don't want to feel the postback of a page. If so, you can go for UpdatePanel in asp.net ajax and try using Conditional Update.

Try using Asp.net MVC3 or MVC4 People tend to prefer MVC to webforms

Related

ASP.NET (webforms): Using with MINIMAL server controls and substituting with JQUERY?

I am currently working with ASP.NET and the person who designed the form has used all Server Controls for things like TextBoxes and Dropdowns etc when really they are not providing postbacks.. Some of the dropdowns and textboxes are values that I need only in jQuery so as far as I can see there are no drawbacks to coverting these controls to standard html controls rather than ASP.NET server controls?
I suppose I will need to continue to have my GetDataGrid button as a server control because I will need it to postback (and receive PageLoad events etc - all asp.net events) to update the GridView? Or would it be possible to use the GridView (ASP.NET server control) from a Webmethod and call it via Jquery?
Of course in my webmethod I would need to the instance of the gridview to add the datasource - but I don't see how this would be possible without being in the ASP.NET events - or maybe I wrong?
The other thing I thought of was changing the GetGridView button to a standard HTML and calling the javascript postback from the client click event?? This way it would do a real postback and I would end up in Page_load.
Taking everything into effect i don't want to the change the GridView asp.net control as it funcions well as an asp.net server control but i am unsure how i would do this.
I remember a document being available that said "how to use asp.net webforms without server controls" but i can't seem to find it. I suppose using webforms like asp.net MVC - but i can't change the project to MVC - its out of my control.
I would love to hear some feedback with regards to how to do this or comments etc.
I find ASP.NET webforms to inject a lot of code smell into pages - I am using .NET 3.5 so a lot of the output is with tables etc...
If you use Request.Form["..."] then you can get the information which was filled in in standard html input fields.
Instead of keep on using the GridView control I suggest you take a look at either jqGrid or the new templating system that Microsoft put into place for jQuery (currently a plugin but expected to be part of core jQuery from version 1.5 on). These can bound to json which can be retrieved from a webmethod or pagemethod call to fill up the template with data.
Also i don't think its possible from asp.net (code behind) to receive values of an html >control without it having runat=server.
Use webmethods.
Set a client event (like 'onchange') on the html control and then in javascript function called when the event is fired you can use PageMethods to send your data to the code behind.
Some thoughts...
The GridView can't be created in a WebMethod and even if there was a way to get that to work, you'd be better off going with a genuine client side grid. As that's not an option, I don't think there is too much point in trying to make any major changes to your existing pages.
ViewState
Changing the textboxes, buttons etc to HTML versions, would gain you a little bit in reduced Viewstate size but add a bit of complexity in how you handle interactions with the page. You can add runat="server" to HTML controls which will give you control over what is rendered and still have access to the control on the server side.
.Net 4 gives you far more control over viewstate but unfortunately in 3.5 its not as easy.
The GridViews
You could wrap the GridViews in UpdatePanels. That's a 'cheap' way to add some interactivity to your pages although you won't be gaining anything in terms of performance.
It's also still possible to manipulate the Gridview using jQuery on the client-side. There a lots of tutorials, blog posts etc explaining how to do this on the Internet.
MVC with Webforms
Its also possible to mix ASP.Net MVC with Webforms in the same website. As it sounds like you are familiar weith MVC, you might want to consider this approach for any new pages. Here's a blog post explaining how to do this.
Update:
Here's a more recent article by Scott Hanselman on how to use MVC with an existing Webforms application.

server side control in MVC

can we use server side controls in MVC, if no then why server side control list appears.
If you use the webforms viewengine (default) you can use the controls that doesn't use viewstate in asp.net mvc as well. And if you add a form with the attribute runat="server" you can use the controls that use viewstate as well. But I'd tell you not to use any of them even if you can. They don't really add any benefit for you anyway as you do not use your code-behind classes. You can't use them in the same way in your controller action.
No, not really, as they all rely on ViewState and a lot of the web forms plumbing. As to why they appear, I'd guess it's because you're on VS2008 and MS hadn't got a good way of telling one aspx page from another.

How can I emulate the ASP.NET Ajax UpdatePanel when using ASP.NET MVC?

In "traditional" ASP.NET (Web Forms), the UpdatePanel control lets you do a partial refresh of a part of a page. You don't need to write much code to hook this up.
What's the equivalent in ASP.NET MVC? I'm guessing I'd need to use a partial view for the bit that I want to update, then on the client side retrieve that HTML and pump it into the innerHtml of the correct DIV?
A tutorial/example would be helpful...
A quick google with your tags throws up a bunch. This is pretty good:
http://www.iridescence.no/post/Partial-Rendering-with-ASPNET-MVC-and-jQuery.aspx
And yes, you're just calling a controller to return rendered HTML which you then inject. Much lighter than the updatepanel and almost as easy to implement.
Have a look at Superload. I think it's the closest thing to the update panel that I've seen for asp.net mvc. Otherwise you can use jquery ajax, .load() or .get(), to update any parts of the page or divs. You just have to manually set the parameters as needed.

What is the best solution for displaying data in ASP.NET MVC?

I am new to ASP.NET MVC and I want to know your opinion about what is the best way to display some data from database using a <table>... an ASP.NET control or a jqGrid?
In which situation must each of them be used?
I appreciate any tips. Thanks.
If I understand correctly, the MVC model doesn't support the server-side event handling of standard asp.net controls, so I'd say that the jquery grid is definitely the best option.
Asp.net controls cannot be used in asp.net mvc.
The best way is to create a ViewModel classes and declare View as strongly typed.
I could continue but unfortunately don't know what exactly do you need.
Do you mean ASP.NET Controls or ASP.NET MVC Controls?
ASP.NET Server Controls should not be used in this framework... or rather, you should not use anything that relies on postbacks and/or viewstate. There are tricks to get some to work, but YMMV.
If the control is an ASP.NET MVC Control, that's a different story, and it will work ok.
JQuery controls also work since they are javascript controls.
use the MvcContrib grid. it is working fine

Automatically Refresh a web user control

How can we refresh a web user control automatically without a page postback in asp.net 2.0? i have tried meta tags in controls head tag but it also refreshes the whole page.
You could try implementing a little bit of the ASP.NET AJAX Framework into your app and wrapping that control in an UpdatePanel. It will work with .NET 2.0
Edit: Here is a quick link to a tutorial on using the ASP.NET AJAX UpdatePanel: http://ajax.net-tutorials.com/controls/updatepanel-control/
And here is the official introduction page from the ASP.NET website: http://www.asp.net/Ajax/Documentation/Live/tutorials/IntroductionUpdatePanel.aspx
User Controls are server based controls. Your only option might be some type of javascript but depending on what you are try to accomplish, unlikely.
There are several ways to do it and it depends on what your control is made of.
One of the easiest ways to do it is to wrap the control in an UpdatePanel and then use a Timer control to just update that UpdatePanel. But that might be expensive in terms of viewstate.
Another option is to use something like Jquery and a Javascript timer to count down and then refresh using static page Methods (http://encosia.com has examples).

Resources