0

I have a div with a background image who's url is not being bound when the page loads. The URL is set in a get method in the code behind. Not sure what I'm doing wrong here.

The HTML:

<div class="decorator" style="background: transparent url(<%# HeadlineBackgroundImagePath %>) top center no-repeat; height: <%# HeadlineBackgroundImageHeight %>px;">

The code behind:

    protected string HeadlineBackgroundImagePath
    {
        get
        {
            return ($CompanyContext.Entity as IPPCUpdateEntity).HeadlineBackground.ShortSrc;
        }
    }

    protected int? HeadlineBackgroundImageHeight
    {
        get
        {
            return ($CompanyContext.Entity as IPPCUpdateEntity).HeadlineBackground.Height;
        }
    }

When the page loads, I get something like this (notice nothing is in the URL):

<div class="decorator" style="background: transparent url() top center no-repeat; height: px;">

I've gone into debugger, set a breakpoint on the property, but the site doesn't even call up the property.

6
  • Please show us the actual output code instead of comments. Or is that the bug here... Commented Jun 24, 2013 at 16:51
  • 2
    Use these tags <%=whatever %> Commented Jun 24, 2013 at 16:52
  • The last code section has the output. The problem is "url()". There should be a path in there. The <%# HeadlineBackgroundImagePath %> should be calling the property in the code behind, but it actually doesn't. Commented Jun 24, 2013 at 16:53
  • where is set ? where are u setting the value ? Use get-set Commented Jun 24, 2013 at 16:53
  • 1
    Yes @Ani he is using databinding syntax not Response.Write syntax. He should use <%= blah %> or if he uses <%# blah %> then he may have to call Page.Databind Commented Jun 24, 2013 at 16:53

2 Answers 2

1

This is how you use get-set :

private string _headlineimg ="N/A";
public property HeadlineBackgroundImagePath() As string{
    get{
        return _headlineimg
   }
    set{
        _headlineimg = value
    }
}

then On page load event:

HeadlineBackgroundImagePath = "whatever";
Sign up to request clarification or add additional context in comments.

5 Comments

The problem isn't setting the property. It's that the HTML isn't calling the property.
Did you change the tag to : <%=whatever %> ?
When I use <%=, I simply get <div class="decorator" style="background: transparent url(<%= HeadlineBackgroundImagePath %> ......... IF I throw runat="server" onto the div tag. Otherwise it throws an error "The Controls collection cannot be modified because the control contains code blocks"
Can you check by adding a breakpoint to see if you are actually getting some value in HeadlineBackgroundImagePath ?
It was being set, figured it out though. Thanks for your help!
0

Ani's answer got me on the right path, but was only part of the solution. I set up the property as suggested, but I needed to add one line of code after setting the image path in the code behind. In the page load event, I set the property and then called Page.DataBind() to get it to work.

    protected void Page_Load(object sender, EventArgs e)
    {
        try
        {
            HeadlineBackgroundImagePath = ($CompanyContext.Entity as IPPCUpdateEntity).HeadlineBackground.ShortSrc;
            Page.DataBind();
            if (!String.IsNullOrEmpty(($CompanyContext.Entity as IPPCUpdateEntity).FloodlightTag))
                floodlightTag.Text = ($CompanyContext.Entity as IPPCUpdateEntity).FloodlightTag;
        }
        catch
        {
        }
    }

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.