Solved

how to call a registerstartupscript javascript within a c# method

Posted on 2008-10-10
8
1,580 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction A frequently asked question goes something like this:  "I am running a long process in the background and I want to alert my client when the process finishes.  How can I send a message to the browser?"  Unfortunately, the short answer …
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.
This is a video describing the growing solar energy use in Utah. This is a topic that greatly interests me and so I decided to produce a video about it.

943 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

10 Experts available now in Live!

Get 1:1 Help Now