locked
embedded javascript resource not visible when the custom control is dropped in user control RRS feed

  • Question

  • User-1221966532 posted

     Hi,

    As I am working on an AJAXified web application, got to the point that using user controls to provide dynamic page navigation. I have a custom web control with embedded CSS and javascript file and I am dropping this custom control on the user control. Now for some reason the javascript is not visible while the CSS file is visible and applied to the user control when I run the application.I tested the custom control on a simple aspx page and it works, but for some reason when I drop on a user control, only the CSS file is applied. In the custom control I am getting the webresource for both files using GetWebResourceUrl() and then just simply writing it to the HtmlTextWriter.

    Is there a special way to embedding/applying a javascript for custom controls when used in user controls or I miss something here?

    cheers.
     

    Tuesday, May 13, 2008 11:06 AM

Answers

  • User1397122928 posted

    Hmm ok thats another problem. I have searched around abit maybe this article helps you. Sorry for misunderstanding you.

     http://weblogs.asp.net/dwahlin/archive/2007/04/29/creating-custom-asp-net-server-controls-with-embedded-javascript.aspx

    Greetings 

     


     

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, May 13, 2008 12:01 PM

All replies

  • User1397122928 posted

    Yes there is a special way to embed javascript in usercontrols because the usercontrol and the page which consumes the usercontrols get one unit. So one way you can solve your problem is to write you javascript into the page OR you create the javascript dynamically via code:

    Small example:

     StringBuilder strBld = new StringBuilder();
            strBld.AppendLine("function endScript(){");
            strBld.AppendLine("alert('Come visit us again');");
            strBld.AppendLine("}");
            ClientScript.RegisterClientScriptBlock(this.GetType(), "endScript", strBld.ToString(), true);

    Tuesday, May 13, 2008 11:24 AM
  • User-1221966532 posted

    Hi Armin,

    Well, that is not a solution! The custom web control is an independent .dll  file, meaning the CSS, javascript, or whatever else (images, etc) are baked into the control and there is no necessary to register on pages where you use the control.That is the meaning of the custom control that you can reuse without any additional code.

    What you mean under special way  to embed javascript in user controls?To my understanding you can use the same way as alsewhere ( register from design or code behind ) 

    Tuesday, May 13, 2008 11:39 AM
  • User1397122928 posted

    Hmm ok thats another problem. I have searched around abit maybe this article helps you. Sorry for misunderstanding you.

     http://weblogs.asp.net/dwahlin/archive/2007/04/29/creating-custom-asp-net-server-controls-with-embedded-javascript.aspx

    Greetings 

     


     

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, May 13, 2008 12:01 PM
  • User-1221966532 posted

     Armin,

    I do not have problem with embedding the webresources and rechecked in my custom web control that the webresource path is valid and indeed pointing to the Javascript file. However, the proiblem was caused by the fact that the user control in which the web custom control is sitting, was placed in a table structure which is set to tbl.Visible = false by default and then reset to Visible = true, and the user control was visible, but for some reason the javascript webresource could not been add to the page using the Visible attribute. INTERESTING is, that the CSS file was handled the same way (as a webresource) and that was indeed applied to the page even that I was using the Visible attribute.

    Quick fix: now on using the tbl.Style.Add("display","none") style attribute to make the parent table structure to visible/not visible and the javascript webresource is successfully pushed to the page.

    Still I dont see why a CSS webresource is handled differently from a javascript file? 

    Thursday, May 15, 2008 6:02 AM
  • User-16411453 posted

    Embedded  CSS files or CSS URLs write to the head tag section of the form. Usually you just register the JavaScript at the beginning of your control code. The ClientScript Manger will take care of script positioning on the form

      

    Dim cs As ClientScriptManager = Me.Page.ClientScript
    Dim rsType As Type = Me.GetType()
    'Register the javascript
    If (Not cs.IsClientScriptIncludeRegistered(rsType, "savedItems")) Then
    cs.RegisterClientScriptInclude("savedItems", cs.GetWebResourceUrl(rsType, "ICE5Software.DynamicCartV2.savedItems.js"))
    End If
      

     

    Thursday, May 15, 2008 7:22 PM