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