我想运行一个if语句,但条件使用一个变量从背后code。我如何调用该变量? 附注...我使用一个GridView和变量是在数据集(dsResult - idnbr科拉姆)
<的ItemTemplate>
<%字符串气温=(EVAL(idnbr)的ToString());
如果(temp.Contains(X)){%GT;
< ASP:标签ID =标签1=服务器文本='<%#(EVAL(old_amt)的ToString(),ccTot。)%>'>< / ASP:标签&gt ;
<%)其他(%>
< ASP:标签ID =Label2的=服务器文本='<%#(EVAL(new_amt)的ToString(),ccTot。)%>'>< / ASP:标签&gt ;
<%}%GT;
< / ItemTemplate中>
解决方案
创建会为你的C#端的方法: 不是使用的2种方法之一:
如果您处理的知名实体(说时,GridView控件绑定到ObjectDataSource的)你 只需将它转换为你的实体,并传回:C#:
保护字符串MySelectorImpl(对象rowData)
{
myEntity所ENT =(myEntity所)rowData;
如果(ent.idndr ....)
返回 ....
其他
返回 ...
}
ASP.Net:
<的ItemTemplate>
< ASP:标签文本='<%#MySelector(Container.DatatItem)%> ...
第二种情况 - 只使用eval语法
C#:
保护字符串MySelector(对象条件,对象值1,对象值2)
{
如果((串)的条件....)返回value1.ToString ....
}
ASP.Net:
<的ItemTemplate>
< ASP:标签文本='<%#MySelector(Container.DatatItem(idnbr,......%>...
(
I would like to run an if statement but the condition uses a variable from the code behind. How do I call that variable? Side note... I am using a gridview and the variable is in a dataset (dsResult - idnbr colum)
<ItemTemplate>
<% string temp = (Eval("idnbr").ToString());
if (temp.Contains("X")) { %>
<asp:Label ID="Label1" runat="server" Text='<%# (Eval("old_amt").ToString(),"ccTot") %>'></asp:Label>
<% } else { %>
<asp:Label ID="Label2" runat="server" Text='<%# (Eval("new_amt").ToString(),"ccTot") %>'></asp:Label>
<% } %>
</ItemTemplate>
解决方案
Create c# side method that does it for you: Than use one of 2 ways:
If you deal with well known entity (saying when GridView bound to ObjectDatasource) you can just cast it to your entity and pass back:C#:
protected String MySelectorImpl(Object rowData)
{
MyEntity ent = (MyEntity)rowData;
if(ent.idndr .... )
return ....
else
return ...
}
ASP.Net:
<ItemTemplate>
<asp:Label Text='<%# MySelector(Container.DatatItem) %>' ...
Second case - just use eval syntax
C#:
protected string MySelector(Object condition, Object value1, Object value2)
{
if((String)condition ....) return value1.ToString ....
}
ASP.Net:
<ItemTemplate>
<asp:Label Text='<%# MySelector(Container.DatatItem("idnbr", ... %>' ...
(,