views:

2239

answers:

3

I'm working with an ASP.net 2.0 GridView control that is bound to the results of a sql query, so it looks something like this:

<asp:GridView ID="MySitesGridView" runat="server" AutoGenerateColumns="False" DataSourceID="InventoryDB" AllowSorting="True" CellPadding="4" ForeColor="#333333" GridLines="None" OnRowCommand="GridView1_RowCommand" OnRowDataBound="siteRowDataBound">
        <Columns>
            <asp:BoundField DataField="Server" HeaderText="Server"/>
            <asp:BoundField DataField="Customer" HeaderText="Customer" SortExpression="Customer" />
            <asp:BoundField DataField="PublicIP" HeaderText="Site Address" DataFormatString="&lt;a href='http://{0}/foo'&amp;gt;Go To Site&lt;/a&gt;" />
        </Columns>
</asp:GridView>

As you can see, I'm displaying links with addresses in one of the columns (the one bound to the PublicIP field) using the format string:

&lt;a href='http://{0}/foo'&amp;gt;Go To Site&lt;/a&gt;

Here's the problem: I need to use one of the other columns from the result set as well as the PublicIP column in my links, but I don't know how to make that available to my format string. I essentially need that column bound to two columns from the result set. To clarify, I need something like:

&lt;a href='http://{0}/{1}'&amp;gt;Go To Site&lt;/a&gt;

Where {1} is the value of my other column. Is there any way to accomplish this cleanly (even if it doesn't use format strings)? I've looked into using TemplateFields as well, but can see no easy way to do it with them either.

+1  A: 

Have you tried TemplateField in combination with Eval?

<asp:TemplateField>
    <ItemTemplate>
        <a href='<%#Eval("PublicIP")/<%# Eval("Customer") %>'>Go to site</a>
    </ItemTemplate>
</asp:TemplateField>
Cristian Libardo
This basically worked, although I think you made a typo. I had to change it to:<a href="<%#Eval("PublicIP")%>/<%# Eval("Customer")%>">I'd edit the answer, but I lack the points.
sgibbons
+2  A: 

TemplateFields are the way to go.

I usually prefer to have a private string function in the Page which I pass several object variables, and calculate the resulting string.

<a href="<%# CalculateUrl(Eval("PublicIP"), Eval("Customer")) %>">site</a>

and in the code-behind:

private string CalculateUrl(object PublicIP, object Customer)
{
    if (PublicIP==null || PublicIP==DBNull.Value)
        return "";
    if (Customer==null || Customer==DBNull.Value)
        return "";
    return "http://" + PublicIP.ToString() + "/" + Customer.ToString();
}

Advantage is that the function can be shared in a common parent class, or as a static public function of a utility class.

devio
A: 

If you want an actual link, you can also use a HyperLinkField, which has a property DataNavigateUrlFields (plural) that you can use to specify multiple fields. You then set DataNavigateUrlFormatString to something containing {0}, {1} etc. This will become your link. For the link text, you use DataTextFormatString and DataTextField (singular, don't ask me why). This is probably a bit easier than a TemplateField (although less flexible).

Erik Hesselink