Solved

how to call a registerstartupscript javascript within a c# method

Posted on 2008-10-10
8
1,600 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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
 

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

Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

Question has a verified solution.

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

Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
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.
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…
Finding and deleting duplicate (picture) files can be a time consuming task. My wife and I, our three kids and their families all share one dilemma: Managing our pictures. Between desktops, laptops, phones, tablets, and cameras; over the last decade…

734 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