views:

93

answers:

2

I added 3 tree views in my master page, i want them to show them based on different conditions, but they are not visible on other pages.

Code:

    Dim TreeView1 As System.Web.UI.WebControls.TreeView = New TreeView
    Dim TreeView2 As System.Web.UI.WebControls.TreeView = New TreeView
    Dim TreeView3 As System.Web.UI.WebControls.TreeView = New TreeView
    Dim str As String = HttpContext.Current.Profile.GetPropertyValue("UserType")
    Select Case (str)
        Case "Faculty"
            Response.Write("Faculty")
            TreeView1.Visible = True
            Exit Select
        Case "Admin"
            Response.Write("Admin")
            TreeView2.Visible = True
            Exit Select
        Case "Student"
            Response.Write("Student")
            TreeView3.Visible = True
            Exit Select
    End Select

Please let me know how to resolve this problem.

+1  A: 

Call Controls.Add(TreeView1).

If this doesn't help, please provide more detail.

SLaks
The control collection cannot be modified during DataBind, Init, Load, PreRender or Unload phases.
sunita
second answer give this errorControl 'ctl00_ctl02' of type 'TreeView' must be placed inside a form tag with runat=server.
sunita
its give error Control 'ctl00_ctl02' of type 'TreeView' must be placed inside a form tag with runat=server.
sunita
Did you read the error? Put it inside a `<form runat="server">`.
SLaks
+3  A: 

Do this.

Dim TreeView1 As System.Web.UI.WebControls.TreeView = New TreeView
Dim TreeView2 As System.Web.UI.WebControls.TreeView = New TreeView
Dim TreeView3 As System.Web.UI.WebControls.TreeView = New TreeView
Dim str As String = HttpContext.Current.Profile.GetPropertyValue("UserType")
Select Case (str)
    Case "Faculty"
        Response.Write("Faculty")
        TreeView1.Visible = True
        Page.Controls.Add(Treeview1)
        Exit Select
    Case "Admin"
        Response.Write("Admin")
        TreeView2.Visible = True
        Page.Controls.Add(Treeview2)
        Exit Select
    Case "Student"
        Response.Write("Student")
        TreeView3.Visible = True
        Page.Controls.Add(Treeview3)
        Exit Select
End Select
RJ1516
its give error The control collection cannot be modified during DataBind, Init, Load, PreRender or Unload phases.
sunita