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

  • Home
  • SEARCH
  • 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 3610466
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T21:45:21+00:00 2026-05-18T21:45:21+00:00

Thanks Stilgar solved my problem. I just create a js file and write all

  • 0

Thanks Stilgar solved my problem.
I just create a js file and write all my codes into it after i added this file to UserControl and after i get this UserControl’s html i use $(“#DivID”).html(UserControlHTML);
Its working now.

Hi everyone;

Im using Ajax and Webservice to load UserControls. Its ok i can easily get the html code of UserControl but there is a problem.

For example UserControl’s html code is something like that.

<h3>Header</h3>
<div id="content">
    <p>lorem ipsum dolor sit amet...</p>
</div>
<script type="text/javascript">
        alert("This is a message");
</script>
<h3>Footer</h3>

Javascript codes that requesting html code of usercontrol
(usage UserControlYukle(“UserControls/Article.ascx”); )

//This is a function that i request UserControl's html code from WebService
function UserControlYukle(ControlPath) {
    PersonalWebPage.Services.LoadUserControl.GetControlHtml(ControlPath, Success, Failed);
}
function Success(result) {
    var RsltElem= $get("Icerik");
    RsltElem.innerHTML = result;
}
function Failed(error) {
    alert(error.get_message());
}

Webservice Codes (LoadUserControl.asmx)

//This is the code block which is in WebService 
//and returning back UserControl's html code.
public string GetControlHtml(string controlLocation)
{
    Page page = new Page();
    UserControl userControl = (UserControl)page.LoadControl("~/UserControls/" + controlLocation);
    userControl.EnableViewState = false;
    HtmlForm form = new HtmlForm();
    form.Controls.Add(userControl);
    page.Controls.Add(form);
    StringWriter textWriter = new StringWriter();
    HttpContext.Current.Server.Execute(page, textWriter, false);
    return textWriter.ToString();
}

When i get this html code and insert it to an div’s innerHTML.
html looking as it should. But javascript codes which written on usercontrol not working.
It should give me alert but its not.

  • 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-18T21:45:21+00:00Added an answer on May 18, 2026 at 9:45 pm

    It seems like assigning the innerHtml property won’t trigger JS execution. If this is a pattern you use often you can define some function by convention for example ajaxCallbackControlName where ControlName is the name of your current control. Then wrap the alert in that function and call the function in the place where the alert is called. This way the control will work the same when loaded normally (not via the service). On the other hand in the success function for the service you should be able to call the function like this:

    window["ajaxCallback" + controlName]();
    

    (see How to execute a JavaScript function when I have its name as a string for more details)

    However with this approach you will have problems if there are multiple instances of the same control on the page because all of them will be triggered. You can work arond this by adding a random string as a parameter to the service and adding it to the function name.

    All this is a super generic solution that should work for any control and with any js code in it, but in practice you may want a more specific solution like loading the callback functions in JS file and making specific functions for loading specific control via the service. Then you can call the needed callback function after you set the innerHtml property.

    Edit:
    It seems like setting the innerHTML will not define functions. So you should eval the code yourself. Here is a simple client side only example.

    <html>
        <body>
        test
        <br />
        <div id="testDiv"></div>
        <br />
        <input type="button" onclick="insert()" value="Insert" />
           <script type="text/javascript">
           function insert(){
            var element = document.getElementById("testDiv");
            element.innerHTML = "test innerHTML  <script id='loadScript' type='text/javascript'> function foo() {alert('test'); } <\/script> ";
            eval(document.getElementById("loadScript").innerHTML);
            foo();
            }
            </script>
        </body>
    </html>
    

    Another aproach would be to select all script elements for example via JQuery and eval their content. It is also worth checking if the JQuery API has some kind of function to set the HTML and execute the scripts.

    Edit 2: As it seems the JQuery html() function has exactly this behaviour. If you are using JQuery you can change your Success function like this:

    function Success(result) {
        $("Icerik").html(result);
    } 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Thanks to a Q&A on stackoverflow. I just found out how to determine the
Thanks for reading this. I am dynamically generating some data which includes a select
Thanks for a solution in C , now I would like to achieve this
Thanks for reading this. I have markup similar to what is below. Using the
Thanks for reading this I thought I could use find(), but couldn't make it
Thanks for reading. I'm a bit new to jQuery, and am trying to make
Thanks for answers,Actually I am not puzzled about draw 1024*768 pixels is slower than
thanks in advance for your help. I am wondering if there is a (design)
Thanks to a library upgrade (easymock 2.2 -> 2.4), we're having tests that have
Thanks to ASP.NET MVC framework, it became possible to unit test web applications. But

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.