You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何格式化HyperLinkField?已尝试多种方法仍未解决

Hey there, let's work through this HyperLinkField navigation issue you're facing! I see you've tried tweaking DataNavigateUrlFormatString and switching to NavigateUrl without luck—let's break down some fixes and also cover the return link for your target page.

1. Double-check your binding and path basics

First, let's rule out the simplest issues:

  • Confirm the PONumber field exists in your data source and has no typos (ASP.NET is case-sensitive here!)
  • Test the relative path ../PurchaseOrder/PurchaseOrderInsert.aspx directly in your browser to make sure it leads to the correct page (relative paths depend on your current page's location in the folder structure)
  • Temporarily set DataTextField="PONumber" to see if the correct order number displays on the page—this will confirm your field binding is working as expected

2. Explicitly configure the HyperLinkField

Sometimes adding a small explicit setting can fix hidden binding quirks. Try updating your control like this:

<asp:HyperLinkField 
    DataNavigateUrlFields="PONumber" 
    DataNavigateUrlFormatString="../PurchaseOrder/PurchaseOrderInsert.aspx?PurchaseOrderID={0}" 
    DataTextField="Description" 
    HeaderText="Description" 
    SortExpression="Description"
    Target="_self" <!-- Ensures navigation happens in the current window (default, but explicit avoids surprises) -->
/>

3. Switch to a TemplateField for full control

If the HyperLinkField still won't cooperate, using a TemplateField to build the link manually is more reliable:

<asp:TemplateField HeaderText="Description" SortExpression="Description">
    <ItemTemplate>
        <a href='../PurchaseOrder/PurchaseOrderInsert.aspx?PurchaseOrderID=<%# Eval("PONumber") %>'>
            <%# Eval("Description") %>
        </a>
    </ItemTemplate>
</asp:TemplateField>

This bypasses any automatic binding logic that might be causing issues and gives you direct control over the link.

For the PurchaseOrderInsert.aspx return functionality, you can use Request.UrlReferrer to get the previous page's address. Here are two options:

HTML Link Version

<a href='<%= Request.UrlReferrer?.ToString() ?? "../YourDefaultPage.aspx" %>'>Back to List</a>

The ?? "../YourDefaultPage.aspx" acts as a fallback if there's no referrer (e.g., if someone navigates directly to the insert page).

Button Version (with code-behind)

Add the button to your ASPX:

<asp:Button ID="btnBackToList" runat="server" Text="Back to List" OnClick="btnBackToList_Click" />

Then add this code to your code-behind file:

protected void btnBackToList_Click(object sender, EventArgs e)
{
    if (Request.UrlReferrer != null)
    {
        Response.Redirect(Request.UrlReferrer.ToString());
    }
    else
    {
        // Fallback to your default page if no referrer exists
        Response.Redirect("../YourDefaultPage.aspx");
    }
}

内容的提问来源于stack exchange,提问作者user2516641

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.15 06:27:52