views:

996

answers:

2

I have a grid view using bound, hyper link, and template fields.

I'm trying to figure out how to include two dynamic items in the NavigateURL properly of both the Lo-Fi:HyperLink control and the Hi-Fi:HyperLink control I know how to do this with a HyperLinkField field as I do in the second two columns but I can't use that inside the template column.

I need to concatenate the <%# Eval("Locker_LO_Filename")%> item I'm using for the text property onto the end of the URL where the {1} is currently.

<asp:GridView ID="gvLocker" runat="server" HeaderStyle-BackColor="Goldenrod" 
        HeaderStyle-ForeColor="DarkBlue" AlternatingRowStyle-BackColor="Cornsilk" 
            Font-Names="Verdana,arial,helvetica" AutoGenerateColumns="False" Font-   Size="13px" Width="640px" BorderColor="#404040">
   <Columns> 
                <asp:BoundField DataField="memid" HeaderText="MemID" ReadOnly="True" SortExpression="memid"
                    Visible="False" HeaderStyle-Font-Size="13px" />       
    <asp:HyperLinkField  DataNavigateUrlFormatString="myLockerEditSong.aspx?ID={0}&amp;li={1}"
     DataTextField="EditIt" DataNavigateUrlFields="memid,lockid" HeaderStyle-Font-Size="13px">
     <ItemStyle Width="65px" Font-Size="13px" />
     </asp:HyperLinkField>
    <asp:HyperLinkField  DataNavigateUrlFormatString="myLockerDeleteSong.aspx?ID={0}&amp;li={1}"
     DataTextField="Delete" DataNavigateUrlFields="memid,lockid" HeaderStyle-Font-Size="13px">
                    <ItemStyle Width="65px" Font-Size="13px" />
                </asp:HyperLinkField>
                <asp:TemplateField HeaderText="Song Information" HeaderStyle-Font-Size="13px">
              <ItemTemplate>
                        <strong><asp:Label ID="Label1" runat="server" Text='<%# Eval("Locker_Title") %>'></asp:Label></strong><br />
                        Lo-Fi:<asp:HyperLink ID="HyperLink1" runat="server" NavigateUrl='<%# Eval("memid", "/uploads/{0}/Locker/LoFi/{1}") %>'
                            Text='<%# Eval("Locker_LO_Filename") %>' Font-Size="13px"></asp:HyperLink><br />
                        Hi-Fi:<asp:HyperLink ID="HyperLink2" runat="server" NavigateUrl='<%# Eval("memid", "/uploads/{0}/Locker/HiFi/{1}") %>'
                            Text='<%# Eval("Locker_HI_Filename") %>' Font-Size="13px"></asp:HyperLink>                                
                    </ItemTemplate>
                    <ItemStyle Width="350px" Font-Size="13px" />
                </asp:TemplateField>
                <asp:TemplateField HeaderText="File Information" SortExpression="Locker_UploadDate" HeaderStyle-Font-Size="13px">
                    <ItemTemplate>
                       <strong>Uploaded:</strong> <asp:Label ID="Label2" runat="server" Text='<%# Eval("Locker_UploadDate") %>'></asp:Label><br />
                       <strong>Modified:</strong> <asp:Label ID="Label3" runat="server" Text='<%# Eval("Locker_DateLastModified") %>'></asp:Label>
                       <ItemStyle Font-Size="13px" />
                    </ItemTemplate>                        
                </asp:TemplateField>                    
   </Columns>        
        <HeaderStyle BackColor="#FFDF59" ForeColor="Maroon" HorizontalAlign="Left" />
        <AlternatingRowStyle BackColor="PaleGoldenrod" />
    </asp:GridView>
+1  A: 

Take a look at the GridView RowDataBound event. You need something like this (untested):

void gvLocker_RowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        // I'm not sure what kind of data object you use as your datasource,
        // but dataItem should have some type.
        MyObject dataItem = e.Row.DataItem;

        // Find the right control inside the row and set its Text property.
        HyperLink link = (HyperLink) e.Row.FindControl("HyperLink1");
        link.Text = dataItem.Locker_LO_Filename;
    }
}
Ronald Wildenberg
This will work as well. But I think the goal was to assign a composite string, not just a data item. :)
Craig
rwwilden - thanks for you input but Craigs suggestion was more inline with what I needed. Thanks for you time.
Both will work. Never really considered using page methods like this.
Ronald Wildenberg
+1  A: 

Instead of using <%# Eval(..) %>, use the following..

<%# FormatHiFi(DataBinder.Eval(Container.DataItem, "Locker_Title"), DataBinder.Eval(Container.DataItem, "OtherStringName"))%>

This will call some code in the code-behind page (see below) that will format the string as needed.

protected string FormatHiFi(object str1, object str2) 
{
   return string.Format("/uploads/{0}/Locker/HiFi/{1}", str1.ToString(), str2.ToString())
}

The same can be used for Lo-Fi as well. I hope this helps and if not, at leasts points in the right direction.

Craig
Craig -except for having to rewrite for VB.net and add the correct field names in the function call your suggestion worked perfectly. In all of the stress I forgot that you can call functions from a templateitem. Thanks so much for the help.