Solved

Dynamic controls problem in VB.Net/Asp.Net

Posted on 2011-09-13
11
275 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
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Moving code from one TFS branch to another 3 25
cannot connect to sqlserver 8 25
Batch file or script with password 22 40
ASP.NET MVC -Add authentication 2 20
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…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …

786 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