SitePoint Sponsor

User Tag List

Results 1 to 12 of 12
  1. #1
    SitePoint Member PaulHobson's Avatar
    Join Date
    Jun 2010
    Posts
    14
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    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!

  2. #2
    Chopped Liver bronze trophy imaginekitty's Avatar
    Join Date
    Aug 2007
    Location
    Pennsyltucky
    Posts
    1,494
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    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?

  3. #3
    SitePoint Member PaulHobson's Avatar
    Join Date
    Jun 2010
    Posts
    14
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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) />

  4. #4
    Chopped Liver bronze trophy imaginekitty's Avatar
    Join Date
    Aug 2007
    Location
    Pennsyltucky
    Posts
    1,494
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    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.

  5. #5
    Chopped Liver bronze trophy imaginekitty's Avatar
    Join Date
    Aug 2007
    Location
    Pennsyltucky
    Posts
    1,494
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    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.

  6. #6
    SitePoint Member PaulHobson's Avatar
    Join Date
    Jun 2010
    Posts
    14
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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?

  7. #7
    SitePoint Guru pinch's Avatar
    Join Date
    Mar 2005
    Posts
    688
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by PaulHobson View Post
    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?

  8. #8
    Chopped Liver bronze trophy imaginekitty's Avatar
    Join Date
    Aug 2007
    Location
    Pennsyltucky
    Posts
    1,494
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    If you show a sample of the output it may be easier to understand what you're after.

  9. #9
    Resident OCD goofball! bronze trophy Serenarules's Avatar
    Join Date
    Dec 2002
    Posts
    1,911
    Mentioned
    26 Post(s)
    Tagged
    0 Thread(s)
    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?

  10. #10
    SitePoint Author silver trophybronze trophy
    wwb_99's Avatar
    Join Date
    May 2003
    Location
    Washington, DC
    Posts
    10,653
    Mentioned
    4 Post(s)
    Tagged
    0 Thread(s)
    Did this alot, what you want is a dynamic list of forms, not a bunch of dynamically named forms.

  11. #11
    SitePoint Member PaulHobson's Avatar
    Join Date
    Jun 2010
    Posts
    14
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(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:

    <div id="container-0" style="display%= Container-0-Display %>">

    That display variable is what I'm trying to create.

  12. #12
    SitePoint Author silver trophybronze trophy
    wwb_99's Avatar
    Join Date
    May 2003
    Location
    Washington, DC
    Posts
    10,653
    Mentioned
    4 Post(s)
    Tagged
    0 Thread(s)
    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.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •