Solved

Dynamic controls problem in VB.Net/Asp.Net

Posted on 2011-09-13
11
276 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
  • 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:
Slimfinger 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
Space-Age Communications Transitions to DevOps

ViaSat, a global provider of satellite and wireless communications, securely connects businesses, governments, and organizations to the Internet. Learn how ViaSat’s Network Solutions Engineer, drove the transition from a traditional network support to a DevOps-centric model.

 
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:Slimfinger
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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

860 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