How can I include two dynamic elements in the NavigateURL correctly?

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

I am trying to figure out how to properly include two dynamic elements in a NavigateURL with both a control Lo-Fi:HyperLink

and a control Hi-Fi:HyperLink

. I know how to do this with the HyperLinkField as in the second but I cannot use that inside the template column.

I need to bind the element <%# Eval("Locker_LO_Filename")%>

I am using for the text property at the end of the url where {1} is currently located.

<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>

      

0


a source to share


2 answers


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

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

      

This will call some code on the code 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 least point in the right direction.

+1


a source


Take a look at the event GridView

RowDataBound

. 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;
    }
}

      

+1


a source







All Articles