c# - 如何将BoundField转换为HyperLinkField?

原文 标签 c# asp.net gridview hyperlink boundfield

How do I convert a BoundField to a HyperLinkField?

I have a GridView (gv) bound to a dataset (ds). Columns[1] is bound to a field in ds named orderFilename; Columns[6] is a date field.

If Columns[6] is null, I want Columns[1] to appear as text; if Columns[6] is not null, I want Columns[1] to appear as a hyperlink, with a url ~/directory/ + orderFilename.

I have found a couple possible solutions on the Web but none seem to do what I want. Any help would be appreciated.

Answer

I prefer to stay away from BoundFields specifically because the next guy needs to always seem to convert them to template fields anyways to do customizations. I would recommend the following:

Use a template field with a Literal control for your column 1:

<asp:TemplateField HeaderText="File">
    <ItemTemplate>
        <asp:Literal ID="ltFilename" runat="server" 
            OnDataBinding="ltFilename_DataBinding" />
   </ItemTemplate>
</asp:TemplateField>

Then implement the OnDataBinding for the columns control:

protected void ltFilename_DataBinding(object sender, System.EventArgs e)
{
    Literal lt = (Literal)(sender);
    if (Eval("yourColumn6Field") == DBNull.Value)
    {
        // just show a text filename
        lt.Text = Eval("orderFilename").ToString();
    }
    else
    {
        // produce the link
        lt.Text = string.Format("<a href='{0}'>{1}</a>",
             ResolveUrl("~/directory/" + Eval("orderFilename").ToString()),
             Eval("orderFilename").ToString());
    }
}

The advantage to this is you have localized the logic directly to the control. You can easily swap it out and change it around without affecting other parts of the grid accidently.

翻译

我有一个绑定到数据集(gv)的GridView(ds)。 Columns[1]绑定到ds中名为orderFilename的字段; Columns[6]是日期字段。

如果Columns[6]为null,我希望Columns[1]以文本形式显示;如果Columns[6]不为null,我希望Columns[1]以超链接的形式出现,并带有URL ~/directory/ + orderFilename

我在网上找到了两种可能的解决方案,但似乎都没有我想要的。任何帮助,将不胜感激。
最佳答案
我更喜欢远离BoundFields,这是因为下一个家伙总是需要始终将它们转换为模板字段以进行自定义。我建议以下内容:

在第1列中使用带有Literal控件的模板字段:

<asp:TemplateField HeaderText="File">
    <ItemTemplate>
        <asp:Literal ID="ltFilename" runat="server" 
            OnDataBinding="ltFilename_DataBinding" />
   </ItemTemplate>
</asp:TemplateField>


然后为列控件实现OnDataBinding:

protected void ltFilename_DataBinding(object sender, System.EventArgs e)
{
    Literal lt = (Literal)(sender);
    if (Eval("yourColumn6Field") == DBNull.Value)
    {
        // just show a text filename
        lt.Text = Eval("orderFilename").ToString();
    }
    else
    {
        // produce the link
        lt.Text = string.Format("<a href='{0}'>{1}</a>",
             ResolveUrl("~/directory/" + Eval("orderFilename").ToString()),
             Eval("orderFilename").ToString());
    }
}


这样做的好处是您可以将逻辑直接本地化到控件。您可以轻松地交换出来并进行更改,而不会意外影响网格的其他部分。
相关推荐

c# - 如何获得父级AppDomain?

c# - 枚举值的生成常数

c# - 关于python正则表达式的两个问题

c# - 连接来自C#中两个数据库的表

c# - C#委托具有不同可选参数的方法

c# - 使用货币时的最佳做法

c# - LINQ to XML C#获取根元素属性

c# - C#网络凭据未传递到服务器?

c# - CPU使用率问题

c# - 我可以强制子类实现静态解析方法吗?