How to implement ClientScript.RegisterClientScriptInclude

V

verci

Hi guys,

I'm using asp.net 2.0, can anybody send me some complete examples on using
ClientScript.RegisterClientScriptInclude and
ClientScript.RegisterClientScriptSource, for registering an external
javascript file (myfile.js) in my ASP page, I have not found any clear or
complete examples, meaning in wich event handler should I put it in
page_load? if I'm using a master page where is the correct place to put it
in?
please any help would be very appreciated.

Regards
 
G

Guest

Howdy,

1. declaratively (aspx page code)
<script language="javascript" src="<%=ResolveClientUrl("~/scripts/timer.js")
%"></script>

or programmatically in master page load / init/ prerender / any event
handlers:
Page.ClientScript.RegisterClientScriptInclude("my",
ResolveClientUrl("~/scripts/timer.js"));

2. i reckon instead of ClientScript.RegisterClientScriptSource you meant
ClientScript.RegisterClientScriptBlock (or if i got you wrong, do you use
store javascripts in a resource file?)
If i got you right, try this:

protected void Page_Load(object sender, EventArgs e)
{
RegisterMessageScript("Hello World!");
}

private void RegisterMessageScript(string message)
{
System.Text.StringBuilder script =
new System.Text.StringBuilder();

script.Append("<script language=\"javascript\">\n");
script.AppendFormat("alert('{0}');\n", message);
script.Append("</script>\n");

Type type = this.GetType();

if(!ClientScript.IsClientScriptBlockRegistered(type, "messageScript"))
ClientScript.RegisterClientScriptBlock(type, "messageScript",
script.ToString());

}

hope this helps

Milosz
 
V

verci

Hi Milosz,

First, thank you so much for your complete answer. :)

Second, sorry about that bunch of posts but my outlook express was acting
weird, kept closing when I tried to post messages.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,774
Messages
2,569,596
Members
45,137
Latest member
NoelAshwor
Top