Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Web Form only adding one control on the fly.

Status
Not open for further replies.

bra1niac

Programmer
Jun 13, 2001
127
US
Scenario:

I have a webform that has several user controls. Some of these user controls have yet another embedded user control. All this is on a multipage control with a tabstrip control. Up until now, everything has worked fine.

Now, on the second page of my multipage control (everything else has been on page one), I am adding another simple user control to collect yet more info. This is pretty standard stuff. It works just fine. However, this is duplicatable data, meaning I can have many rows of this type of data and in order to capture that, I have added a "Add Control" button to this page of the form.

Problem: When you click on the button, it adds one control, as it should. Click on it again, you get the postback, but no new control. No exception is thrown (break into debugger on all errors, handled or not is set) and I can't see why I'm not getting another control added to the form.

Code: The declarations in the code-behind class looks like this:
Code:
Public ctrl As New System.Web.UI.Control
    Protected WithEvents cellAddressForm As New System.Web.UI.htmlcontrols.HtmlTableCell

My click event handler:

Code:
Private Sub btnAddAddress_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnAddAddress.Click
        Me.ctrl = LoadControl("~/_Common/Contact/AddressForm.ascx")
        ctrl.ID = "frmAddressForm" & Me.cellAddressForm.Controls.Count
        Me.cellAddressForm.Controls.Add(ctrl)
    End Sub

The part that gets me is why only work ONCE? The other odd thing is that my first control is not indexed (frmAddressForm:ControlNames) where as the first one I add which is the second control on the form of this type is indexed (as per the code above: frmAddressForm3:ControlNames

I still have yet to interogate the controls collection, which I am about to do.

Anything spring to mind?

"It's easier to ask forgiveness than it is to get permission." - Rear Admiral Dr. Grace Hopper
 
the controls collection will already have a control with the name ctrl in it when you click the AddAdress Button for the second time
 
Ok, I took into account that WHERE I declared ctrl might cause global variable problems, so here's the change:

Code:
    Private Sub btnAddAddress_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnAddAddress.Click
        Dim ctrl As New AddressForm
        ctrl = LoadControl("~/_Common/Contact/AddressForm.ascx")
        ctrl = LoadControl("~/_Common/Contact/AddressForm.ascx")
        ctrl.ID = "frmAddressForm" & Me.cellAddressForm.Controls.Count
        Me.cellAddressForm.Controls.Add(ctrl)
    End Sub

So now everytime the button is clicked, a new instance of ctrl is created and the intention is to add that object to the page, but it's still not happening.

The page goes through all the motions of posting back, runs through the page load (which is empty, there is no prerender eventhandler) then the click eventhandler finally fires. The thing of it is, it just doesn't seem to see the declaritively NEW control as a new control. It seems to thing that it's adding the same one... not in addition to, but as if there wasn't one there before...

Wait just one second. Could the controls collection be getting wiped out between render and postback?

"It's easier to ask forgiveness than it is to get permission." - Rear Admiral Dr. Grace Hopper
 
Oops, ignore the dupe line. Copy & paste error. ;)

"It's easier to ask forgiveness than it is to get permission." - Rear Admiral Dr. Grace Hopper
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top