Results 1 to 16 of 16

Thread: User controls

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Jan 2001
    Location
    Denmark
    Posts
    1,049

    Question User controls

    I have a form which uses 50 different user controls. The existing code includes all 50 user controls on the form, and they are activated by setting UserControl.Visible = true.
    But this results in a bad performance. My question is: Is it possible not to place all the user controls at the webform but instead load the user controls when they are needed? Maybe using a placeholder instead?

    I have attatched a file with the design mode. It is obvious that the applcation will have bad performance.
    Attached Images Attached Images  

  2. #2
    Big D Danial's Avatar
    Join Date
    Jul 2000
    Location
    ASP.Net Forum
    Posts
    2,877

    Re: User controls

    Yes you should use Placeholder and load the UserControl Dynamically as and when it is needed.

    E.G.

    Top of your page..
    Code:
    <%@ Reference Control = "WebUserControl1.ascx" %>

    Code:
    WebUserControl1 uc = 
          (WebUserControl1) Page.LoadControl("WebUserControl1.ascx"); 
        PlaceHolder1.Controls.Add(uc);
    Hope it helps.
    [VBF RSS Feed]

    There is a great war coming. Are you sure you are on the right side? Atleast I have chosen a side.

    If I have been helpful, Please Rate my Post. Thanks.

    This post was powered by :

  3. #3

    Thread Starter
    Frenzied Member
    Join Date
    Jan 2001
    Location
    Denmark
    Posts
    1,049

    Re: User controls

    Thanks a lot, I still have a small problem:
    I am not using c#, what will the code be in VB.Net?

  4. #4

    Thread Starter
    Frenzied Member
    Join Date
    Jan 2001
    Location
    Denmark
    Posts
    1,049

    Re: User controls

    Now I've added this code:
    Code:
    Public Class WebForm1
        Inherits System.Web.UI.Page
        Protected WithEvents Button1 As System.Web.UI.WebControls.Button
        Protected WithEvents Button2 As System.Web.UI.WebControls.Button
        Protected WithEvents PlaceHolder1 As System.Web.UI.WebControls.PlaceHolder
    
        Protected WithEvents UC1 As WebUserControl1
        Protected WithEvents UC2 As WebUserControl2
    
    
    #Region " Web Form Designer Generated Code "
    
        'This call is required by the Web Form Designer.
        <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
    
        End Sub
    
        Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init
            'CODEGEN: This method call is required by the Web Form Designer
            'Do not modify it using the code editor.
            InitializeComponent()
        End Sub
    
    #End Region
    
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            PlaceHolder1.Controls.Add(UC1)
        End Sub
    
    End Class
    But I receive an error when I click the button:
    Code:
    Value cannot be null. Parameter name: child

  5. #5
    Big D Danial's Avatar
    Join Date
    Jul 2000
    Location
    ASP.Net Forum
    Posts
    2,877

    Re: User controls

    Sorry for posting C# code, Here is the VB.Net version, and it works fine, i have tested it.

    VB Code:
    1. Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    2.         Dim uc As ucOne
    3.         Dim uc2 As ucTwo
    4.  
    5.         uc = CType(Page.LoadControl("ucOne.ascx"), ucOne)
    6.         uc2 = CType(Page.LoadControl("ucTwo.ascx"), ucTwo)
    7.  
    8.         Me.PlaceHolder1.Controls.Add(uc)
    9.         Me.PlaceHolder1.Controls.Add(uc2)
    10.  
    11.  
    12.     End Sub
    [VBF RSS Feed]

    There is a great war coming. Are you sure you are on the right side? Atleast I have chosen a side.

    If I have been helpful, Please Rate my Post. Thanks.

    This post was powered by :

  6. #6

    Thread Starter
    Frenzied Member
    Join Date
    Jan 2001
    Location
    Denmark
    Posts
    1,049

    Re: User controls

    GREAT, one last question:
    If I have a text field in each of the two user controls, how can I keep the information when I click the buttons?
    Is there a smarter way than using sessions for this? It's quite important to me because I have 50 different calculation user controls and I need two switch between them, and I don't want to recalc every time I have loaded a new. Please see the attatched file.
    Attached Images Attached Images  

  7. #7
    Big D Danial's Avatar
    Join Date
    Jul 2000
    Location
    ASP.Net Forum
    Posts
    2,877

    Re: User controls

    Do you mean in Postback?If the viewstate of your textbox is set to true, then the value should not reset, unless you are assigning the value dynamically. If so then you only assign in when there is no postback, e.g
    if Not Page.IsPostBack Then
    fill text boxes with value
    end if

    Hope it make sense.

    Good luck.
    [VBF RSS Feed]

    There is a great war coming. Are you sure you are on the right side? Atleast I have chosen a side.

    If I have been helpful, Please Rate my Post. Thanks.

    This post was powered by :

  8. #8

    Thread Starter
    Frenzied Member
    Join Date
    Jan 2001
    Location
    Denmark
    Posts
    1,049

    Re: User controls

    Yes that's exactly what I mean. Where do I set the viewstate? I can find no property with that name.

  9. #9
    Big D Danial's Avatar
    Join Date
    Jul 2000
    Location
    ASP.Net Forum
    Posts
    2,877

    Re: User controls

    The property is called "EnableViewState".
    [VBF RSS Feed]

    There is a great war coming. Are you sure you are on the right side? Atleast I have chosen a side.

    If I have been helpful, Please Rate my Post. Thanks.

    This post was powered by :

  10. #10

    Thread Starter
    Frenzied Member
    Join Date
    Jan 2001
    Location
    Denmark
    Posts
    1,049

    Re: User controls

    Quote Originally Posted by Danial
    The property is called "EnableViewState".
    Well sorry for disturbing you but I still have the problem. The text boxes are both empty when I click the buttons although I write some text in the textboxes.
    HTML:
    Code:
    Some text<asp:TextBox id=TextBox1 runat="server" EnableViewState=True></asp:TextBox>
    VBCode:
    Code:
    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
            Page.Controls.Add(c2)
            PlaceHolder1.Controls.Add(c2)
        End Sub

  11. #11

    Thread Starter
    Frenzied Member
    Join Date
    Jan 2001
    Location
    Denmark
    Posts
    1,049

    Re: User controls

    This is my code in zip format, see attatched file
    Attached Files Attached Files

  12. #12
    Big D Danial's Avatar
    Join Date
    Jul 2000
    Location
    ASP.Net Forum
    Posts
    2,877

    Re: User controls

    When you are switching between two controls by using Placeholder1.Controls.Add the item that was there before is getting unloaded. So any values you type will be lost. You will have to save those value in Cookies/Session and Restore it when you switch back to that control again. ViewState is only for PostBack..
    [VBF RSS Feed]

    There is a great war coming. Are you sure you are on the right side? Atleast I have chosen a side.

    If I have been helpful, Please Rate my Post. Thanks.

    This post was powered by :

  13. #13

    Thread Starter
    Frenzied Member
    Join Date
    Jan 2001
    Location
    Denmark
    Posts
    1,049

    Re: User controls

    Quote Originally Posted by Danial
    When you are switching between two controls by using Placeholder1.Controls.Add the item that was there before is getting unloaded. So any values you type will be lost. You will have to save those value in Cookies/Session and Restore it when you switch back to that control again. ViewState is only for PostBack..
    OK, I was worried about that, but that's the only way. I have 50 user controls so there will be a lot of sessions. Are there any rule of thumb of the number of sessions before the application will be slower?

  14. #14
    Big D Danial's Avatar
    Join Date
    Jul 2000
    Location
    ASP.Net Forum
    Posts
    2,877

    Re: User controls

    I always try not to use Session variable for storing data, only case i use session variable is for Validation only. It will all depend on the number of concurrent users you have. If the concurrent user number is low then you wont have too much problem. My rule is that never store objects in Session, e.g DataSet, Large Array etc.. If its few string values you can get away with it.

    You could try cookies instead of session then you would take the load out of the server to the client..
    [VBF RSS Feed]

    There is a great war coming. Are you sure you are on the right side? Atleast I have chosen a side.

    If I have been helpful, Please Rate my Post. Thanks.

    This post was powered by :

  15. #15

    Thread Starter
    Frenzied Member
    Join Date
    Jan 2001
    Location
    Denmark
    Posts
    1,049

    Re: User controls

    OK, I will use Cookies instead, because the number of concurrent user is quite high. Thanks for your help

  16. #16
    Big D Danial's Avatar
    Join Date
    Jul 2000
    Location
    ASP.Net Forum
    Posts
    2,877

    Re: User controls

    You are welcome. Apart from cookie, you could try putting some hidden field in host form and store the values in hidden input field or Hidden Controls such as ListBox etc. I uses this technique quite a lot, since user might have cookies disabled.

    Hope my suggestions helped.
    [VBF RSS Feed]

    There is a great war coming. Are you sure you are on the right side? Atleast I have chosen a side.

    If I have been helpful, Please Rate my Post. Thanks.

    This post was powered by :

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width