开发者

How do I run an if statement in aspx?

开发者 https://www.devze.com 2023-01-20 07:22 出处:网络
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 -

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开发者_如何学Go").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", ... %>' ...

(,


I know this doesn't completely answer your question but why not just do this in the code behind? I'm assuming you're doing something with DataBinding?

string temp  = (string)DataBinder.Eval(e.Item.DataItem, "idnbr");
string newAmount = (string)DataBinder.Eval(e.Item.DataItem, "new_amt");
string oldAmount = (string)DataBinder.Eval(e.Item.DataItem, "old_amt");
Label lbl1        = e.Item.FindControl("label1") as Label;

if(temp.Contains("X") {
 lbl1.Text = oldAmount;
} else {
 lbl1.Text = newAmount;
}


You can read from a property that is declared in the code behind; does this satisfy what you want?

Instead of string temp = ..., you can use this.MyProperty.Contains("X")...


<a href="javascript:onclick= window.location = 'RenewalPaymentGateway.aspx?RPID=<%# Eval("RPID")%>'" title="Pay">
 <asp:Label ID="TEMP" Text='<%# If(Eval("PaymentStatus").ToString() = "Paid", "View", "Make payment") %>'  runat="server" />

here in label the text view will appear when PaymentStatus=paid or the text will be make payment
0

精彩评论

暂无评论...
验证码 换一张
取 消