<asp:hyperlink with dynamic data

Jaak Ivask 1 Reputation point
2021-08-18T11:51:20.68+00:00

Hello,

In webapp I have <asp:GridView.. and inside it column:

 <asp:TemplateField HeaderText="PS" ItemStyle-HorizontalAlign="Center">
                    <ItemTemplate>
                        <%#(int)Eval("PaymentStatusId") == int)PaymentStatusEnum.Paid ? "P" :   
                         (int)Eval("PaymentStatusId") == (int)PaymentStatusEnum.PendingOK ? "PINV" : "H"%>    

                         //code I want to add

     </ItemTemplate>

</asp:TemplateField>

I want to dynamically add URl to send remider and add it instead of the comment above:

<a href="https://www.company.com/et/orders/<%#((Order)Container.DataItem).Parent.OrderGuid%>/reminder" target="_new">Saada meeldetuletus</a>

the URL formed correctly. Problem is that it is displayed in every row of the grid.

Insted of the simple HTML url I can use <asp:Hyperlinc to conditionally display the URL, but I can't get it to work with dynamic data:

<asp:HyperLink id="hyperlink1"  
                  NavigateUrl="http://www.company.com/et/orders/" + <%#((Order)Container.DataItem).Parent.OrderGuid%> + "/reminder"
                  Text="Send reminder"
                  Target="_new"
       Visible="<%#(int)Eval("PaymentStatusId") == (int)PaymentStatusEnum.PendingOK ? "True" : "False"%>"
                  runat="server"
  />     

The error is: "The server tag is not well formed." The url has to redirect another domain. If I add <%#((Order)Container.DataItem).Parent.OrderGuid%> alone to NavigateUrl it's working.

ASP.NET Core
ASP.NET Core
A set of technologies in the .NET Framework for building web applications and XML web services.
4,209 questions
ASP.NET
ASP.NET
A set of technologies in the .NET Framework for building web applications and XML web services.
3,289 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Yijing Sun-MSFT 7,071 Reputation points
    2021-08-19T08:28:47.2+00:00

    Hi @Jaak Ivask ,

    The error is: "The server tag is not well formed."

    The problem is IntelliSense, "<" will sense the nearest ">", so it will cause confusion.

    Maybe you can try the following code:

    NavigateUrl='<%#String.Format("https://www.company.com/et/orders/{0}/reminder",((Order)Container.DataItem).Parent.OrderGuid)  %>'  
    

    You could not use double quotes within double quotes so use combination of single and double quotes.

    Visible='<%#(int)Eval("PaymentStatusId") == (int)PaymentStatusEnum.PendingOK ? true : false%>'  
    

    Your full codes need to be like this:

     <asp:TemplateField>  
                            <ItemTemplate>  
                                <asp:HyperLink ID="hyperlink1" runat="server" Text="Send reminder" Target="_new" NavigateUrl='<%#String.Format("https://www.company.com/et/orders/{0}/reminder",((Order)Container.DataItem).Parent.OrderGuid)  %>'  Visible='<%#(int)Eval("PaymentStatusId") == (int)PaymentStatusEnum.PendingOK ? true : false%>' ></asp:HyperLink>  
                            </ItemTemplate>  
                        </asp:TemplateField>  
    

    Best regards,
    Yijing Sun


    If the answer is helpful, please click "Accept Answer" and upvote it.

    Note: Please follow the steps in our  documentation  to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments