How to add navigateurl to hyperlink in gridview
You can use the following syntax:
<asp:HyperLinkField DataNavigateUrlFields="col1,col2" DataNavigateUrlFormatString="gotopage.aspx?p1={0}&p2={1}" Text="Print">
More information here and here on MSDN.
EDIT:
If you don't know the exact url when creating the grid, you'll have to do a bit more work.
An example I've worked on this week, using an itemtemplate. But in order to use this, you have to set EnableSortingAndPagingCallbacks
to false
. And do some extra work in code behind for this.
I've only used paging on my grid, so this sample I can provide.
Markup:
<asp:GridView ID="gridViewTicketsClosed" runat="server" CellPadding="4" ForeColor="#333333"
GridLines="None" EnableSortingAndPagingCallbacks="False" AutoGenerateColumns="False"
AllowPaging="True" OnRowDataBound="gridViewTicketsClosed_RowDataBound" OnPageIndexChanging="GridViewPageIndexChanging">
<PagerSettings Mode="NextPrevious" NextPageText="Next" PreviousPageText="Previous" />
<Columns>
<asp:BoundField DataField="TicketId" HeaderText="Nr.">
<ItemStyle Width="20px" />
</asp:BoundField>
<asp:BoundField DataField="DateStarted" DataFormatString="{0:dd/MM/yyyy HH:mm}" HeaderText="Date">
<ItemStyle Width="100px" />
</asp:BoundField>
<asp:BoundField DataField="President" HeaderText="President">
<ItemStyle Width="100px" />
</asp:BoundField>
<asp:BoundField DataField="TicketTypeId" HeaderText="TicketType"></asp:BoundField>
<asp:TemplateField>
<ItemTemplate>
<asp:HyperLink ID="lnkActionLog" runat="server" Text="Log"></asp:HyperLink>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
Code behind:
protected void gridViewTicketsClosed_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
var hyperLink = e.Row.FindControl("lnkActionLog") as HyperLink;
if (hyperLink != null)
hyperLink.NavigateUrl = CreateShowActionLogUrl(e.Row) + "?id="+ DataBinder.Eval(e.Row.DataItem, "TicketId");
}
}
Include following in your NavigateUrl
attribute
NavigateUrl='<%# Eval("Sl_no",
"frmAddIntake.aspx?id=Dashboard&intake_id={0}") %>'