1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

Pulling data from a Query String

Discussion in 'C#' started by WayTooAwesome, Jun 20, 2006.

  1. #1
    (This is using ASP.NET 2.0 with C#)

    I'm currently trying to figure out how to pull the information from a query string down into an asp:hyperlink to alter which page the link goes to.

    Basically, I have a link like this: url.com/blah.aspx?t=1&r=0
    SEMrush
    "t" signifies my UID, but "r" is what I want to affect my URL. Based on R's value, I want to change this Hyperlink:

    <asp:HyperLink ID="HyperLink3" runat="server" Text="Back" NavigateUrl='<%# "sectionman.aspx?t=" + Eval("ManagerID") %>'></asp:HyperLink><br />
    Code (markup):
    So that the "sectionman.aspx?t=" portion may change to something such as "labman.aspx" or "employeegrid.aspx?t=". I'm fairly certain the code is something to the equivalent of Request.QueryString["r"], I just don't know how to implement that into the hyperlink, whether it's by an if statement or not.
     
    WayTooAwesome, Jun 20, 2006 IP
    SEMrush
  2. fhirzall

    fhirzall Guest

    Messages:
    124
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #2
    I'm not sure how you would do that, but can I ask why you would need that to change? It seems like you have different aspx pages that you want to link to, so why not just link it normally without having to pull anything from query strings?
    Lets say you have a list of people on sectionman.aspx, and a detail page on labman.aspx. if you want to pass the value to the other page, you just do what your code says, and on the page_load method of the other page, you request the query string and pull whatever you need from the database. I apologize if i'm way off on what your trying to do, try explaining some more!
     
    fhirzall, Jun 20, 2006 IP
  3. WayTooAwesome

    WayTooAwesome Peon

    Messages:
    36
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Sorry 'bout that... I'm trying to describe the bare minimum because I have a habit of over-explaining simple things. ^_^;

    The reason I can't directly link to the other pages is because each of them are dynamic - At any time, I want a user to able to pull up a page of employees under a specific manager, and I want the page to update itself when a new entry is added into the database.

    Currently, the hierarchy of employee listing pages goes like this:

    labman.aspx
    sectionman.aspx
    projectman.aspx
    employeegrid.aspx
    employee.aspx

    There's only one Lab Manager, so that's just a static page, and the employee.aspx page is that page that displays all of an employee's data, like their job, responsabilities, picture, etc.

    I've been kind of thinking about meshing Labman, Sectionman, Projectman, and Employeegrid all into a single page. That seems like it would work much better, I just don't know how I'd go about doing it...

    Ya see, All of the managers have people under them, so I've put a hyperlink on the managers pages to go onto the next page down with a query string to show who works for them - as opposed to the normal employeegrid tables, who have no one working for them.

    Think it'd be easier to make them all one page and throw in a hyperlink with a switch statement or something? If so, how would I go about doing that?

    Thanks again!
     
    WayTooAwesome, Jun 21, 2006 IP
  4. fhirzall

    fhirzall Guest

    Messages:
    124
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Ok, I think I understand what you're trying to do but maybe making it a little more complicated that it is :).
    Lets say you have a few section managers listed on sectionman.aspx, and you want to show the project managers for the section manager selected, what I would do is just put a hyperlink with the section manager's name, and pass his id to the other page, so your hyperlink will be like this...

    <asp:HyperLink id="namelink" runat="server" NavigateUrl='<%"projectman.aspx?smanid=" + Eval("SectionManID") %>'>Manager Name or use Eval to pull from database</asp:HyperLink>
    SectionManID is the field name in your database for the section manager's ID, and make sure you put all that into an Item Template for any control you like so that it can pull them from the database, let me know if you need help with that.
    On projectman.aspx, you just pull the information from the database with a parameter in your sql query, just call it @secID or anything like that, and set its value to the query string. Let me know if you need help with that too.
    You would basically do the same thing for projectman.aspx and employeegrid.aspx, but this time you pass the project manager's ID to the employeegrid.aspx page, then grab the employees, so the sql would be SELECT field1,field2, field3 from employees where projectmanID = @projectmanID, and then add the query string value to @projectmanID.
    Hope that helps!
     
    fhirzall, Jun 21, 2006 IP
  5. WayTooAwesome

    WayTooAwesome Peon

    Messages:
    36
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    I might be wrong, but I'm pretty sure that's what I'm already doing. My labman page starts it off, and its forward link looks like this:

    <asp:HyperLink ID="HyperLink1" runat="server" Text="View Employees" NavigateUrl='<%# "sectionman.aspx?t=" + Eval("ID") %>'></asp:HyperLink>
    Code (markup):
    Which then goes to my sectionman page, in which the links are also the same, then progresses similarly to my projectman page, as well as to my employeegrid page.

    My QueryString and DataSource looks the same on all of the pages, except for labman, since it doesn't need a QueryString:

            <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:DatabaseConnectionString %>" SelectCommand="SELECT * FROM [Employee] WHERE managerID=@managerID">
                <SelectParameters>
                    <asp:QueryStringParameter Name="managerID" QueryStringField="t" Type="String" />
                </SelectParameters>
            </asp:SqlDataSource>
    Code (markup):
    The problem with it is that the pages have different information/hyperlinks that are generated based off of the QueryString and database, but my employee.aspx always links back to the employeegrid.aspx - which doesn't work out well, because the managers have to link back to labman, sectionman, or projectman, otherwise the page gets jumpled and confused in itself, and the whole thing falls apart. >_O

    That's why I'm lookin' for a way to put an If statement or something into effect for the hyperlink within the employee.aspx page, so that I can direct it based upon my second query to one of the seperate pages.
     
    WayTooAwesome, Jun 22, 2006 IP
  6. WayTooAwesome

    WayTooAwesome Peon

    Messages:
    36
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #6
    EUREKA!

    I ended up playing with some If statements and got it to work, here's my relevant code on the front-end:

    <asp:HyperLink ID="BackLink" runat="server" Text="Linkie" NavigateUrl='<%# Linkie() %>'></asp:HyperLink>
    Code (markup):
    And on the back-end:

        protected String Linkie()
        {
            string r = Request.QueryString["r"];
    
            if (r == null)
            {
                r = "0";
            }
    
            if (r == "0")
            {
                return ("labman.aspx");
            }
            else
            {
                return ("projectman.aspx");
            }
        }
    Code (markup):
    Thank you very much for your help!
     
    WayTooAwesome, Jun 22, 2006 IP
  7. walnet

    walnet Peon

    Messages:
    1
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #7
    Guys how do you do this in VB?;)
     
    walnet, Jul 13, 2006 IP
  8. WayTooAwesome

    WayTooAwesome Peon

    Messages:
    36
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #8
    Sure.

    I use this tool a lot, it's a very well made VB --> C# convertor, and also does it the other way around.

    http://www.developerfusion.co.uk/utilities/convertcsharptovb.aspx

    Using tha tool, the same backend code in VB would be:

    Protected Function Linkie() As String
     Dim r As String = Request.QueryString("r")
     If r Is Nothing Then
       r = "0"
     End If
     If r = "0" Then
       Return ("labman.aspx")
     Else
       Return ("projectman.aspx")
     End If
    End Function
    Code (markup):
    All the rest of the code would of course stay the same, as it's in asp.net.
     
    WayTooAwesome, Jul 14, 2006 IP