WebControl with CSS and Javascript

  • Thread starter Thread starter patrick.sannes
  • Start date Start date
P

patrick.sannes

Hi there,

I'm creating a custom control with Javascript and CSS. It will be a
nice overlay calendar with a result textbox. My main problem is that
the Javascript is realy huge, so you want to include it in a seperate
..js file. But, then it is not bound with the WebControl.... What is the
normal way to solve this??

Regards,
Patrick
 
You can embed the javascript file into the control assembly as an embedded
resource if you like.
Depending on which version of the framework you are working with , you can
get this out with GetManifestResourceStream.
 
the standard way is:

RegisterClientScriptBlock("mycontroljs","<script
src=\"mycontrol.js\""></script>");

or the new

RegisterClientScriptResource(typeof(this),"mycontrol.js");


both of these methods handle being called more than once so that the
<script> tag is only output once. they also have methods to determine if
they have been called already for the same tag.

-- bruce (sqlwork.com)
 
I did it with this peace of code I found, but then I get it inline (and
it is a lot, so it would be nicer to get it in a .js file... I realy
don't know if it is possible...

private void RegisterScript(String scriptName)
{
if
(!this.Page.ClientScript.IsClientScriptBlockRegistered(scriptName))
{
using (System.IO.StreamReader reader = new
System.IO.StreamReader(this.GetType().Assembly.GetManifestResourceStream(this.GetType(),
scriptName)))
{
String script = "<script language='javascript'
type='text/javascript'>\r\n<!--\r\n" + reader.ReadToEnd() +
"\r\n//-->\r\n</script>";
this.Page.RegisterClientScriptBlock(scriptName,
script);
}
}


}
 
Solved it...

Added
[assembly:
System.Web.UI.WebResource("Company.WebControls.popcalendar.js",
"text/js", PerformSubstitution = true)]
To the AssamblyInfo.cs
And
string scriptLocation =
Page.ClientScript.GetWebResourceUrl(this.GetType(),
"Company.WebControls.popcalendar.js");

Page.ClientScript.RegisterClientScriptInclude("Company.WebControls.popcalendar.js",
scriptLocation);
To the WebControl class.
 

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

Back
Top