Custom Control Width sometimes has a value...sometimes doesn't

Discussion in 'ASP .Net Building Controls' started by paul reed, Feb 17, 2004.

  1. paul reed

    paul reed Guest

    Hello,

    My custom control is acting strangely.

    In my CreateChildControls method after creating my control, I wrap it in Div
    tags... with something like:

    Dim divStr As String = "<DIV STYLE=""overflow: auto; width: " &
    Me.Width.ToString & _
    "; height: " & Me.Height.ToString &
    """>"

    Controls.Add(New LiteralControl(divStr))
    Controls.Add(_treeControl)
    Controls.Add(New LiteralControl("</DIV>"))

    I noticed though that I wasn't getting horizontal scroll bars (but was
    getting vertical scroll bars). So, I got into debug and sure enough, the
    Me.Width was 0px. However, sometimes I run the same page with the same
    control and everything is fine...Height and Width have values and I do get
    both scroll bars.

    I have been very diligent to call EnsureChildControls() where appropriate as
    I understand from other posts that many times things get "weird" if you
    don't follow the correct protocol.

    I am stumped.

    Thanks,

    Paul
     
    paul reed, Feb 17, 2004
    #1
    1. Advertisements

  2. Hi Paul,

    Thank you for posting in the community!

    Based on my understanding, you use composite control in webform. You used
    <div> tag as child control, which takes width and height properties. But
    these 2 properties sometimes work well and sometimes does not initialize to
    0.

    ============================================
    Based on my research, I suspect that the problem may occur if your
    CreateChildControls method is called too early(Before the Width and Height
    properties are set).

    I suppose you place your custom control on the webform, so its Width and
    Height properties are retrieved from html view by the html page parser.

    Each webcontrol on the webform has a default ControlBuilder associated,
    which interacts with the html page parser to parse the html code into web
    control instance.

    ControlBuilder will parse the html code and collect all the attribute of
    the control, such as Width and Height properties. The ControlBuilder
    happens right after the creation of the webcontrol and before the Init of
    the control.

    So your problem may occur:
    public class childcontrol : System.Web.UI.WebControls.WebControl
    {
    public override Unit Width
    {
    get
    {
    this.EnsureChildControls();
    return base.Width;
    }
    set
    {
    this.EnsureChildControls();
    base.Width = value;
    }
    }

    protected override void CreateChildControls()
    {
    base.CreateChildControls ();
    string divstr="<DIV STYLE=\"overflow: auto; width: "
    +this.Width.ToString() +"; height: " + this.Height.ToString() +"\">";
    Button b=new Button();
    b.Text="Button";
    b.ID="bt";
    b.Width=300;
    b.Height=300;
    this.Controls.Add(new LiteralControl(divstr));
    this.Controls.Add(b);
    this.Controls.Add(new LiteralControl("</div>"));
    }
    }

    <cc1:childcontrol id="Childcontrol1" style="Z-INDEX: 101; LEFT: 184px;
    POSITION: absolute; TOP: 48px" runat="server" Width="160px"
    Height="160px"></cc1:childcontrol>

    Because before the initialize of the Width and Height properties, you have
    called EnsureChildControls to make CreateChildControls fire too early.

    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    And, normally, this will work well:

    public class childcontrol : System.Web.UI.WebControls.WebControl
    {
    protected override void CreateChildControls()
    {
    base.CreateChildControls ();
    string divstr="<DIV STYLE=\"overflow: auto; width: "
    +this.Width.ToString() +"; height: " + this.Height.ToString() +"\">";
    Button b=new Button();
    b.Text="Button";
    b.ID="bt";
    b.Width=300;
    b.Height=300;
    this.Controls.Add(new LiteralControl(divstr));
    this.Controls.Add(b);
    this.Controls.Add(new LiteralControl("</div>"));
    }
    }

    <cc1:childcontrol id="Childcontrol1" style="Z-INDEX: 101; LEFT: 184px;
    POSITION: absolute; TOP: 48px" runat="server" Width="160px"
    Height="160px"></cc1:childcontrol>

    I think this may be your problem, you should check the improper
    EnsureChildControls call in your code.
    But if this is not the problem, please provide me the steps and more
    detailed information to reproduce your problem, so that I can help you
    better.

    ========================================================
    Please apply my suggestion above and let me know if it helps resolve your
    problem.

    Thank you for your patience and cooperation. If you have any questions or
    concerns, please feel free to post it in the group. I am standing by to be
    of assistance.
    Have a nice day!!

    Btw: In your another post "Dynamically Building Property Values for
    Designer", I think "Alessandro Zifiglio" has provided you a solid and well
    solution for your issue. Is your problem resolved? If you still have
    concern, please feel free to tell me, I will help you. :)

    Best regards,
    Jeffrey Tan
    Microsoft Online Partner Support
    Get Secure! - www.microsoft.com/security
    This posting is provided "as is" with no warranties and confers no rights.
     
    Jeffrey Tan[MSFT], Feb 18, 2004
    #2
    1. Advertisements

  3. paul reed

    Paul Reed Guest

    Paul Reed, Feb 18, 2004
    #3
  4. paul reed

    Paul Reed Guest

    Jeffrey,

    Hello...

    I am not quite sure what your telling me to do? You showed 2 different
    CreateChildControl methods...but they are identical. Maybe I am missing
    something. Here are some more details:

    1. I don't expose in my control public Width and Height properties...so
    I don't call EnsureChildControls for Width and Height. Are you saying,
    maybe I need these but DON'T call EnsureChildControls().
    2. I notice in your CreateChildControls you have a call to
    "base.CreateChildControls()". Do I need this for my VB implementation
    (e.g., MyBase.CreateChildControls())? Because I don't have that.

    Thanks,


    Paul Reed
    www.jacksonreed.com
     
    Paul Reed, Feb 18, 2004
    #4
  5. Hi Paul,

    Thanks very much for your feedback.

    Based on your feedback, you did not invoke EnsureChildControls, so I think
    my original suppose may not be the problem.

    I think MyBase.CreateChildControls() is also not the problem, because I
    have tried:

    Public Class createchildcontroldivcontrol
    Inherits System.Web.UI.WebControls.WebControl

    Protected Overrides Sub CreateChildControls()
    Dim divStr As String = "<DIV STYLE=""overflow: auto; width: " &
    Me.Width.ToString & "; height: " & Me.Height.ToString & """>"
    Dim b As Button = New Button
    b.Text = "Button"
    b.ID = "bt"
    b.Width = New Unit(300)
    b.Height = New Unit(300)
    Controls.Add(New LiteralControl(divStr))
    Controls.Add(b)
    Controls.Add(New LiteralControl("</DIV>"))
    End Sub
    End Class

    It works well. You may try this code snippet at your side to see if it
    works.(I think it should work, so I think the problem should be other place)

    I think you need to provide some code snippet and reproduce steps to
    reproduce your problem for me.

    Additionally, I want to note you that:
    For "overflow: auto" attribute, div's content is clipped and scrolling is
    added only when necessary(content exceeds the dimensions of the div.)
    That is, if your TreeView's width or height is small than the div's Width
    and Height, then no scrollbar will display. But if you use "overflow:
    scroll", then scrollbar will always display.

    =================================================
    Thank you for your patience and cooperation. If you have any questions or
    concerns, please feel free to post it in the group. I am standing by to be
    of assistance.

    Best regards,
    Jeffrey Tan
    Microsoft Online Partner Support
    Get Secure! - www.microsoft.com/security
    This posting is provided "as is" with no warranties and confers no rights.
     
    Jeffrey Tan[MSFT], Feb 19, 2004
    #5
  6. paul reed

    Paul Reed Guest

    Jeffrey,

    How do I post my code to you? I use a 3rd party control as my tree so
    probably aren't going to be able to run the control.

    Yes, your explaination of overflow is as I understood it as well.

    Thanks.

    Paul Reed
    www.jacksonreed.com
     
    Paul Reed, Feb 19, 2004
    #6
  7. Hi Paul,

    Thanks for your feedback.

    Oh, you still did not confirm that whether use a normal web control in the
    <div> works ? I suppose that my code snippet works well.

    Based on your statement, your treeview is a third party control, so the
    problem should be related with that third party control.(You can determine
    through replace your third party control with a normal control with the
    same width and heigth value)

    If you can determine the problem is due to the third party control, I think
    you should consult with the corporation of that third party control to find
    support.

    If the problem is not due to the third party control, please feel free to
    feedback. I think a reproduce project Non-related with that third party
    control. Then I can help you.

    Thanks

    Best regards,
    Jeffrey Tan
    Microsoft Online Partner Support
    Get Secure! - www.microsoft.com/security
    This posting is provided "as is" with no warranties and confers no rights.
     
    Jeffrey Tan[MSFT], Feb 20, 2004
    #7
  8. paul reed

    Paul Reed Guest

    Jeffrey,

    Right now the control seems to be well behaved. I am noticing that I
    seem to see this behavior when in debug mode versus running it like
    http:\\localhost\blahblah.


    Paul Reed
    www.jacksonreed.com
     
    Paul Reed, Feb 20, 2004
    #8
  9. Hi Paul,

    Thanks very much for your feedback.

    I am glad it now works well :).

    Anyway, if you have any further problem, please feel free to post, I will
    help you.

    Best regards,
    Jeffrey Tan
    Microsoft Online Partner Support
    Get Secure! - www.microsoft.com/security
    This posting is provided "as is" with no warranties and confers no rights.
     
    Jeffrey Tan[MSFT], Feb 21, 2004
    #9
    1. Advertisements

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 (here). After that, you can post your question and our members will help you out.