Search code examples
asp.netwebformsjquery-templates

Mixing server controls and jquery templates on a ASP.NET page


I am working on a page that has a mix of server side controls and simple html controls rendered using jquery templates. This is a ASP.NET webforms application.

When a server side request is made by clicking on a save button, if validation fails on the server, then the postback causes the values entered in the simple html controls to be lost (since it has no viewstate).

The only solution I can think of is to save the state of the simple html controls into a hidden input control before the server request is made. If validation fails, rehydrate the jquery template from this hidden input. It appears I can use the serialize function in jquery but I need to ignore the server controls from this collection (i think?)

Is there a simpler way other than having to serialize/deserialize information? Will UpdatePanels help?


Solution

  • Frankly, I don't think UpdatePanels ever help. They will complicate your code-behind (although you may already have lots of if (Page.IsPostback == false) statements in your code) and the requests can be really slow. I find them extremely cumbersome to use, personally.

    Depending on the size and complexity of your page, it might be worth checking into .asmx webservices. I've used them extensively myself (for this precise situation, as a matter of fact), and it's pretty easy to call them if you're using jQuery already.

    However, as a caveat: I have read a few questions here and articles elsewhere, saying that .asmx-style webservices are going away eventually, and that you should start using WCF to replace this functionality.

    If you post some of your markup and/or C# code (it doesn't have to be complex at all; just enough so that we're speaking the same language (terrible joke)), I could provide you with example jQuery AJAX calls and WebService examples.