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.
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:
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.
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?
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?
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?
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: