Solved

Dynamic controls problem in VB.Net/Asp.Net

Posted on 2011-09-13
11
273 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
Comment Utility
0
 
LVL 18

Author Comment

by:Cluskitt
Comment Utility
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
Comment Utility
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
 
LVL 18

Author Comment

by:Cluskitt
Comment Utility
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
Comment Utility
Is there no solution for this? Especially the second question?
0
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 
LVL 5

Assisted Solution

by:Rainverse
Rainverse earned 250 total points
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
Cluskitt: isn't that what I explained on 9/13?
0
 
LVL 18

Author Closing Comment

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

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

772 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now