Hi, I have created user control that dynamically builds tabs based on a dictionary that is passed as a parameter. When user clicks a link button event is fired and parent’s method should be invoked which in turn loads other user controls and repaints tabs.
I have a classic problem with page lifecycle. I can't get these controls to work in predictable manner.
On page load event user control is rebound to page placeholder end the Event Handler then when user controls events are processed - TabMenu_Initialise function should be launched and should repaint the tab with new selection. I am going nuts.
=============================
User control: tab_menu.ascx.vb
=============================
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports System.Web.UI.HtmlControls
Partial Class tab_menu
Inherits System.Web.UI.UserControl
Dim Lst As New ListDictionary
Dim strSelection As String
Private _delClickHandler As System.Delegate
Private strFunction As String
Public WriteOnly Property MenuList() As ListDictionary
Set(ByVal value As ListDictionary)
Lst = value
End Set
End Property
Public WriteOnly Property Selection() As String
Set(ByVal value As String)
strSelection = value
End Set
End Property
Public WriteOnly Property ClickHandler() As System.Delegate
Set(ByVal value As System.Delegate)
_delClickHandler = value
End Set
End Property
Private Sub EnvokeClick(ByVal sender As Object, ByVal e As System.EventArgs)
Dim aObj(0) As Object
aObj(0) = sender.ID.ToString
_delClickHandler.DynamicInvoke(aObj)
End Sub
Protected Sub tbl_TabMenu_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles tbl_TabMenu.Load
Dim Cell As TableCell
Dim Row As New TableRow
Dim Link As LinkButton
Dim de As DictionaryEntry
For Each de In Lst
Cell = New TableCell
Link = New LinkButton
Cell.Text = de.Key
Cell.Width = 81
Cell.Height = 23
Cell.Style("text-align") = "center"
If de.Key = strSelection Then
Cell.Style("background-image") = "Images/tab_active.gif"
Else
Cell.Style("background-image") = "Images/tab_idle.gif"
End If
Link.ID = de.Key
Link.Text = de.Key
Link.DataBind()
AddHandler Link.Click, AddressOf EnvokeClick
Cell.Controls.Add(Link)
Row.Controls.Add(Cell)
Next de
tbl_TabMenu.Controls.Add(Row)
End Sub
End Class
=============================
Parent:
=============================
Imports System.IO
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports System.Web.HttpContext
Imports System.Web.UI.HtmlControls
Partial Class _Default
Inherits System.Web.UI.Page
Delegate Sub _delTabHandler(ByVal myStr As String)
Dim delUpdate As Object
Dim oCtrlTabMenu As ASP.tab_menu = CType(LoadControl("tab_menu.ascx"), ASP.tab_menu)
Protected oCtrlCompanyForm As ASP.ecompanyaddresstab_ascx = CType(LoadControl("eCompanyAddressTab.ascx"), ASP.ecompanyaddresstab_ascx)
Protected oCtrlCompProf As ASP.ecompanyprofile_ascx = CType(LoadControl("eCompanyProfile.ascx"), ASP.ecompanyprofile_ascx)
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Dim str As String = ""
TabMenu_Initialise("Company")
End Sub
Sub Company_Click(ByVal str As String)
phForm.Controls.Clear()
phForm.Controls.Add(oCtrlCompanyForm)
RenderTabControl(str)
End Sub
Sub Profile_Click(ByVal str As String)
phForm.Controls.Clear()
phForm.Controls.Add(oCtrlCompProf)
RenderTabControl(str)
End Sub
Sub Logos_Click(ByVal str As String)
phForm.Controls.Clear()
RenderTabControl(str)
End Sub
Sub TabMenu_Initialise(ByVal str As String)
Select Case str
Case "Profile" : Profile_Click(str)
Case "Logos" : Logos_Click(str)
Case Else
Company_Click("Company")
End Select
End Sub
Protected Sub RenderTabControl(ByVal strSelection As String)
delUpdate = New _delTabHandler(AddressOf TabMenu_Initialise)
Dim Lst As New ListDictionary
Lst.Add("Company", "")
Lst.Add("Profile", "")
Lst.Add("Logos", "")
oCtrlTabMenu.Selection = strSelection
oCtrlTabMenu.MenuList = Lst
oCtrlTabMenu.ClickHandler = delUpdate
phTabMenu.Controls.Clear()
phTabMenu.Controls.Add(oCtrlTabMenu)
End Sub
End Class
I have a classic problem with page lifecycle. I can't get these controls to work in predictable manner.
On page load event user control is rebound to page placeholder end the Event Handler then when user controls events are processed - TabMenu_Initialise function should be launched and should repaint the tab with new selection. I am going nuts.
=============================
User control: tab_menu.ascx.vb
=============================
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports System.Web.UI.HtmlControls
Partial Class tab_menu
Inherits System.Web.UI.UserControl
Dim Lst As New ListDictionary
Dim strSelection As String
Private _delClickHandler As System.Delegate
Private strFunction As String
Public WriteOnly Property MenuList() As ListDictionary
Set(ByVal value As ListDictionary)
Lst = value
End Set
End Property
Public WriteOnly Property Selection() As String
Set(ByVal value As String)
strSelection = value
End Set
End Property
Public WriteOnly Property ClickHandler() As System.Delegate
Set(ByVal value As System.Delegate)
_delClickHandler = value
End Set
End Property
Private Sub EnvokeClick(ByVal sender As Object, ByVal e As System.EventArgs)
Dim aObj(0) As Object
aObj(0) = sender.ID.ToString
_delClickHandler.DynamicInvoke(aObj)
End Sub
Protected Sub tbl_TabMenu_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles tbl_TabMenu.Load
Dim Cell As TableCell
Dim Row As New TableRow
Dim Link As LinkButton
Dim de As DictionaryEntry
For Each de In Lst
Cell = New TableCell
Link = New LinkButton
Cell.Text = de.Key
Cell.Width = 81
Cell.Height = 23
Cell.Style("text-align") = "center"
If de.Key = strSelection Then
Cell.Style("background-image") = "Images/tab_active.gif"
Else
Cell.Style("background-image") = "Images/tab_idle.gif"
End If
Link.ID = de.Key
Link.Text = de.Key
Link.DataBind()
AddHandler Link.Click, AddressOf EnvokeClick
Cell.Controls.Add(Link)
Row.Controls.Add(Cell)
Next de
tbl_TabMenu.Controls.Add(Row)
End Sub
End Class
=============================
Parent:
=============================
Imports System.IO
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports System.Web.HttpContext
Imports System.Web.UI.HtmlControls
Partial Class _Default
Inherits System.Web.UI.Page
Delegate Sub _delTabHandler(ByVal myStr As String)
Dim delUpdate As Object
Dim oCtrlTabMenu As ASP.tab_menu = CType(LoadControl("tab_menu.ascx"), ASP.tab_menu)
Protected oCtrlCompanyForm As ASP.ecompanyaddresstab_ascx = CType(LoadControl("eCompanyAddressTab.ascx"), ASP.ecompanyaddresstab_ascx)
Protected oCtrlCompProf As ASP.ecompanyprofile_ascx = CType(LoadControl("eCompanyProfile.ascx"), ASP.ecompanyprofile_ascx)
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Dim str As String = ""
TabMenu_Initialise("Company")
End Sub
Sub Company_Click(ByVal str As String)
phForm.Controls.Clear()
phForm.Controls.Add(oCtrlCompanyForm)
RenderTabControl(str)
End Sub
Sub Profile_Click(ByVal str As String)
phForm.Controls.Clear()
phForm.Controls.Add(oCtrlCompProf)
RenderTabControl(str)
End Sub
Sub Logos_Click(ByVal str As String)
phForm.Controls.Clear()
RenderTabControl(str)
End Sub
Sub TabMenu_Initialise(ByVal str As String)
Select Case str
Case "Profile" : Profile_Click(str)
Case "Logos" : Logos_Click(str)
Case Else
Company_Click("Company")
End Select
End Sub
Protected Sub RenderTabControl(ByVal strSelection As String)
delUpdate = New _delTabHandler(AddressOf TabMenu_Initialise)
Dim Lst As New ListDictionary
Lst.Add("Company", "")
Lst.Add("Profile", "")
Lst.Add("Logos", "")
oCtrlTabMenu.Selection = strSelection
oCtrlTabMenu.MenuList = Lst
oCtrlTabMenu.ClickHandler = delUpdate
phTabMenu.Controls.Clear()
phTabMenu.Controls.Add(oCtrlTabMenu)
End Sub
End Class