0

I tried and tried, looked here, on Google and didn't find how to do it. I just try to write a simple user control (.ascx) to display different type of ads (which all of them are scripts). The problem is that usually it's a complicated scripts, so someone (here) suggested to save the scripts as .JS files and call them from the control (.ascx) file. The question: How do I do it? Tried lot of time and it's not work. I'm frustrated... Can anyone pls give me an example code of how to do it?

Thanks a lot!

1
  • Tried what? Post what you "tried" Commented Dec 2, 2012 at 0:56

1 Answer 1

1

You can attach that "script file" to the page from the User Control using something like this:

(Taken from here in MSDN

public void Page_Load(Object sender, EventArgs e)
{
    // Define the name, type and url of the client script on the page.
    String csname = "ButtonClickScript";
    String csurl = "~/script_include.js";
    Type cstype = this.GetType();

    // Get a ClientScriptManager reference from the Page class.
    ClientScriptManager cs = Page.ClientScript;

    // Check to see if the include script exists already.
    if (!cs.IsClientScriptIncludeRegistered(cstype, csname))
    {
        cs.RegisterClientScriptInclude(cstype, csname, ResolveClientUrl(csurl));
    }
}

This way you include your script file with the correct url into the page. Then will load on client side, and assuming you have written everything correctly, will perform as intended.

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

2 Comments

Hi Thanks. But, not work yet... (I copied and paste.., everything the same except the url of the script of curse...). What I try to do is to display my adSende through UserControl so my pages will be cleaner - like, Instead all the Scripts code I want to put just the UserControl. So I saved the code from adSense as a .JS file and tried to show it with the above code - but, still, don't work , I don't know how to do it... , helpppppppppppppp
This sounds a job for a Master page than a user control. Can you update your question with the code you have developed so I can take a look?

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.