2012年3月10日土曜日

Run Javascript after Multiview Update

hello.

how are you injecting the javascript on the page after a partial postback? you should use the registerxxx api of the clientscriptmanager class to inject javascript instructions on a page during a partial postback or they won't be handled correctly on the client side.


Currently I am just including it in a <script> block in the actual control, but I have tried all different types of registerclientscript methods. When you say registerxxx api, could you show a brief example? I am assuming this should be included on the main page and not in the second control page, correct?


Okay, I found msdn's example of how to register a javascript file using the clientscriptmanager at...

http://msdn2.microsoft.com/en-us/library/system.web.ui.clientscriptmanager.getwebresourceurl.aspx

However, my issue is that in the example an "onclick" attribute triggers the javascript function, but in the case I explained above we need the javascript to trigger immediately on the page load (ie, when a user is on tab 1 and clicks tab 2 to view control 2) without a client trigger. Is that possible?


Its amazying how fast things get resolved once a post begins ; )

Okay, athttp://forums.asp.net/thread/1302293.aspx, I found the following...

string script ="alert(\"asdasdasd\");";
Page.ClientScript.RegisterStartupScript(this.GetType(),"alertScript", script,true);

which works, but it runs the script on both tabs 1 & 2. However, I only want to run it on the second control which displays when tab 2 is clicked. How can I register the script so it executes conditionally? Is this a clientscriptmanager attribute?


hello again.

well, you're the one that have to decide if the script should run. i think that in this case you probably can do it in the server side. for instance, since you're using a multiview, you can handle ActiveViewIndexChanged event. in it, you can check for the current view and, if it's the tab you want, you can register the script (if not, then you don't register).


Great thought. Thanks very much for your help!

0 件のコメント:

コメントを投稿