Jordan
Jordan

Reputation: 399

javascript function in c# Page_Load

I have a c# I am writing and I can not figure out how to properly put the entire function in the c#. I know I can put it in a .js and call it, however, there are two images in it that are being called from the database and will change for each while. I believe the best place to put this is in my sql connection call.

What it comes down to is being able to run my javascript function directly in the c#.

Here is how I thought it might work and correct me where I am wrong please:

ScriptManager.RegisterStartupScript(this,GetType(),"function","function();",true);
    (function() 
        {
            blah blah blah
        });

This is all tucked in to the void Page_Load within my SqlConnection argument... Or I may be way off and not able to put javascript in line. I appreciate the insight. Thanks

Upvotes: 0

Views: 56

Answers (1)

mybirthname
mybirthname

Reputation: 18127

You need to declare your function in string and after that put it as attribute of RegisterStartupScript.

string jsFunction = @"
(function(); 
        {
            //your js code which should be executed
        });
";
Page.ClientScript.RegisterStartupScript(this.GetType(), "function", jsFunction, true);

You can read ClientScriptManager.RegisterStartupScript Method (Type, String, String, Boolean). There is a detail example of how to do it.

Upvotes: 1

Related Questions