Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1637
  • Last Modified:

how to call a registerstartupscript javascript within a c# method

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
Nygter
Asked:
Nygter
  • 5
  • 3
1 Solution
 
NygterAuthor Commented:
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
 
ororioleCommented:
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
 
NygterAuthor Commented:
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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
ororioleCommented:
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
 
NygterAuthor Commented:
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
 
ororioleCommented:
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
 
NygterAuthor Commented:
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
 
NygterAuthor Commented:
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

[Webinar On Demand] Database Backup and Recovery

Does your company store data on premises, off site, in the cloud, or a combination of these? If you answered “yes”, you need a data backup recovery plan that fits each and every platform. Watch now as as Percona teaches us how to build agile data backup recovery plan.

  • 5
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now