Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    New Coder
    Join Date
    Jan 2012
    Posts
    17
    Thanks
    8
    Thanked 0 Times in 0 Posts

    Cool How do i DISPLAY a variable on an ASP page that was passed from a URL

    Hi, I have situation here that works in Coldfusion bout does NOT work here in ASP.NET... Please HELP... How do i DISPLAY a variable on an ASP page that was passed from a URL, i.e.

    1. URL and variable:
    a) [<a href="search/ccSearch.aspx?countryName=mycountry&countryID=2">selected country</a>]

    b) [OR, <a ID="3" href="search/ccSearch.aspx">country link</a>]

    2. What would be the OUTPUT code on the asp.net page for the above?

    x) say, for (a) above i wanted to display the country name on the on the destination page... <%# url.countryName %> [what would be the answer because that does not work... it would work for CFM but not here].

    y) also, (a) or (b) above... if i wanted to use the IDs to pull data from a database how would i do that? For example: [SELECT * FROM myDatabase WHERE myDatabaseID=<%# URL.ID %>] would that work? OR "WHERE myDatabaseID=<%# URL.countryID %>"

    thats it.... thanks for the help!!!

  • #2
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    26,588
    Thanks
    80
    Thanked 4,497 Times in 4,461 Posts
    Major question: Is this code supposed to appear *ON* the page named ccSearch.aspx? Or will it appear on some other (possibly HTML, not even ASP.NET) page?

    If the former, then you need to stop what you are doing and go back and learn how to correctly use ASP.NET controls.

    But if the latter...

    First of all, THIS code would never work to send the ID in *ANY* system. ASP/JSP/PHP/ASP.NET or even CF:
    Code:
    <a ID="3" href="search/ccSearch.aspx">country link</a>
    When you use an href to invoke *ANY* server-side page, the ONLY way to pass data is as part of the query string. So you *MUST* do something like
    Code:
    <a href="search/ccSearch.aspx?ID=3">country link</a>
    (And in any case an HTML id="..." starting with a digit is not supported in many ways.)

    For
    Code:
    <a href="search/ccSearch.aspx?countryName=mycountry&countryID=2">
    you can use Request.QueryString("countryName") to retrieve the value in the query string.

    So you could just use
    Code:
    <% Response.Write( Request.QueryString("countryName") ) %>
    (needs a terminating semicolon of you are writing in C# instead of VB.NET).

    But there are many many better ways to do this with ASP.NET. And the only way you are going to learn them is to start with a *GOOD* ASP.NET tutorial and work through all the examples.

    Same, especially, applies to using a database with ASP.NET. There are so many ways to do so that I can't possibly go into all of them here.

    The simplest would be
    Code:
    ' VB.Net
    Dim SQL As String = "SELECT * FROM myDatabase WHERE myDatabaseID=" & Request.QueryString("id")
    
    ' C#
    String SQL = "SELECT * FROM myDatabase WHERE myDatabaseID=" + Request.QueryString("id");
    But odds are that's not what you are going to ultimately want to do. Again, go find a tutorial and start working through *ALL* the examples.

    ASP.NET is enormously flexible and yet, at the same time, there are some rigid rules that will help you use it in the best ways. ASP.NET has a steep learning curve, compared to CF or PHP, but the end result is worth it. But you must spend the time to REALLY learn it if you expect to get good results from it.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • Users who have thanked Old Pedant for this post:

    robuc (02-08-2012)

  • #3
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    26,588
    Thanks
    80
    Thanked 4,497 Times in 4,461 Posts
    By the by....your question is posed wrong: You are NOT asking about "an ASP page". You are asking about "an ASP.NET page".

    ASP and ASP.NET are vastly different, despite the similarity in names. Kind of like Java and JavaScript.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • Users who have thanked Old Pedant for this post:

    robuc (02-08-2012)

  • #4
    New Coder
    Join Date
    Jan 2012
    Posts
    17
    Thanks
    8
    Thanked 0 Times in 0 Posts

    Cool

    Old Pedant, i don't know if i should thank you or feel offended... anyway i am going to thank you, you did answer my questions .

    I have never used Code:
    <a ID="3" href="search/ccSearch.aspx">country link</a>
    I just wanted to cover all the bases.

    But your other explanations helped... Thanks again!!! It actually worked, including the database query.

  • #5
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    26,588
    Thanks
    80
    Thanked 4,497 Times in 4,461 Posts
    If my answer helped, I'm glad. I was more than a little worried that you were heading off in the wrong direction, as you can certainly tell. If it makes you feel any better, those of us who transitioned from ASP to ASP.NET went through the same learning curve. In some ways, the shock to our systems was bigger than yours has been, because there are a lot of things you *CAN* do in ASP.NET the same way we did them in ASP. Just that those are almost inevitably not the best ways from an ASP.NET-ish perspective.

    I had to relearn almost everything I thought I knew, just as you are going through now. But, in the long run, if you take the time to learn the principles of ASP.NET and explore its "corners", you'll be able to produce much more efficient code.

    So...no offense ever intended. Just concern that you understand that sometimes the quick and dirty way isn't the best way.

    (The real killer for me was finding out that database connections are not closed automatically for you! ASP does that, so if--for example--you have an error in an ASP page you can safely dump out a message and then just stop With ASP.NET, you *must* catch the error, display the message, and then be absolutely sure you have closed all open connections. Else you will indeed run out of connections sooner than you might think!)
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • Users who have thanked Old Pedant for this post:

    robuc (02-21-2012)


  •  

    Posting Permissions

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