Solved

Dynamic controls problem in VB.Net/Asp.Net

Posted on 2011-09-13
11
277 Views
Last Modified: 2012-06-27
I have a page where I load a Table dynamically. Everything works fine. The table is loaded on the TextChanged event of a TextBox and it works fine going back and forth between dates. Some of the rows have a button. And on these rows I have 2 problems:
1) When I add a MaskedEditExtender to these rows, I get an "extender controls may not be declared before PreRender" error.
'Dim med As MaskedEditExtender
    'med = New MaskedEditExtender
    'med.TargetControlID = txt.ID
    'med.ID = "med_horhi" & NumRow
    'med.MaskType = MaskedEditType.Time
    'med.Mask = "99:99"
    'med.AutoComplete = True
    'med.AutoCompleteValue = "00:00"
    'med.ClearTextOnInvalid = True
    'med.ClipboardEnabled = True
    'PlanTable.Rows(NumRow).Cells(1).Controls.Add(med)

Open in new window

When I comment the lines, it works fine. This is only ever called from TextChanged.
2) The buttons work fine (basically, turn a textbox visible in a table cell). But then I can't read the changed value. Seeing as PlanTable is a global variable (in the Page scope anyway), and seeing that clicking the button causes a postback, the changed value of the textbox isn't saved. Any workaround for this?
0
Comment
Question by:Cluskitt
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 2
  • 2
11 Comments
 
LVL 5

Expert Comment

by:Rainverse
ID: 36529821
0
 
LVL 18

Author Comment

by:Cluskitt
ID: 36529835
No. I did nothing other than adding that code posted above. I didn't override any event at all. I've looked into it (most of the solutions on the net are like that) and even tried to force an override so I could then fix it, but no dice.
0
 
LVL 7

Accepted Solution

by:
Bill Nolan earned 250 total points
ID: 36532513
Are you creating ALL controls in the hierarchy in ALL cases (or is "New MaskedEditExtender" only called in some cases)?  Not sure if this is the source of your problem, but .NET page construction assumes this consistency.
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 18

Author Comment

by:Cluskitt
ID: 36534561
I am calling New TableRow on all cases, New TableCell on all cases. Some cells only have .Text, others have labels, or buttons. Some have labels, buttons and MaskedEditExtender. The Dim statement is at the top, afterwards, all instances are being called with New. Actually, all instances are the same as the one posted above. It was done with copy/paste. All that changes is the TargetControlID and the cell being added to.
0
 
LVL 18

Author Comment

by:Cluskitt
ID: 37017986
Is there no solution for this? Especially the second question?
0
 
LVL 5

Assisted Solution

by:Rainverse
Rainverse earned 250 total points
ID: 37018074
In order to retrieve values from dynamic controls you must always create the controls on each load (in the page_load or Init, or at least call the method that creates them from one of those). If the table is loaded on the text changed event of a textbox, as you described, then it doesn't exist when the page is created. You'll get odd results where sometimes if you click it twice, you get the value, etc..

Is this off base for your issue?

-MJC
0
 
LVL 18

Author Comment

by:Cluskitt
ID: 37018122
The table itself is defined as a global variable. The textboxes are inside cells in that table. On each postback, I evaluate if it's necessary to wipe it clean and load with new data (when the relevant textbox outside the table is changed with a new date) or some other operation (like making the textboxes inside the table visible).
I guess I could create the textboxes at page load, like I do with the buttons. It's kinda annoying, cause I have to keep destroying them (else I'll just keep getting duplicate id errors).
0
 
LVL 18

Author Comment

by:Cluskitt
ID: 37094516
We ended up going another route for this, basically dropping the whole dynamic thing. Too much hassle and too little control. However, your suggestion seems to hit the mark. Just like with the buttons, I would have to re-create the textboxes on each postback.
0
 
LVL 7

Expert Comment

by:Bill Nolan
ID: 37095899
Cluskitt: isn't that what I explained on 9/13?
0
 
LVL 18

Author Closing Comment

by:Cluskitt
ID: 37106440
Though not quite the same, both suggestions were good. Sorry I missed yours when I first assigned, Slimfinger.
0

Featured Post

How our DevOps Teams Maximize Uptime

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us. Read the use case whitepaper.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …

735 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question