How to apply conditions to databound objects

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • DaveRook
    New Member
    • Jul 2007
    • 147

    How to apply conditions to databound objects

    I use a repeater on my website.

    My code for this example is

    Code:
    <ItemTemplate>
    Item: <%#DataBinder.Eval(Container.DataItem, "Col")%><br>
    </ItemTemplate>
    Now, depending on the result of the dataitem will depend on what I want to display. For example, The databitem may show a true or false value, and depending on which one I want to show a different result on my page. So instead of it display true, it display a string. EG, if true, the text 'in stock' would display.

    At the moment, I am using a method to call a function (please correct my terminology - or let me know my terminology is right!).
    html:
    Code:
    <ItemTemplate>
    Item: <%#CB(DataBinder.Eval(Container.DataItem, "Col"))%><br>
    </ItemTemplate>
    code behind:
    Code:
    public string CB(object o)
    {
    //perform something
    return string.format("");
    }
    Is this the best way as it appears to be an ugly fix in my opinion. Especially as I have several items on my page which will have to use this (ususally just checking if there is a value in the dataitem or if it is empty/null)

    Thanks

    Dave
  • Frinavale
    Recognized Expert Expert
    • Oct 2006
    • 9749

    #2
    I used to do the same thing but changed my approach to use an If function or an IIF function instead.

    You're using C#...so I'm not sure if you have the If-function at your disposal and I'm not sure if the same peculiar problems exist with the IIF in C# as exist in VB.NET(where the true and false parts of the method are always executed...whic h can be a problem if the if-statement was checking if something was null)

    If your logic is complex then it might be a good idea to keep doing what you are doing...

    But if you are trying to do something simple like printing "in stock" instead of "true" then the If-function or IIF-Function will do nicely.

    -Frinny
    Last edited by Frinavale; Oct 21 '10, 02:44 PM.

    Comment

    • DaveRook
      New Member
      • Jul 2007
      • 147

      #3
      Hi Frinny

      Thank you (again) for your help. As disapointing as it is to have to continue this way as sometimes the logic is more complicated than simple if else statements, it is quite re-assuring to know this is a 'normal' programming dilema and there is not a simple solution. At least I'm on the right path and using the language effectively!

      I am using T-SQL and C# so I guess it's case statements for me!!

      Thank you again for your feedback

      Dave

      Comment

      Working...