Home All Groups Group Topic Archive Search About

WebControl with CSS and Javascript

Author
12 Jun 2006 2:17 PM
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

Author
12 Jun 2006 3:01 PM
Peter Bromberg [C# MVP]
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.

--
Co-founder, Eggheadcafe.com developer portal:
http://www.eggheadcafe.com
UnBlog:
http://petesbloggerama.blogspot.com




Show quoteHide quote
"patrick.san***@gmail.com" wrote:

> 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
>
>
Author
13 Jun 2006 6:21 AM
patrick.sannes
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);
                }
            }


        }

Show quoteHide quote
> 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.
>
> --
> Co-founder, Eggheadcafe.com developer portal:
> http://www.eggheadcafe.com
> UnBlog:
> http://petesbloggerama.blogspot.com
>
Author
13 Jun 2006 9:00 AM
patrick.sannes
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.


patrick.san***@gmail.com wrote:
Show quoteHide quote
> 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);
>                 }
>             }
>
>
>         }
>
> > 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.
> >
> > --
> > Co-founder, Eggheadcafe.com developer portal:
> > http://www.eggheadcafe.com
> > UnBlog:
> > http://petesbloggerama.blogspot.com
> >
Author
12 Jun 2006 4:09 PM
bruce barker (sqlwork.com)
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)



<patrick.san***@gmail.com> wrote in message
Show quoteHide quote
news:1150121845.313400.43670@h76g2000cwa.googlegroups.com...
> 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
>