paulhobson — 2011-03-16T08:05:26-04:00 — #1
I'm trying to create a dynamically named variable in .NET. The situation specifically: I have an application that creates anywhere from 1 to N number of dynamically created "sub forms" that inherit from a master form. I would like a variable that would act as an identifier for each form created. For example, if 4 sub forms get created, I'd like to create 4 dynamically named variables such as:
And so forth. This would be a variable that is set in the code behind page needs to be accessed from the markup. I know how to do this with some of the other languages I've worked with -- Coldfusion and PHP actually make this task quite simple -- but I'm too new to .NET to understand why i can't apply the same methodologies from those languages into .NET.
Any help would be appreciated.
imaginekitty — 2011-03-16T08:11:23-04:00 — #2
Are you sure you don't need a List<> or an Array or maybe a Repeater or ListView? Are you using WebForms or MVC?
I'm sure I just don't understand the purpose though. Can you show what you are trying to output?
paulhobson — 2011-03-16T08:19:06-04:00 — #3
That might be very well what I need. Maybe if I explain the situation a little more:
Each of the subforms is hidden by default. A user would toggle the form, enter information, and then submit the form for an update. Because it's just a sub form that they are submitted, I'm submitted the form asynchronously and when the page gets loaded again, I want to re-display whatever forms where open. So if forms 1,3 and 4 were closed on submission, but form 2 was open, when the page loads I want to redisplay form 2 as 1,3, and 4 will be closed by default.
Does this make any more sense?
If it helps, in ColdFusion this process would look like:
Where i is the dynamic value...
<cfset "variables.form-container-#i#" = "This value" />
<cfset variables.temp_var = Evaluate("variables.form-container-" & i) />
imaginekitty — 2011-03-16T08:22:49-04:00 — #4
I edited my post about 100 times trying to have it make more sense. Sorry about that. I know I should check before submitting but I never do. :lol:
imaginekitty — 2011-03-16T08:26:43-04:00 — #5
I imagine you could do that with a repeater with a hidden variable marked for visibility. This should be easily maintained across submit due to ViewState.
paulhobson — 2011-03-16T08:35:36-04:00 — #6
I know I could do it with the repeater, but I have too many nested elements to make this a feasible option. At this point i think the easiest route would be to dynamically name a variable. Any idea on how to do this?
pinch — 2011-03-16T15:46:24-04:00 — #7
I think you're trying to use ASP.net as if it were a scripting language. This is possible but why bother with all of that code? You should be able to do what you need without resorting to that.
If you need to repeat a form which itself has mutiple elements, the simplest solution will be a Repeater. If not a Repeater, how exactly do you plan on repeating this template?
imaginekitty — 2011-03-16T19:44:38-04:00 — #8
If you show a sample of the output it may be easier to understand what you're after.
serenarules — 2011-03-17T08:16:26-04:00 — #9
Paul, you said the sub-forms inherit from a master. Do these sub-forms add elements unique to themselves? This would make using a repeater difficult as they expect each row rendered to follow a known pattern.
Also, WebForms isn't really designed to handle more than one <form> element though it can be worked around. So are you trying to wrap each form in it's own <form> tag, or it is really one compound form, separated into sections using <div>'s?
wwb_99 — 2011-03-17T10:48:33-04:00 — #10
Did this alot, what you want is a dynamic list of forms, not a bunch of dynamically named forms.
paulhobson — 2011-03-18T07:22:45-04:00 — #11
Perhaps "sub form" wasn't the best use of words. Dynamic field-sets would be better, as the "sub forms" are really just addition fields dynamically added to the same form. The variable I'm trying to set would end up controlling the display property of the fieldset's containing elements IE:
<div id="container-0" style="display:<%= Container-0-Display %>">
That display variable is what I'm trying to create.
wwb_99 — 2011-03-18T11:36:47-04:00 — #12
Way we typically did this was to use a repeater of user controls, with the repeater "footer" being the "add new" form. Works like a charm.