如何将 BoundField 转换为 HyperLinkField?
我有一个绑定到数据集 (ds
) 的 GridView (gv
)。 Columns[1]
绑定到 ds
中名为 orderFilename
的字段; Columns[6]
是日期字段。
如果 Columns[6] 为 null,我希望 Columns[1] 显示为文本;如果 Columns[6]
不为空,我希望 Columns[1]
显示为超链接,网址为 ~/directory/
+ 订单文件名
。
我在网上找到了一些可能的解决方案,但似乎都没有达到我想要的效果。任何帮助将不胜感激。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我更喜欢远离
BoundFields
特别是因为下一个人似乎总是需要将它们转换为模板字段来进行自定义。我建议执行以下操作:对列 1 使用带有
Literal
控件的模板字段:然后为列控件实现
OnDataBinding
:这样做的优点是您已本地化逻辑直接到控制。您可以轻松地更换它并对其进行更改,而不会意外影响网格的其他部分。
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:Then implement the
OnDataBinding
for the columns control: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.
假设您在
column[1]
中添加了一个超链接控件,如果column[6]
不为空,那么您可以设置NavigateURL
属性并设置 URL。在这种情况下,它看起来像一个超链接,如果column[6] 为 null
,那么您不需要设置 URL,因为它的行为就像文本。Let's say, you have added a hyperlink control in
column[1]
, If thecolumn[6]
is not null then you can set theNavigateURL
property and set the URL. In this case, it will look like a hyperlink and ifcolumn[6] is null
, then you don't need to set the URL, as it will behave like text.使用模板列,并在其中放置两个面板。一个面板包含链接,另一个面板包含文本。尝试这样的操作:
另一个选项,正如 @Muhammad Akhtar 建议的那样,无论如何都使用超链接,并且仅在 Column[6] 的 DataField 不为空时设置 URL。
Use a template column, and put two panels inside of it. One panel containing the link, and the other containing the text. Try something like this:
The other option, as @Muhammad Akhtar suggested, is to use a hyperlink regardless, and only set the URL when the DataField for Column[6] is not null.