Combine string and variable?

Results 1 to 4 of 4

Thread: Combine string and variable?

  1. #1

    Default Combine string and variable?

    I've never programmed in C# and I've also never programmed in .NET. However, we're working on a new site that utilizes both of these. After looking at some of the code, I'm starting to understand some of it better now, but I have a fairly basic question.

    One of our pages is listing buttons using this code:

    Code:
    <asp:HyperLink ID="ManufacturerLink" runat="server"  Text='<%#Eval("Name")%>' NavigateUrl='<%#Eval("ManufacturerID")%>'></asp:HyperLink>
    It works fine, except that the Navigation URL is currently being set to the ID and nothing else. I need to add the string portion of the link, but cannot figure out how to do that.

    This sort of worked, but I lost my ID:

    Code:
    <asp:HyperLink ID="ManufacturerLink" runat="server"  Text='<%#Eval("Name")%>' NavigateUrl='~/page.aspx?ID=<%#Eval("ManufacturerID")%>'></asp:HyperLink>
    The link then turns into /page.aspx?ID=<%#Eval("ManufacturerID")%> and does not execute the eval script, it just displays it as plain text. I know there's obviously something I'm not doing properly, but I just don't know what it is yet.

    Can anyone tell me how I can write the string for the link, as well as the id that's being pulled from the database, all into one string?

    Also, does anyone have any suggestions on where I should start reading up on my transition from classic asp/vbscript to now working with asp.net/C#? I literally just started two days ago, so I'm starting from square one right now.

    Thanks,
    Wil

  2. #2

    Default

    Found this which seems to work for what I need...

    Code:
    <asp:HyperLink ID="ManufacturerLink" runat="server"  Text='<%#Eval("Name")%>' NavigateUrl='<%#this.ResolveUrl("~/" + string.Format("page.aspx?ID={0}",Eval("ManufacturerId")))%>'></asp:HyperLink>
    Not sure if that is the best (or correct) way to do it though. Any feedback is appreciated.

    Thanks,
    Wil

  3. #3
    Join Date
    Feb 2009
    Posts
    452

    Default

    Code:
    <asp:HyperLink 
         ID="LinkStory" 
         runat="server" 
         NavigateUrl='<&#37;# Eval("StoryID", "StoryBook.aspx?ID={0}") %>'
         Text='<%# Eval(”Title”) %>' 
    </asp:HyperLink>
    I borrowed this from another article to illustrate the point, but Eval has a couple of overloads - this one lets you do the formatting directly.

    I don't think you would need to specify the whole url as your example showed?

    Good article on it: http://www.codenewsgroups.net/group/...topic9795.aspx
    Last edited by Ryan6491; 04-02-2009 at 05:59 PM.

  4. #4

    Default

    Very cool. I will try that instead.

    Thanks Ryan!

    Wil


Posting Permissions

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