c# - Perform postback on ASP.Net control dynamically added by jQuery -


i have website running under asp.net. has number of ascx controls each represent different page. master page call loadcontrol() on ascx , add panel. ascx page_load() hook personal save function event on page "save" button on master page right thing. working.

i trying more dynamic , use jquery more , things more on client side. end, experimenting loading ascx controls dynamically. goal able minimal changes each ascx. have page, following contents (edited brevity):

<head> <script language="javascript" type="text/javascript"> function documentmetadataload() {     jquery.ajax({       type: "get",  //get       url: "jqueryhandler.ashx?operation=loadpage&name=/usercontrols/documentmanagment/documentmetadata.ascx",       datatype: "html",       cache: false,       success: function (response)       {         $('#property_tab').html(response);       },       error: function ()       {         debugger;         $('#property_tab').html('error');       }     }); </script> </head> <body id="mainbody" class="bodyclass"> <form id="form1" runat="server"> <div id="btn_save">     <a href="#" id="btnsave" class="topbtntext">save</a> </div> <div>     <div id="tab1" onclick="documentmetadataload(); ">properties</div> </div> <div id="property_tab" style="display: block;"></div> </form> </body> 

this page dynamically load control called documentmetadata.ascx when user clicks on tab. (yes, aware there gaping security hole when client passes path of control, proof-of-concept.)

the jqueryhandler take given path , generate page. processrequest() looks this:

public void processrequest(httpcontext context) {     if (context.request.querystring["operation"] == "loadpage")     {         string strpath = context.request.querystring["name"];         using (tispage mypage = new tispage())         {             htmlform f = new htmlform();             f.action = context.request.urlreferrer.absolutepath;             usercontrol userctrl = mypage.loadcontrol(strpath) usercontrol;             f.controls.add(userctrl);             mypage.controls.add(f);             context.server.execute(mypage, context.response.output, true);         }     } } 

(thank http://www.infoconcepts.com/rendering-ascx-files-ajax/ indicating htmlform should wrap control.) tispage subclass of system.web.ui.page, added event saving data, ascx expects.

now, problem having want documentmetadata.ascx have save function called when user presses #btnsave. code behind, , expects have viewstate retained. assume have have postback. prefer not have rewrite c# save function own ashx call passed in parameters; there lot of pages , not worth trouble rewrite them all.

i have following code in documentmetadata.ascx setup button press something.

$(document).ready(function () {     $("#btnsave").bind("click", save);  });  function save() {     debugger;     __dopostback('ctl00', null); } 

i experimented having call __dopostback() in javascript, error: "validation of viewstate mac failed. if application hosted web farm or cluster, ensure configuration specifies same validationkey , validation algorithm. autogenerate cannot used in cluster." experiment, tried remove machine key configuration web.config, did not seem help.

is there way use javascript call code behind of ascx control loaded jquery?

loading .ascx control through ajax same loading string. one-way pipeline, , there no post-back communication available control. updatepanel solution not work either (and terrible too).

my suggestion create client-side templates, using mustache.js, load json data through ajax populate templates, , ajax webservice communicate server/database. separating code, easier maintain, you'll able reuse more code, better efficiency etc.

post-backs old school tech, , updatepanels nasty implementation of ajax. take time , right, both app , yourself, , you'll both better because of it.


Comments

Popular posts from this blog

jquery - How can I dynamically add a browser tab? -

node.js - Getting the socket id,user id pair of a logged in user(s) -

keyboard - C++ GetAsyncKeyState alternative -