如何运行一个if语句中的aspx? [英] How do I run an if statement in aspx?

查看:142
本文介绍了如何运行一个if语句中的aspx?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想运行一个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", ... %>' ...

(,

这篇关于如何运行一个if语句中的aspx?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆