AddHandler doesn't work in Page_PreRender()

N

news.microsoft.com

I need to move the FOR in the following code, which dynamically creates an
array of textbox, dropdown, button object, and handlers, from the
Page_Load() event to the Page_PreRender() event.

Control events change my rendering, so the Page_Load() is too soon, but I
can't get the dynamically handers created with the AddHandler method to fire
when they are created in the Page_PreRender() event.

Is there another method for creating the handler or another event before
Page_PreRender()?

Any suggestions would be appreciated?

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load

Dim i As Integer

If Not IsPostBack Then viewstate(m_sObjectID & "_Count") = 1

plhContent.Controls.Add(New LiteralControl("<TABLE border=1>"))

Dim btnButton As Button = New Button

plhContent.Controls.Add(New LiteralControl("<TR><TD>"))

btnButton.Text = "Add Additional Text"

btnButton.ID = m_sObjectID & "_I_0"

AddHandler btnButton.Click, AddressOf Insert_Click

plhContent.Controls.Add(btnButton)

plhContent.Controls.Add(New LiteralControl("</TD></TR>"))

End Sub

Sub Insert_Click(ByVal sender As Object, ByVal e As EventArgs)

viewstate(m_sObjectID & "_Count") += 1

AddSegment(viewstate(m_sObjectID & "_Count"))

End Sub

Sub AddSegment(ByVal index As Integer)

Dim litLabel As LiteralControl

Dim txtTextBox As TextBox, ddlDropDownList As DropDownList, btnButton As
Button

plhContent.Controls.Add(New LiteralControl("<TR><TD>"))

txtTextBox = New TextBox

txtTextBox.ID = m_sObjectID & "_1_" & index

plhContent.Controls.Add(txtTextBox)

plhContent.Controls.Add(New LiteralControl("</TD><TD>"))

ddlDropDownList = New DropDownList

ddlDropDownList.ID = m_sObjectID & "_2_" & index

plhContent.Controls.Add(ddlDropDownList)

plhContent.Controls.Add(New LiteralControl("</TD><TD>"))

btnButton = New Button

btnButton.Text = "Insert Line #" & index

btnButton.ID = m_sObjectID & "_I_" & index

AddHandler btnButton.Click, AddressOf Insert_Click

plhContent.Controls.Add(btnButton)

plhContent.Controls.Add(New LiteralControl("</TD></TR>"))

End Sub

Private Sub Page_PreRender(ByVal sender As Object, ByVal e As
System.EventArgs) Handles MyBase.PreRender

Dim i As Integer

For i = 1 To viewstate(m_sObjectID & "_Count") : AddSegment(i) : Next

plhContent.Controls.Add(New LiteralControl("</TABLE>"))

End Sub
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,994
Messages
2,570,223
Members
46,814
Latest member
SpicetreeDigital

Latest Threads

Top