Solved

how to call a registerstartupscript javascript within a c# method

Posted on 2008-10-10
8
1,577 Views
Last Modified: 2012-08-13
Greetings.. thank you for reading my post..

I have this issue where i have two Page.RegisterStartupScripts which should be manually triggered inside a method, but i'm not sure how to do this... (see code for details.)

First.. the FadeOut() javascript should fade out the panel (pnlCampain) then.. it will change the data in it, update and databind the listview, then fade back in the panel.

If someone can give me some code-help here.. (maybe i'm not doing this the right way ? when it comes to the registerstartupscripts)...
I can't even get the syntax for registerstarupscript correct :(
FIRST JAVASCRIPT : 

if (!Page.ClientScript.IsStartupScriptRegistered("fadeOut"))

        {

            String scriptString = "<script language=\"JavaScript\">";

            scriptString += "Sys.Application.Initialize();";

            scriptString += "function fadeOut()";

            scriptString += "{";

            scriptString += "var ani = new Sys.Preview.UI.Effects.FadeAnimation();";

            scriptString += "ani.set_target($get('pnlCampain').control);";

            scriptString += "ani.set_effect(Sys.Preview.UI.Effects.FadeEffect.FadeOut);";

            scriptString += "ani.set_duration(3);";

            scriptString += "ani.play(  );";

            scriptString += "}";

            scriptString += "</script>";

            Page.ClientScript.RegisterStartupScript("fadeOut", scriptString);

            

        }
 

SECOND JAVASCRIPT : 

if (!Page.ClientScript.IsStartupScriptRegistered("fadeIn"))

        {

            String scriptString = "<script language=\"JavaScript\">";

            scriptString += "Sys.Application.Initialize();";

            scriptString += "function fadeIn()";

            scriptString += "{";

            scriptString += "var ani = new Sys.Preview.UI.Effects.FadeAnimation();";

            scriptString += "ani.set_target($get('pnlCampain').control);";

            scriptString += "ani.set_effect(Sys.Preview.UI.Effects.FadeEffect.FadeIn);";

            scriptString += "ani.set_duration(3);";

            scriptString += "ani.play(  );";

            scriptString += "}";

            scriptString += "</script>";

            Page.ClientScript.RegisterStartupScript("fadeIn", scriptString);

        }
 
 

THE METHOD WHICH SHOULD USE THE JAVASCRIPTS :

protected void timCampain_Tick(object sender, EventArgs e)

    {

        

        // call the first javascript to fade-out the panel

        fadeOut(); ???????
 

        int currPage = dpCampain.StartRowIndex;

        int lastPage = dpCampain.TotalRowCount;
 

        if (currPage < lastPage - 1)

        {

            dpCampain.SetPageProperties(currPage + 1, 1, true);

        }

        else

	    {

            dpCampain.SetPageProperties(0, 1, true);

	    }
 

        upnlCampain.Update();
 

        fadeIn() ????????
 

    }

Open in new window

0
Comment
Question by:Nygter
  • 5
  • 3
8 Comments
 

Author Comment

by:Nygter
ID: 22690506
More work.. but no success yet...
A quick update : 
 

trying to accomplish this with calling fadeIn() and fadeOut() by using this code : 
 

private void fadeLogic()

    {

        if (!this.Page.ClientScript.IsStartupScriptRegistered("fadeOut"))

        {

            string jsFunctionName = "fadeOut";
 

            String scriptString = "<script language=\"JavaScript\">";

            scriptString += "Sys.Application.Initialize();";

            scriptString += "function " + jsFunctionName + "(sender, args)\n";

            scriptString += "function fadeOut()";

            scriptString += "{";

            scriptString += "var ani = new Sys.Preview.UI.Effects.FadeAnimation();";

            scriptString += "ani.set_target($get('pnlCampain').control);";

            scriptString += "ani.set_effect(Sys.Preview.UI.Effects.FadeEffect.FadeOut);";

            scriptString += "ani.set_duration(3);";

            scriptString += "ani.play(  );";

            scriptString += "}";

            scriptString += "</script>";

            this.Page.ClientScript.RegisterStartupScript(typeof(Page), jsFunctionName, scriptString);
 

        }
 

        if (!this.Page.ClientScript.IsStartupScriptRegistered("fadeIn"))

        {

            string jsFunctionName = "fadeIn";
 

            String scriptString = "<script language=\"JavaScript\">";

            scriptString += "Sys.Application.Initialize();";

            scriptString += "function " + jsFunctionName + "(sender, args)\n";

            scriptString += "{";

            scriptString += "var ani = new Sys.Preview.UI.Effects.FadeAnimation();";

            scriptString += "ani.set_target($get('pnlCampain').control);";

            scriptString += "ani.set_effect(Sys.Preview.UI.Effects.FadeEffect.FadeIn);";

            scriptString += "ani.set_duration(3);";

            scriptString += "ani.play(  );";

            scriptString += "}";

            scriptString += "</script>";

            this.Page.ClientScript.RegisterStartupScript(typeof(Page), jsFunctionName, scriptString);

        }

    }

Open in new window

0
 
LVL 16

Expert Comment

by:ororiole
ID: 22695420
Ok, I see you have figured out the first problem, that you "call" the script by registering it.
Now, is anything happening at all? No errors, just nothing happening? Its worth a check to see if a simple script will work. Just register these
      Page.ClientScript.RegisterStartupScript(this.GetType(), "script1", "alert('Dude');", true);
      Page.ClientScript.RegisterStartupScript(this.GetType(), "script2", "alert('Yo');", true);

and make sure they work. If they do then there is something wrong with the Javascript itself. have you tried coding that script into the page and calling it to make sure it runs?
Try all that and let me know what happens.
0
 

Author Comment

by:Nygter
ID: 22695559
Greetings ororiole... Well.. i get build-errors when trying to call the javascripts.. that's the issue..

How do i register the scripts, so i can manually just call then whenever i want ?

Have no clue how to do this.. The Visual Studio "Typing Intelligence" doesn't catch it, and i get build-errors when trying to call ex. fadeIn()... It doesn't recognize i at all.. That's the issue :)

Terje.
0
 
LVL 16

Expert Comment

by:ororiole
ID: 22699964
Right, well what are the errors? Knowing that will help.
If you are registering startup javascript, you dont call it using fadeIn(), its called when you register it. That why I want you to try the "alert" example I gave you, so you can see how its supposed to work.

0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 

Author Comment

by:Nygter
ID: 22701200
The alert thing is working perfectly :)

The issue is that it's a trigger (timer trigger with 10 seconds tick) which should trigger the javascripts..

So in this case.. i need to call the fadeIn javascript, then update the data, and then... call the fadeOut javascript.. Maybe i _cannot_ use the registerstartupscript at all :)

In that case.. how can i make this happen ? I'm not sure how to create a javascript another way in .NET, but if you know. i would be very happy with a code-help on this :)

Terje.
0
 
LVL 16

Accepted Solution

by:
ororiole earned 500 total points
ID: 22708081
Well Im not sure what you are trying to do. There are lots of things wrong with the javascript, the problem is not with the timer. You MAY be able to call it from the server, but first you should get it working client side. Put the snippet below in your .aspx.

Just by doing that I found out that it should be Sys.Application.initialize(); capitalization does matter.
Next, Sys.Preview.UI doesnt exist.  Did you copy this from somewhere? Cause you appear to be missing some things.  I suspect you should be including a .js library.

You can see this error if you look at the bottom of the browser after you click the button. If you are using IE, its on the left, a little error indicator. Click it, and you will see the error.

Overall, this is pretty sophisticated javascript. If you haven't written javascript by hand, you might want to try learning that before tackling this.
put this in your head section

    <script language="JavaScript" type="text/javascript">

    	function fadeOut() {

    		Sys.Application.initialize();

    		var ani = new Sys.Preview.UI.Effects.FadeAnimation();

    		ani.set_target($get('pnlName').control);

    		ani.set_effect(Sys.Preview.UI.Effects.FadeEffect.FadeOut);

    		ani.set_duration(3);

    		ani.play();

    	}

 </script>
 

make sure you have this inside your form, in the body section:

	<asp:ScriptManager ID="ScriptManager1" runat="server">

		</asp:ScriptManager>
 

then I added a button and called the function just so I could test it. You should do the same:

        <asp:Button ID="Button1" runat="server" Text="Button" 

			OnClientClick="fadeOut();" UseSubmitBehavior="False" />

Open in new window

0
 

Author Comment

by:Nygter
ID: 22709580
Ah.. yes :) there are *.js files included in this. There's also some addings in the assembly.
The scriptmanager and js registrations are in the masterpage.

I'll give it a shot and see what i end up with here.

Thanx for the help so-far..
0
 

Author Closing Comment

by:Nygter
ID: 31506099
You learned me a bit on the limitations of javascript vs asp.net, and my own limitations (for my own good.. hehe).. seems like this problem is a bit too early to fix, sadly enough..

but thank you for good help !
0

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

708 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now