views:

98

answers:

3

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>
A: 

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")...

Mark Avenius
just have to make sure it's visible (public or protected)
David
@David Right... I meant that when I said property, but I suppose I should be explicit :-)
Mark Avenius
A: 

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;
}
Jack Marchetti
+1  A: 

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", ... %>' ...

(,

Dewfy