Ajax in existing asp .net project - asp.net

I have a web page devoloped in visual studio 2008.
I have 4 dropdowns and a repeater in the page.based on the selection(search criteria) from the dropdowns the repeater value will change.
and one dropdown selection will bind values to the other dropdown also.
Since the page is causing a lot of postback we decided to implement ajax here.
I am yet to learn ajax.
Can anyone tell what is the best way to do this .which ajax control replace dropdowns?
i have already server side code written on all dropdowns.
Please give me a good solution which i can implement in less time and reuse my code.
One more update: i have a master page used in the project.
I am using update panel of ajax which does not work if i use master page.
(That means all the dropdown controls and repeater i put it in update panel.But still page postback occurs.)
In a normal page(without master page) it works? why is this happening?
Thanks
SNA

You are able to use UpdatePanel and place dropdowns inside it.

Your solution will depend on the AJAX framework you choose, but here are cascading dropdown examples in ASP.NET AJAX and jQuery

If your main reason for using AJAX is to remove the number of postbacks you are getting, then I would recommend against using Microsoft's built in solution, e.g updatepanels.
The learning curve will be higher but learn to use jQuery, maybe with a little help from jTemplate to help you build your drop downs on the page.
Using updatepanels will not reduce your postbacks as behind the scenes asp.net is doing a full page life-cycle, sending all content back to your client but only updating the update panel. jQuery will be far more efficient. (and the reason I use it!!)
Update
If you don't believe me, see... Why Update Panels are dangerous
Update #2
If you don't want to go the whole way of learning Ajax just yet (although I'd recommend it) you could always pre-load your page with all the possible drop down combinations and then swap them using javascript / javascript + jQuery.
Here is one example of how you may do it -- use jQuery for dependent drop down combos
Using this method you are more likely to be able to save the code you've already written to work out the drop down options.

Related

Disable javascript in GridViews

Is there a way prevent an ASP gridview from always relying on javascript for the edit and delete LinkButtons? If I disable javascript in my browser the edit and delete commands still seem to link to "javascript: _doPostBack". (I'm using VB and .NET framework 2.0)
Thanks in advance.
You could implement the edit and delete buttons yourself using an <asp:TemplateField>. Then you are free to have them do the postback however you like, with <input type=submit> buttons, perhaps.
You are limited to smaller subset of controls when Javascript is not available. See MSDN. The postbacks function conveys to the runtime what was clicked and that is how the server side events are triggered. If you really want to develop an app without Javascript support, you will have to stay away from most server controls. The rendering is fine but any interaction (like editable grid) would be one of those controls to stay away from. You might also want to look into ASP.NET MVC framework.

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.

Replacing ASP.NET Ajax controls with jQuery

I have a solution that uses Accordian and tab controls, amongst others.
Is it worth swapping these out and use the jQuery controls instead?
What are the benefits? Is it best practice to load one tab at a time with data?
There are obviously benefits to using both, but in my experience if you are using ASP.NET then you are going to find the (I'm assuming you meant Ajax Control Toolkit) the ASP.NET controls better if you have a requirement for data-binding or have a high tendency to utilise your server execution time a lot.
IMO if the current controls meet your requirements then there isn't a breaking reason to switch to the jQuery controls, some ASP.NET server controls that provide Ajax functionality can be quite heavy-weight and cumbersome, but I've found controls like the Accordion to fit my needs perfectly, and wouldn't see a need to switch unless it wasn't flexible enough.
I had to make this swap in some test on my projects, from ASP.NET Ajax controls to jQuery controls, and my only issues was the UpdatePanel's.
ASP.NET take care the UpdatePanel messages (when you make asynchronous calls), but if you going to change it with jQuery you need to take care this updates by yourself - if you use UpdatePanel.
Update.
I agree with the comments for the UpdatePanel. I was working on a project, that had already many upadtePanels, and we just started to change some things to see what and how. If you build it up from the beginning I agree that it's better to avoid UpdatePanel, and do it with jQuery.

ASP.NET AJAX modal popup framework

I'm using ASP.NET 3.5 + Ajax Control Toolkit + jQuery (NO MVC yet)
I'm looking for a standard solution for showing a user control (.ascx) as a modal popup (like the Ajax Control Toolkit ModalPopupExtender implementation), where I can pass parameters to it before it opens. Then get some values on closing, and potentially handle events on the server.
I'm currently using a combination of the ModalPopupExtender and update panel to implement this each time I need it. It seems like it takes too much plumbing that has to be created each time.
Before creating my own packaged solution, I'm looking for an existing solution, or for a better pattern to implement this.
I've always set a hidden field value on clientside. My modalpopups data would have parameters that would come from that hidden field. This works great when you update the updatepanel.
function setfield(v) {
document.getElementById('<%=HiddenField2.ClientID%>').value = v;
}
But maybe this solution isn't a best-practice.
It didn't take me too much time to do this. Let me dig up my own implementation.
EDIT:
In it's simplest form, this is what I used. I could drop this into a UserControl and then drop that in any page I wished to use it.
Trying to self contain pop ups which use the AjaxToolkit ModalPopUpExtender
Not sure if this is the part you're wrestling with though.

How do I refresh an ASP.NET ListView using jQuery and AJAX?

I have a page with a number of ListViews that I want users to be able to sort and page through. Rather than postback and rebind the entire page each time, I would like to do it via jQuery/AJAX selectively for the control in question. I am comfortable making the client-side call to a WebMethod in my page - my question is how do I get the returned data back into the ListView via jQuery?
(Note: I don't want to use an UpdatePanel!)
I'm not sure if it'll actually be achievable to update a ListView without a postback, just because of the underlying data model of the ListView control.
You're best option to having a complete AJAX solution would be to use a JavaScript templating engine. I've done a demo on my blog using jTemplates and the MS AJAX Library v4 preview - http://www.aaron-powell.com/blog.aspx?id=1209
But despite common belief you can use an UpdatePanel and have it efficient, I also looked at that here: http://www.aaron-powell.com/blog.aspx?id=1195. The biggest thing to keep in mind when using UpdatePanels is ViewState. If you don't need ViewState saved on a control make sure it's turned off. You can really reduce your post-load by doing that. Also removing whitespace will help.

Resources