0

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?

4 Answers 4

2

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 --> How to edit CSS style of a div using C# in .NET

Sign up to request clarification or add additional context in comments.

1 Comment

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

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:

Is it possible to use Data-Binding Expressions directly in markup to show/hide content?

Which may answer the most important part of your question.

1 Comment

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

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\"") : "" %>">

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.