0

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

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Crap... I knew that :/ Thanks, still learning and I haven't used a lot of these enough. Will mark once timer allows.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.