Access to variables in an asp.net user control vs an include file

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • jumbojs
    New Member
    • May 2009
    • 20

    Access to variables in an asp.net user control vs an include file

    I've asked this question before but couldn't get the answer I was looking for so I'm going to try it again.

    I'm translating pages from old asp to asp.net and I don't want to do this any other way so I really just want to know if this can be done.

    In asp, I'd assign a variable on one page

    Code:
    <% myVar = "something" %>
    I could assign many variables here and then use an include

    Code:
    <!--#include file="Test2.aspx"-->
    then in test2 file, I could access all the variables without having to pass all the variables into the control or declaring them again, like

    Code:
    <% myVar = "something else" %>
    I want to do this the dot net way but I have some thirty variables on the page and i don't want to pass a bunch into the user control and I don't want to have to keep declaring the same variables.

    All I really want to know is if there is some way to replicate the behavior above in asp.net?
  • Frinavale
    Recognized Expert Expert
    • Oct 2006
    • 9749

    #2
    I know it's hard moving from one environment to another but when you do you have to keep in mind that each environment has its own way of doing things.

    It sounds like someone has already told you what I was going to suggest: to use a User Control.

    There are other ways to do this but before I suggest any of them I need to know a little more about these variables.

    Are they global variables that are accessible on many of your pages (shared amongst them)?

    How are you using these variables?
    What are they for?

    -Frinny

    Comment

    • jumbojs
      New Member
      • May 2009
      • 20

      #3
      Well, in the original program it was an import process that had a web interface. The variables were "global" but they weren't really defined as global but the scope was global only because that when you use server side includes the file that is injected into the page has access to all the variables from the parent page. I did find something on Page Inheritence and it sounded like it used to work on ASP.NET 1.1 where if you had a control on the parent page you could access the parent control variables but now in 2.0 it sounds like you have to do a bunch of stuff that makes this almost silly. Like create a base class and put it in APP_CODE and then inherit from that class and then your control can have access to the parent control variables... all of which is much more difficult then what I used to do with includes. I'm not really seeing the advantage of using user controls here.

      Comment

      • Frinavale
        Recognized Expert Expert
        • Oct 2006
        • 9749

        #4
        User Controls aren't really what you're looking for but they are similar to what you want: they are "semi-pages" included into other pages...so they could probably do what you want them to do except that their main purpose isn't really to do this.

        I'm only just starting to get used to the style that you're so comfortable with because I'm starting to do a little more PhP programming. PhP does the same thing: uses includes to "import" variables between pages. It's weird for me to work this way and I get annoyed when I try to figure out where these variables are coming from when pages include several different imports...

        I can see how ASP.NET would be strange for you because your programming style is very different from the programing style that ASP.NET uses.

        I think if I were in your place I'd probably make a Class that stores these variables for you in one place. I typically use a class ("Utils") that contains "Lists" and other variables (along with methods) that are shared amongst several pages.

        Have you considered using a Static/Shared Class like my Utils class?

        Have you considered using the web.config file to store the variables that are shared amongst several pages?

        Creating a base class for your pages sounds like it will also work but I usually think it's overkill. I've seen other posts where people are asking how to use inheritance and base classes for their pages...I never could figure out why they were doing this. Thanks for opening my eyes to their way of thinking.

        -Frinny

        Comment

        • jumbojs
          New Member
          • May 2009
          • 20

          #5
          So, it sounds like the static class might work. So you have a bunch of static variables and methods that are shared among all the pages? The only problem is that these classes need to talk back to the aspx page so there can be some user feedback on the progress of the import but I don't seeing that being much of a problem.

          Comment

          • Frinavale
            Recognized Expert Expert
            • Oct 2006
            • 9749

            #6
            I'm not sure what you mean by "provide feedback about the status of the import".
            Let me know how the static class works out for you :)

            -Frinny

            Comment

            • jumbojs
              New Member
              • May 2009
              • 20

              #7
              Status would mean, as the import process is running, the user would see information on the web page like number of records processed, the time elapsed...etc.

              I will let you know about the static variables, thank you.

              Comment

              • Frinavale
                Recognized Expert Expert
                • Oct 2006
                • 9749

                #8
                Oh, I was just helping someone else with this.
                You may be interested in this thread:

                ASP.NET Progress

                -Frinny

                Comment

                Working...