3

The following code:

aspx file:

<div id="challengedIndependence" class="collegeAccentBlock grid_8 push_1" runat="server" clientidmode="Static" style='display:<%# testMethod %>' >

associated aspx.cs file:

  public string testMethod()
    {
        return "none;";
    }

rendered html file:

<div id="challengedIndependence" class="collegeAccentBlock grid_8 push_1" style="display:&lt;%# testMethod %&gt;">
</div>

I am expecting the result to rendder as ...style:"display:none;"... what am I doing wrong.

Not sure if this should be in the same question: I am doing this because I have multiple lines of code to determine whether this div should be displayed. If there is another way of doing this and an explanation as to why it is better I would also accept that answer.

Reference: Inline expressions referenced on MSDN

3
  • 1
    Where are you performing the data binding? Commented Jul 15, 2011 at 14:30
  • Does it work with property instead of a method? Commented Jul 15, 2011 at 14:38
  • After Oded's comment I changed the code to use the display expression directive <%= %> which did not work and I m unsure why. The data binding occurs as file.aspx.cs for file.aspx Commented Jul 15, 2011 at 16:06

2 Answers 2

1

If you're going to use an inline expression here, it needs to be the entire content of that attribute, like this:

<div id="challengedIndependence" class="collegeAccentBlock grid_8 push_1" runat="server" clientidmode="Static" style='<%# testMethod() %>' >

and then return the entire string you want there. It's just rendering it as part of the string.

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

Comments

0

Dictate an UI style by code behind looks not right, but anyway try to return whole style string like return "display:none" and bind to style attribute

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.