如何在asp:HyperLinkField中使用C#变量

本文关键字:变量 HyperLinkField asp | 更新日期: 2023-09-27 18:00:04

这是我所拥有的。页面加载,但超链接不是超链接。我在浏览器中查看页面的源代码,在a href中看到的字符串与下面的DataNavigateUrlFormatString属性中看到的相同。我尝试了许多不同的方法,在执行过程中要么没有链接,要么出现错误。

<asp:HyperLinkField DataNavigateUrlFields="LIDCode" 
DataNavigateUrlFormatString='<%# Request.ServerVariables["SCRIPT_NAME"] %>?LC={0:d}&DD=true'>
</asp:HyperLinkField>

如何在asp:HyperLinkField中使用C#变量

如果DataNavigateUrlFormatString给我带来了麻烦,因为我无法将内联服务器标记嵌入另一个服务器标记的属性中(这就是问题所在),我要做的就是在代码后面设置DataNavigateUrlFormatString属性,例如在Page_Load Event 中

private void Page_Load()
{
    if (!IsPostBack)
    {
       string strUrlFormat = Request.ServerVariables["SCRIPT_NAME"] + "?LC={0:d}&DD=true"
       // get the gridview object and set the DataNavigateUrlFormatString property for the column in question here...  so if you gridview ID = "myGridView" and it is the third column...
       myGridView.Columns[2].DataNavigateUrlFormatString = strUtlFormat;
    }
}

试试这样的方法:-创建公共静态方法GetLink()-更新超链接中的代码,使其看起来像这个

DataNavigateUrlFormatString='<%# PageName.GetLink() %>' 
public static string GetLink()
{
    //needs to be completed but you see the point
    return HttpContext.Current.Request.ServerVariables["SCRIPT_NAME"];
}