Hi, I have created user control that raises an event from dynamicaly generated buttons that should be picked up by the parent,
however, event fires every second postback and I have no idea why.
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
MsgBox(strSelection & ":Child")
For Each de In Lst
Cell = New TableCell
Link = New LinkButton
If de.Key = strSelection Then
Cell.Style("background-image") = "Images/tab_active.gif"
Else
Cell.Style("background-image") = "Images/tab_idle.gif"
End If
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 strSelection As String
Dim WithEvents 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_Init(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Init
ReloadControl("")
End Sub
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Not IsPostBack Then
' RetrieveCompany()
End If
End Sub
Sub Company_Click(ByVal str As String)
phForm.Controls.Clear()
phForm.Controls.Add(oCtrlCompanyForm)
Sub TabMenu_Initialise(ByVal sender As Object, ByVal e As System.EventArgs) Handles oCtrlTabMenu.Updated
Dim str As String = sender.id.ToString
MsgBox("TBINIT: " & str)
Select Case str
Case "Profile" : Profile_Click(str)
Case "Logos" : Logos_Click(str)
Case "Company" : Company_Click(str)
End Select
End Sub
Sub ReloadControl(ByVal str As String)
Dim Lst As New ListDictionary
Lst.Add("Company", "")
Lst.Add("Profile", "")
Lst.Add("Logos", "")
wmin
Member
2 Points
3 Posts
Dynamic User Control - RaiseEvent Fires every second postback
Feb 09, 2009 06:12 PM|LINK
Hi, I have created user control that raises an event from dynamicaly generated buttons that should be picked up by the parent, however, event fires every second postback and I have no idea why.
=============================
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
Public Event Updated(ByVal sender As Object, ByVal e As EventArgs)
Private Lst As New ListDictionary
Private strSelection 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
Private Sub EvokeClick(ByVal sender As Object, ByVal e As System.EventArgs)
MsgBox("Raising Event")
RaiseEvent Updated(sender, e)
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
MsgBox(strSelection & ":Child")
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
AddHandler Link.Click, AddressOf EvokeClick
Link.DataBind()
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 strSelection As String
Dim WithEvents 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_Init(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Init
ReloadControl("")
End Sub
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Not IsPostBack Then
' RetrieveCompany()
End If
End Sub
Sub Company_Click(ByVal str As String)
phForm.Controls.Clear()
phForm.Controls.Add(oCtrlCompanyForm)
ReloadControl(str)
End Sub
Sub Profile_Click(ByVal str As String)
phForm.Controls.Clear()
phForm.Controls.Add(oCtrlCompProf)
ReloadControl(str)
End Sub
Sub TabMenu_Initialise(ByVal sender As Object, ByVal e As System.EventArgs) Handles oCtrlTabMenu.Updated
Dim str As String = sender.id.ToString
MsgBox("TBINIT: " & str)
Select Case str
Case "Profile" : Profile_Click(str)
Case "Logos" : Logos_Click(str)
Case "Company" : Company_Click(str)
End Select
End Sub
Sub ReloadControl(ByVal str As String)
Dim Lst As New ListDictionary
Lst.Add("Company", "")
Lst.Add("Profile", "")
Lst.Add("Logos", "")
oCtrlTabMenu.Selection = str
oCtrlTabMenu.MenuList = Lst
'phTabMenu.Controls.Clear()
phTabMenu.Controls.Add(oCtrlTabMenu)
End Sub
VB .NET ASP.NET event usercontrol asp.net 2.0 asp.net 2.0 VB 2008 asp.net2.0 control Delegation User Controls Events RaiseEvent
wmin
Member
2 Points
3 Posts
Re: Dynamic User Control - RaiseEvent Fires every second postback
Feb 10, 2009 12:22 PM|LINK
The soulution seems to be here: http://stackoverflow.com/questions/310002/events-on-aspnet-usercontrol-raise-every-other-click
It looks like the problem is arising because my user control is recreated on each page reload - instead of being rebound.
How do i rebind it?
vb.net ASP.NET rebinding user control fire event postback
ixtli
Participant
1812 Points
318 Posts
Re: Dynamic User Control - RaiseEvent Fires every second postback
Feb 16, 2009 02:56 AM|LINK
TRY TO LOAD THE USERCONTROL AT PAGE_PREINIT EVENT
anjop
Member
2 Points
1 Post
Re: Dynamic User Control - RaiseEvent Fires every second postback
Mar 30, 2009 07:34 PM|LINK
This behavior occurs because the user control changes name after the first postback. (i.e. "_ctl1" changes to "_ctl0", see client source code)
If you overrides "ClientID" and "UniqueID" forcing both to return a predefined value, you can resolve this trouble
see http://www.west-wind.com/WebLog/posts/4605.aspx