C# - Setting url of a Hyperlink control in a Gridview TemplateField column
-
09-01-2021 - |
Question
I have a gridview that has a column that has a Hyperlink control as wellas a LinkButton control. As such:
<asp:TemplateField HeaderText="Actions">
<ItemTemplate>
<asp:HyperLink ID="linkEdit" runat="server" NavigateUrl="~/shipment.aspx">Edit</asp:HyperLink> |
<asp:LinkButton ID="linkSend" runat="server">Send</asp:LinkButton>
</ItemTemplate>
<ItemStyle Width="76" HorizontalAlign="Center" />
</asp:TemplateField>
Each row will have an "Actions" column that looks like this: Edit | Send
How do I set the NavigateUrl property of the Hyperlink in each row to include a value from the first column of the row?
Example: NavigateUrl="~/shipment.aspx?edit=VALUE_FROM_COLUMN_0_OF CURRENT_ROW"
Solution
Try
<asp:HyperLink ID="linkEdit" runat="server"
NavigateUrl='<%# "~/shipment.aspx?edit=" + Eval("IdField")" %>' >Edit</asp:HyperLink>
OTHER TIPS
Give this a shot:
asp:HyperLink ID="lnkEdit" runat="server" NavigateUrl='<%# String.Format("~/shipment.aspx?edit={0}", Eval("SomeID")) %>' ...>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow