Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 909687
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T16:55:19+00:00 2026-05-15T16:55:19+00:00

I am trying to have a method in my code behind at runtime add

  • 0

I am trying to have a method in my code behind at runtime add a function at runtime.
I can dynamically add elements to my document with no problem. So I tried the same strategy for adding a script block.

Here’s what I have tried thus far with no luck. It can create the script block but when I try to add the function. The only thing I can gather is that since the page is loading, it can’t add the JS method. I am trying to add this before the page is loaded. So I figured I would be able to add script. If I can’t add a function, I would like to at least feed it some javascript in my code behind to invoke at runtime.

Here is how I try to dynamically add the script block… which throw a runtime error on the SetProperty() method.

HtmlElement script = HtmlPage.Document.CreateElement("script");
script.SetAttribute("type", "text/javascript");
script.SetProperty("innerHTML", "function testing() { alert('hello world'); }");
HtmlPage.Document.Body.AppendChild(script);

Then to invoke an EXISTING function on the document this works…

HtmlPage.Window.Invoke("testing2");

If I can’t dynamically add a script block could I somehow invoke some javascript from my code behind?

Thanks!!!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T16:55:20+00:00Added an answer on May 15, 2026 at 4:55 pm

    Well I figured out how to Invoke some script from the code behind…

    Here’s how to invoke some script at run-time

    HtmlPage.Window.Eval("window.close();");
    

    Here is a way to dynamically create JavaScript functions and add them to your page to use in silverlight applications.

    // Create the script block
    var scriptElement = HtmlPage.Document.CreateElement("script");
    scriptElement.SetAttribute("type", "text/javascript");
    // Add a function called foo
    scriptElement.SetProperty("text", "function foo() { alert('hello foo!'); } ");
    // Append script block to the body of the page
    HtmlPage.Document.Body.AppendChild(scriptElement);
    
    
    // Now invoke the function from your silverlight application
    HtmlPage.Window.Invoke("foo", null);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a method from decompiled code that i am trying to run and
I'm trying to not repeat code, so I have a method that does a
I have a method which I am trying to call dynamically. That method has
I have an asp.net page with a c# code-behind. I am trying to have
I am trying to access a method in my code behind called Button1_Click using
I am trying to call a simple method in my code behind using Jquery
I have a gridview that I'm trying to validate from code behind. In this
I am trying to have a method that takes in a username and will
Trying to use GridBagLayout. I have the method called buildLabel. This creates three label.
I have been trying to pass a string value from one method into another.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.