WebControl with CSS and Javascript

Discussion in 'ASP .Net Building Controls' started by patrick.sannes@gmail.com, Jun 12, 2006.

  1. Guest

    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
     
    , Jun 12, 2006
    #1
    1. Advertising

  2. 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




    "" 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
    >
    >
     
    Peter Bromberg [C# MVP], Jun 12, 2006
    #2
    1. Advertising

  3. 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)



    <> wrote in message
    news:...
    > 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
    >
     
    bruce barker \(sqlwork.com\), Jun 12, 2006
    #3
  4. Guest

    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
    >
     
    , Jun 13, 2006
    #4
  5. Guest

    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.


    wrote:
    > 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
    > >
     
    , Jun 13, 2006
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Guest
    Replies:
    2
    Views:
    742
  2. Replies:
    4
    Views:
    6,690
  3. Luis Ramírez.

    Can a webcontrol (webcontrol.dll) have a configuration file?

    Luis Ramírez., Jul 5, 2004, in forum: ASP .Net Building Controls
    Replies:
    2
    Views:
    207
    Luis Ramírez
    Jul 6, 2004
  4. Kasabaarde Sumta

    WebControl inside of WebControl and attaching events

    Kasabaarde Sumta, Dec 13, 2004, in forum: ASP .Net Building Controls
    Replies:
    0
    Views:
    203
    Kasabaarde Sumta
    Dec 13, 2004
  5. WebControl with CSS and Javascript

    , Jun 12, 2006, in forum: ASP .Net Web Controls
    Replies:
    4
    Views:
    129
Loading...

Share This Page