After trying numerous things with the AjaxToolKit, which unfortunately doesn't allow me to launch an animation from codebehind I started looking into jQuery.
Basically I have somewhere in my code an if statement (something I myself cannot do in JS) and I want to launch the animation from there.
RegisterClientScriptBlock paces the javascript code right after the <form> element. if your div is not defined before the form, then when the code runs it does not exists. try:
endrequest phase will be happened before pageload. When you call registerClientScriptBlock, it will render the script with HTML code. And then. As far as it got the response, endrequest will be triggered. So after you render the script out, it just work
for the next round. For current round when endrequest did, the client doesn't have the script that you registered yet.
For this scenario, you had better put the script on the page instead of using registerclientscriptblock to register them.
If you have any feedback about my replies, please contact msdnmg@microsoft.com
Marked as answer by Vince Xu - MSFT on Jun 16, 2010 09:31 AM
JavaJoe
Member
29 Points
57 Posts
JQuery from codebehind via ClientScript.Register... ?
Jun 10, 2010 08:36 AM|LINK
I really need to toggle a div from codebehind.
After trying numerous things with the AjaxToolKit, which unfortunately doesn't allow me to launch an animation from codebehind I started looking into jQuery.
Basically I have somewhere in my code an if statement (something I myself cannot do in JS) and I want to launch the animation from there.
Can I launch the toggle of a div via
<div style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;" id="_mcePaste"> String scriptText = "";</div> <div style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;" id="_mcePaste"> scriptText += " $('#asd').toggle(1000)";</div> <div style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;" id="_mcePaste"> Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "CounterScript", scriptText, true);</div>String scriptText = ""; scriptText += " $('#asd').toggle(1000)"; Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "Script", scriptText, true);??
vishwanath_h...
Participant
968 Points
179 Posts
Re: JQuery from codebehind via ClientScript.Register... ?
Jun 10, 2010 09:16 AM|LINK
hi,
Register the script with some javascript function and inside that function make your jquery calls to toggle your div.
and use scriptmanager instead of page.clientscript if you have scriptmanager and control triggering the script is inside
updatepanel.
check the thread :
jquery toggle keep div from disappearing after postback (http://forums.asp.net/p/1527207/3687653.aspx) probably it will help you.
bruce (sqlwo...
All-Star
36904 Points
5452 Posts
Re: JQuery from codebehind via ClientScript.Register... ?
Jun 10, 2010 01:47 PM|LINK
RegisterClientScriptBlock paces the javascript code right after the <form> element. if your div is not defined before the form, then when the code runs it does not exists. try:
scriptText = "$(function(){$('#asd').toggle(1000);});";
this will telll jQuery to defer running the code until the after the dom loads.
JavaJoe
Member
29 Points
57 Posts
Re: JQuery from codebehind via ClientScript.Register... ?
Jun 10, 2010 03:28 PM|LINK
Works perfectly ! How can I make it work (once) after postback ?
JavaJoe
Member
29 Points
57 Posts
Re: JQuery from codebehind via ClientScript.Register... ?
Jun 11, 2010 02:50 PM|LINK
Would it be something like this ?
Sys.Application.add_init(appl_init);
function appl_init()
{
var pgRegMgr = Sys.WebForms.PageRequestManager.getInstance();
pgRegMgr.add_endRequest(EndHandler);
}
function EndHandler()
{
afterAsyncPostBack($(function(){$('#asd').toggle(1000);}););
}
JavaJoe
Member
29 Points
57 Posts
Re: JQuery from codebehind via ClientScript.Register... ?
Jun 11, 2010 05:56 PM|LINK
This works in pageload perfectly, but not with a button click event. How can I get this working ?
protected void Button_Click(object sender, EventArgs e) { String scriptText = ""; scriptText += "Sys.Application.add_init(appl_init);"; scriptText += "function appl_init(){"; scriptText += "var pgRegMgr = Sys.WebForms.PageRequestManager.getInstance();"; scriptText += "pgRegMgr.add_endRequest(EndHandler);}"; scriptText += "function EndHandler() {"; scriptText += "afterAsyncPostBack();}"; scriptText += "function afterAsyncPostBack(){"; scriptText += " $('#asd').fadeIn(500); $('#asd').fadeOut(500);}"; Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "Script", scriptText, true); }Vince Xu - M...
All-Star
80367 Points
6801 Posts
Re: JQuery from codebehind via ClientScript.Register... ?
Jun 15, 2010 02:21 AM|LINK
Hi,
endrequest phase will be happened before pageload. When you call registerClientScriptBlock, it will render the script with HTML code. And then. As far as it got the response, endrequest will be triggered. So after you render the script out, it just work for the next round. For current round when endrequest did, the client doesn't have the script that you registered yet.
For this scenario, you had better put the script on the page instead of using registerclientscriptblock to register them.