Passing information between pages

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Ashlewis
    New Member
    • Mar 2008
    • 20

    Passing information between pages

    I know this is fairly trivial and i know there are several ways of doing it but i was wondering whats the most appropriate way to do this:

    I have a set of links of different sports Football Basketball and Tennis, they all link to a Sports.aspx page.

    On the Sports.aspx page I need whatever link was clicked to be the title of the page.

    The title will be a label.
    The name of the hyperlink is obviously stored under link.Text

    Now, i know this can be done with query strings yet couldnt a user just change the url and then it would break?

    whats the best way to do this?

    Also am i right in saying if i made it some sort of session there would be wierd behaviour if the Sports.aspx link was sent to someone else, and therefore a querystring would be the best idea since it would link to say football correctly?

    ps i have no idea how to do a querystring
  • balabaster
    Recognized Expert Contributor
    • Mar 2007
    • 798

    #2
    You are right in thinking that if a user changed the querystring it would "break". So long as you account for the possibility that the information from the querystring may not be correct, then you can effectively just spit out an error to the user telling them they clicked a badly formed like or tell them that they shouldn't **** around with the querystring or this is what happens.

    You can get as complex as you like or as simple as you like...at the complex end we've got a database that stores all the content for the page as well as the title and holds an id for each sport...this may be what you're after ultimately or you could go as simple as defining your link as:
    <a href="sports.as px?sport=tennis ">Tennis</a>
    <a href="sports.as px?sport=footba ll">Football</a>
    etc
    In your sports page in the codebehind to read the querystring using the request.queryst ring collection.
    Label1.Text = Request.QuerySt ring["sport"]

    In the address you can separate parameters using &.

    MyPage.aspx?par am1=stuff&param 2=morestuff&par am3=otherstuff

    Label1.Text = Request.QuerySt ring["param1"];
    Label2.Text = Request.QuerySt ring["param2"];
    Label3.Text = Request.QuerySt ring["param3"];

    Hopefully that gives you a pointer in the right direction. You can't really use session variables if you want people to be able to link directly to your sports page.

    Comment

    • Ashlewis
      New Member
      • Mar 2008
      • 20

      #3
      Thats fantastic. Thank you exactly what I needed.
      Infact the links are dynamic, they are created from a table i have in a database called TblSports. but i guess thats no use when i need to be able to tell which one has been clicked, ill go for the query string. Thanks for the examples too.

      Ash

      Comment

      • Mohammed Seidu
        New Member
        • Sep 2007
        • 19

        #4
        You can try using session variable for passing values between pages.
        For example when the link is clicked you can say
        SESSION['myvar']=link.text
        and then on the page where you want to access the clicked link say
        variable=SESSIO N['myvar']

        Comment

        • balabaster
          Recognized Expert Contributor
          • Mar 2007
          • 798

          #5
          Originally posted by Ashlewis
          Thats fantastic. Thank you exactly what I needed.
          Infact the links are dynamic, they are created from a table i have in a database called TblSports. but i guess thats no use when i need to be able to tell which one has been clicked, ill go for the query string. Thanks for the examples too.

          Ash
          Before you get to rewriting all your code, you can do something similar using a combination of database and querystrings.

          Load your links dynamically using your data reader:

          While(MyReader. Read()){
          Hyperlink MyLink = New HyperLink
          MyLink.ID = "Link" + i
          MyLink.Navigate URL = "sports.aspx?id ="+ MyReader["id"]
          this.Controls.A dd MyLink
          }

          In sports.aspx:
          string id = Request.QuerySt ring["id"]

          Grab the content from your database having the correct id.

          Hey presto - you've got all the data in your database for easy centralized management and every time you add data into your database, your page automatically loads it without you having to keep rewriting your pages every time you want to add content.

          Comment

          Working...