Dynamically Name .NET Variables

Hey All,

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:

Form-Container-0
Form-Container-1
Form-Container-2
Form-Container-3

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.

Thanks!

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. :wink: Can you show what you are trying to output?

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” />

or

<cfset variables.temp_var = Evaluate(“variables.form-container-” & i) />

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:

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?

If you show a sample of the output it may be easier to understand what you’re after. :slight_smile:

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?

Did this alot, what you want is a dynamic list of forms, not a bunch of dynamically named forms.

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.

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.