嵌入式IF说明书在GridView控件组件 [英] Embeded IF statment in gridview component
本文介绍了嵌入式IF说明书在GridView控件组件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想有一个条件,我网格视图的列中的一个,它可能包含了一些字符串(6-10位)或字符(3-6)。是这样的:
I would like to have a condition in one of my Grid-view's column,which it may contains a number string (6-10 digits) or characters (3-6). Something like :
<asp:HyperLink ID="HL_Number" runat="server" Text='<%# Eval("Code")%>' Target="_blank"
NavigateUrl='<%# "http://www.address.com/" + Eval("Code")%>'> Visible='<% (IsNumber(Eval("Code"))==true)? true:false %>'
</asp:HyperLink>
<br />
<asp:HyperLink ID="HL_String" runat="server" Text='<%# Eval("Code")%>' Target="_blank"
NavigateUrl='<%# "~/PDF/" + Eval("Code")+"pdf" %>' Visible='<% (IsNumber(Eval("Code"))==false)? true:false %>'>
</asp:HyperLink>
一个超链接都必须在同一时间是可见的,我怎么能执行呢?先谢谢了。
One of HyperLink must be visible at the same time, how can i perform it? Thanks in Advance.
推荐答案
从一个好的设计的角度来看,移动这个逻辑在业务层。比方说,这是你的实体
From a good design perspective, move this logic to your business layer. Let's say this is your entity
public class MyEntity
{
public int Id {get;set;}
// ... some other properties
public string Code {get;set;}
// if you need some other control to be visible based on
// whether Code is a number or not, use this to bind to Visible property.
// Note, this is not required in case of HyperLink
public bool IsVisible
{
{ get {return IsNumber(Code); }
}
public string NavigateUrl
{
get { return GetUrl(Code); }
}
private bool IsNumber(string code) { // your method body here }
private string GetUrl(string code)
{
if(!IsNumber(code))
{
return string.Format("~/PDF/{0}pdf", code);
}
return string.Format("http://www.address.com/{0}",code);
}
}
假设你的数据源是myEntity所对象的集合。
Assuming your datasource is collection of MyEntity objects.
var dataSource = // some method that returns collection of MyEntity objects,
// for example List<MyEntity>
myGridView.DataSource = dataSource;
myGridView.DataBind();
现在,只保留1 超链接
控制你的GridView,并将其绑定到相应的属性。
Now, keep only 1 HyperLink
control in your GridView and bind it to respective properties.
<asp:HyperLink ID="HL_String" runat="server" Text='<%# Eval("Code")%>' Target="_blank"
NavigateUrl='<%# Eval("NavigateUrl") %>'>
</asp:HyperLink>
这篇关于嵌入式IF说明书在GridView控件组件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文