CSS doesn't work on dynamicly added user controls

Discussion in 'ASP .Net' started by Henke, Dec 29, 2003.

  1. Henke

    Henke Guest

    Hi!

    I add some web user controls dynamicly with:

    myPanel.Controls.Add(Page.LoadControl("MyDynamiclyAddedPage.ascx"));
    the style of the controls on the user control is set from a css-class, by
    setting the CssClass-property, but they doesn't display properly.

    If the user control is added at design time everything looks correct.

    Any ideas?
    Thanks!
    /Henke
     
    Henke, Dec 29, 2003
    #1
    1. Advertising

  2. When you add a user control, the control ID may change. If you're using
    this ID in any of the styling, then the style will no longer work correctly.

    E.g.

    <style>
    #MyDiv .foo
    {
    color: #ff2200;
    }
    </style>

    <div id="MyDiv" runat="server" CssClass="foo">This is some text</div>

    This will work correctly if added directly at design time. However, if you
    put this into a user control, the ID will be changed to something like
    "MyUserControl_MyDiv". This will cause the style to stop working, as the
    style will only be applied to tags with the ID of "MyDiv".

    This might not be your case, but if you can post the style(s) not working,
    and a snippet of your code, we can check :)

    Hope this helps,

    Mun



    "Henke" <> wrote in message
    news:%...
    > Hi!
    >
    > I add some web user controls dynamicly with:
    >
    > myPanel.Controls.Add(Page.LoadControl("MyDynamiclyAddedPage.ascx"));
    > the style of the controls on the user control is set from a css-class, by
    > setting the CssClass-property, but they doesn't display properly.
    >
    > If the user control is added at design time everything looks correct.
    >
    > Any ideas?
    > Thanks!
    > /Henke
     
    Munsifali Rashid, Dec 29, 2003
    #2
    1. Advertising

  3. Henke

    Henke Guest

    Hi, and thaks for your quick answer.
    I should say I'm not that used to work with css-files but this is how it
    looks:
    In my css-file:
    ..Label
    {
    font-size: 10pt;
    font-family: 'Arial Narrow';
    }

    And than I just set the CssClass property on all my labels on the user
    control to Label.

    This is how one label looks in "HTML-file" at design time:
    <asp:label id="Label8" style="Z-INDEX: 104; LEFT: 8px; POSITION: absolute;
    TOP: 24px" runat="server" CssClass="Label">Name *</asp:label>

    An this is how the source looks when the page is displayed in a browser:
    <span id="dynamicControlHost__ctl0_Label8" class="Label" style="Z-INDEX:
    104; LEFT: 8px; POSITION: absolute; TOP: 24px">Name *</span>

    /Henke

    "Munsifali Rashid" <mun@**RemoveToReply**vefuk.com> skrev i meddelandet
    news:...
    > When you add a user control, the control ID may change. If you're using
    > this ID in any of the styling, then the style will no longer work

    correctly.
    >
    > E.g.
    >
    > <style>
    > #MyDiv .foo
    > {
    > color: #ff2200;
    > }
    > </style>
    >
    > <div id="MyDiv" runat="server" CssClass="foo">This is some text</div>
    >
    > This will work correctly if added directly at design time. However, if

    you
    > put this into a user control, the ID will be changed to something like
    > "MyUserControl_MyDiv". This will cause the style to stop working, as the
    > style will only be applied to tags with the ID of "MyDiv".
    >
    > This might not be your case, but if you can post the style(s) not working,
    > and a snippet of your code, we can check :)
    >
    > Hope this helps,
    >
    > Mun
    >
    >
    >
    > "Henke" <> wrote in message
    > news:%...
    > > Hi!
    > >
    > > I add some web user controls dynamicly with:
    > >
    > > myPanel.Controls.Add(Page.LoadControl("MyDynamiclyAddedPage.ascx"));
    > > the style of the controls on the user control is set from a css-class,

    by
    > > setting the CssClass-property, but they doesn't display properly.
    > >
    > > If the user control is added at design time everything looks correct.
    > >
    > > Any ideas?
    > > Thanks!
    > > /Henke

    >
    >
     
    Henke, Dec 29, 2003
    #3
  4. There doesn't appear to be anything obviously wrong with the code below.
    I'm not sure that 'Arial Narrow' should be in single quotes though - it
    should work fine without. However, that wouldn't explain why it works
    correctly at design time, but not as a user control.

    When you said it doesn't display correctly, what exactly happens? Is the
    font incorrect (eg. it appears too big, or is the wrong typeface), or is the
    style tag being ignored (eg. the user control is not obeying the absolute
    positioning and is not being displayed on the correct part of the page) ?

    Regards,

    Mun




    "Henke" <> wrote in message
    news:...
    > Hi, and thaks for your quick answer.
    > I should say I'm not that used to work with css-files but this is how it
    > looks:
    > In my css-file:
    > .Label
    > {
    > font-size: 10pt;
    > font-family: 'Arial Narrow';
    > }
    >
    > And than I just set the CssClass property on all my labels on the user
    > control to Label.
    >
    > This is how one label looks in "HTML-file" at design time:
    > <asp:label id="Label8" style="Z-INDEX: 104; LEFT: 8px; POSITION: absolute;
    > TOP: 24px" runat="server" CssClass="Label">Name *</asp:label>
    >
    > An this is how the source looks when the page is displayed in a browser:
    > <span id="dynamicControlHost__ctl0_Label8" class="Label" style="Z-INDEX:
    > 104; LEFT: 8px; POSITION: absolute; TOP: 24px">Name *</span>
    >
    > /Henke
     
    Munsifali Rashid, Dec 29, 2003
    #4
  5. Henke

    Henke Guest

    Both font-size and font-family is incorrect. But I noticed something else,
    some user controls get correctly displayed the first time they ar shown, but
    the second time the gets displayed the fonts are wrong.
    Does it matter if I put my user controls on panels or in place holders?
    Now I have a panel on which I loads new user controls depending on some menu
    selections.

    /Henke

    "Munsifali Rashid" <mun.news@#RemoveToReply#cordlessmouse.co.uk> skrev i
    meddelandet news:...
    > There doesn't appear to be anything obviously wrong with the code below.
    > I'm not sure that 'Arial Narrow' should be in single quotes though - it
    > should work fine without. However, that wouldn't explain why it works
    > correctly at design time, but not as a user control.
    >
    > When you said it doesn't display correctly, what exactly happens? Is the
    > font incorrect (eg. it appears too big, or is the wrong typeface), or is

    the
    > style tag being ignored (eg. the user control is not obeying the absolute
    > positioning and is not being displayed on the correct part of the page) ?
    >
    > Regards,
    >
    > Mun
    >
    >
    >
    >
    > "Henke" <> wrote in message
    > news:...
    > > Hi, and thaks for your quick answer.
    > > I should say I'm not that used to work with css-files but this is how it
    > > looks:
    > > In my css-file:
    > > .Label
    > > {
    > > font-size: 10pt;
    > > font-family: 'Arial Narrow';
    > > }
    > >
    > > And than I just set the CssClass property on all my labels on the user
    > > control to Label.
    > >
    > > This is how one label looks in "HTML-file" at design time:
    > > <asp:label id="Label8" style="Z-INDEX: 104; LEFT: 8px; POSITION:

    absolute;
    > > TOP: 24px" runat="server" CssClass="Label">Name *</asp:label>
    > >
    > > An this is how the source looks when the page is displayed in a browser:
    > > <span id="dynamicControlHost__ctl0_Label8" class="Label" style="Z-INDEX:
    > > 104; LEFT: 8px; POSITION: absolute; TOP: 24px">Name *</span>
    > >
    > > /Henke

    >
    >
     
    Henke, Dec 30, 2003
    #5
  6. It shouldn't matter whether your controls are in panels or placeholders.
    I've used both in the past, with CSS styling without any problems. Netscape
    does some weird things with stylesheets sometimes, but I'm assuming that
    you're using Internet Explorer, which shouldn't be a problem.

    Mun



    "Henke" <> wrote in message
    news:...
    > Both font-size and font-family is incorrect. But I noticed something else,
    > some user controls get correctly displayed the first time they ar shown,

    but
    > the second time the gets displayed the fonts are wrong.
    > Does it matter if I put my user controls on panels or in place holders?
    > Now I have a panel on which I loads new user controls depending on some

    menu
    > selections.
    >
    > /Henke
    >
    > "Munsifali Rashid" <mun.news@#RemoveToReply#cordlessmouse.co.uk> skrev i
    > meddelandet news:...
    > > There doesn't appear to be anything obviously wrong with the code below.
    > > I'm not sure that 'Arial Narrow' should be in single quotes though - it
    > > should work fine without. However, that wouldn't explain why it works
    > > correctly at design time, but not as a user control.
    > >
    > > When you said it doesn't display correctly, what exactly happens? Is

    the
    > > font incorrect (eg. it appears too big, or is the wrong typeface), or is

    > the
    > > style tag being ignored (eg. the user control is not obeying the

    absolute
    > > positioning and is not being displayed on the correct part of the page)

    ?
    > >
    > > Regards,
    > >
    > > Mun
     
    Munsifali Rashid, Dec 30, 2003
    #6
    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. Henke
    Replies:
    1
    Views:
    488
  2. Bennett Haselton
    Replies:
    1
    Views:
    543
    John M Deal
    Nov 8, 2004
  3. Anders K. Jacobsen [DK]

    Viewstate and dynamicly added controls

    Anders K. Jacobsen [DK], Mar 22, 2005, in forum: ASP .Net
    Replies:
    1
    Views:
    439
    Karl Seguin
    Mar 22, 2005
  4. Mike Housey

    Persistance of Dynamicly added user controls.

    Mike Housey, Aug 5, 2003, in forum: ASP .Net Web Controls
    Replies:
    1
    Views:
    136
    Ken Cox [Microsoft MVP]
    Aug 8, 2003
  5. Fraggle

    Reading data FROM a dynamicly added control

    Fraggle, Nov 12, 2003, in forum: ASP .Net Web Controls
    Replies:
    1
    Views:
    182
    Alvin Bruney
    Nov 12, 2003
Loading...

Share This Page