tags:

views:

218

answers:

4

Hi.

Inside my aspx file, I have the following html code inside an repeater: <div class="someItem ">.

Now, if Eval("Approved") == true, then I would like to add the class approved to the div.
So the new html would be <div class="someItem approved">.

I tried doing something like this:

<%# if(Eval("Approved")) approved %>

But that didn't work very well. Any ideas?

+1  A: 

maybe it would be better to consider this if the item is approved <div id="someItem"> and then add class approved if the eval of whatever is true ..

<div id="someItem" "<%# Eval("Approved") ? Response.Write("class=\"approved\"") : "" %>">
c0mrade
to use eval you must put #, your example will not work
Amr ElGarhy
yes you are correct my bad ..
c0mrade
+1  A: 

If can't work in databinding # line.

So you can work around this by something like this:

<%# isApproved = Convert.ToBoolean(Eval("Approved").ToString()) %>
<% if(isApproved){ %>
  <div class="someItem approved">
<%}%>

And don't forget to declare isApproved as protected in the code behind.

Also please take a look at this question:

http://stackoverflow.com/questions/1553881/is-it-possible-to-use-data-binding-expressions-directly-in-markup-to-show-hide-co

Which may answer the most important part of your question.

Amr ElGarhy
Hi Amr. Thanks for good answer. Your first line does not store the value in 'isApproved' variable. It just outputs the text "true" or "false". I was hoping to avoid code behind. But it looks like I have to call a function and pass the object Eval("approved").
Steven
+1  A: 

You can make the div a server control as -->

<div id="myDIV" class="myClass" runat="server"></div>.

Then you can directly access it from your code behind, as

myDIV.Attributes["class"] = "classOfYourChoice";

See this --> http://stackoverflow.com/questions/175381/how-to-edit-css-style-of-a-div-using-c-in-net

Bhaskar
Hum... I didn't know I could make html tags into a server control. Thanks for the tip!
Steven
A: 

You can use an asp.net panel and control the action of it from the code behind. A panel renders as a DIV and you have access to it's CssClass during the runtime.

Chris