tags:

views:

133

answers:

2
A: 

I've created a project based on your code and I've managed to recreate the issue.
When I initially run it and click on the Add control A button the Main control will load Control A and note this in the viewstate. Control A's button to add Control X will now appear with an id of Main1_ctl00_Button1. When clicking on this button Main's page load will add Control A back to it's placeholder and Control A's button click event will fire. If you now click Main's Control A button again the problem arises. Main's page load method will look at the Viewstate and add control A to the placeholder. Then Main's button click event will then fire clearing the placeholder and re-adding control A. When the form is displayed Control A's button to add Control X will now have an id of Main1_ctl01_Button1 as it was the second control to be generated in the code behind. Now when you click on the button to add control x Main's page load will add Control A again but as it's id will be Main1_ctl00_Button1 and the click event came from a button with an id of Main1_ctl01_Button1 the event will not fire.

To fix this you will have to avoid the duplication of control creation the second time around. You could do this by checking the requests form collection during the page load to see which button was pressed although this is not a very elegant solution. You will need to ensure that the buttons on the Main control have a unique text value for this to work. Hopefully someone else may be able to come up with a more elegant solution:

Dim mainButtonClick = False
For index As Integer = 0 To Request.Form.Count
    If Request.Form(index) = "Button A" Or Request.Form(index) = "Button B" Then
        mainButtonClick = True
    End If
Next

If Not IsPostBack And Not mainButtonClick And ViewState("name") <> String.Empty Then
    AddControl(ViewState("name").ToString())
End If

Please excuse any errors in this code as I am not a VB.NET programmer.

Andy Rose
Andy I added code. If you could create a samll project ot make make this mechism work. This will fix my problem. Thanks in advance
Nick
This Answer will reload the control, but the problem still remains there. Could someone please take a look at my sample code of the problem and try to fix it. I cannot figure this one out :0 Thanks in advance
Nick
If I do not create the control it will disappear all together during postback. How do I fix it? Can you give me some code? Ive been going 12 hours straight on this thing.
Nick
It behaves like the following....1. Control is loaded into main control2. A button is clicked inside the Control3. A page wide post occurs4. Viewstate("name") holds the data to repopulate the the control and does successfully put the control back into main again5. Now if I click the button in the control it the click event fires and works
Nick
I'm pretty sure the issue your having is with the id's asp.net is dynamically creating for your controls. I would suggest viewing the source of each page between postbacks to check the user contol ids specifically the ctlxx part.
Andy Rose
Is it possible for you to write code to fix the the problem in the example. I agree it must have to do with the id's asp.net is creating but I am uncertain how to programmatically fix this problem.
Nick
Andy you still out there??
Nick
A: 

Andy I tried your code but now the button click events in ControlA nor in ControlB fire at all. The controls must be reloaded on every postback otherwise the controls will dissappear so you must reload the control onto the the main control (which is on the default page) everytime any button is click on any control. The trick to this is letting viewstate know that AFTER the postback occurs and the control is reloaded is to automatically fire the event that raised the postback. This I have no idea how I can do?

Nick LaMarca

related questions