0

I'm making an ASP.NET webforms application and I have a problem with injecting a script into the page from a web control. The script is located in the same folder as control:

Screenshot1

So I'm trying like:

[ToolboxData("<{0}:MyWebControl runat=server></{0}:MyWebControl>")]
[System.Drawing.ToolboxBitmap(typeof(MyWebControl))]
public class MyWebControl : Image
{

    //...

    protected override void OnPreRender(EventArgs e)
    {
        base.OnPreRender(e);


        string scriptStr = "<script src=\"my_script.js\" type=\"text/javascript\">";
        ClientScriptManager csm = Page.ClientScript;
        csm.RegisterClientScriptBlock(this.Page.GetType(), "MyScriptTag", scriptStr);

        //...
    }

}

When I check if the script got injected successfully, Chrome gives me this:

Screenshot2

I guess the problem is connected with script src=\"my_script.js\", but I tried changing the path for long with no success.

Looking forward for some help :)

1 Answer 1

0

You need the path to the javascript file to be relative to the page that the user control is being rendered within. I'm assuming that the user control is being loaded by your index.aspx in your web project. Add a Scripts folder to the web project and move the javascript file there. Then, change the src to

src=\"/Scripts/my_script.js\"

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

2 Comments

Great, that worked! But it's not precisely what I meant. Is it possible to somehow encapsulate the script file in the location I originally placed it? I would like to have it in the first project
You would need to set the script as an embedded resource and follow additional steps to implement it properly. msdn.microsoft.com/en-us/library/bb398930.aspx - this is an older doc, but should be relevant to what you're trying to do.

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.