Postback or Click on Run-time created object

G

Guest

I have a database table of x elements. I need to loop through the database
table and add a checkbox for each entry to an asp:Table object. This I can
do.

Dim tRow As New TableRow
Dim tCell As New TableCell
Dim cbDT As New CheckBox
tblDTCodes.Rows.Add(tRow)
cbDT.Text = reader(0)
cbDT.Checked = False
tCell.Controls.Add(cbDT)
tRow.Cells.Add(tCell)

Question: How do I add a "Clicked" or "Checked" event to these new
checkboxes?

Question: If I can't do that, how do I interpret a PostBack from these
checkboxes?
 
G

Guest

Answered my own question. The final resolution was this:
....
Dim cbDT As New CheckBox
cbDT.Text = reader(0)
cbDT.Checked = False
AddHandler cbDT.CheckChanged, Addressof DTCode_Checked
....
with an event handler which looks like:

Public Sub DTCode_Checked(ByVal sender As Object, ByVal e As System.EventArgs)
Dim cdX As CheckBox = sender
....do something with it...
End Sub
 
R

Ray Booysen

Herb said:
Answered my own question. The final resolution was this:
...
Dim cbDT As New CheckBox
cbDT.Text = reader(0)
cbDT.Checked = False
AddHandler cbDT.CheckChanged, Addressof DTCode_Checked
...
with an event handler which looks like:

Public Sub DTCode_Checked(ByVal sender As Object, ByVal e As System.EventArgs)
Dim cdX As CheckBox = sender
...do something with it...
End Sub
Remember, the event probably won't fire unless you re-add the control
before the page is rendered.
 
G

Guest

Re-add? I do see a lag in the operation. If I uncheck something, then submit
I get no results. If I submit again, then the change takes effect. Will
readding correct this?
 
S

Steven Cheng[MSFT]

Hi Herb,

When and where did you create those dynamic subcontrols and add them into
the table control on the page? As for dynamic controls, they need to be
added into control colleciton in each request(not only the initial request,
but also postback). Also, as for the postback event handlers, we need to
register them for the dynamic controls in each requests, and the
registering should take place before the page processing postback events.
So generally we're recommended to put the control creating and event
handler registering code in the Page's Init or Load event.

Regards,

Steven Cheng
Microsoft Online Community Support


==================================================

When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.

==================================================


This posting is provided "AS IS" with no warranties, and confers no rights.



Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)
 

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,770
Messages
2,569,584
Members
45,077
Latest member
SangMoor21

Latest Threads

Top