Curtis White
Curtis White

Reputation: 6353

Declaratively access page properties data binding inside repeater

I have a public property on an ASP.NET Forms page. I can access this page property declaratively with the following code:

<asp:Image ID="PageImage" runat="server" ImageUrl="<%# MyProperty %>" />

However, once I put this same code inside a repeater, it does not work. I seek an explanation and a solution.

* Update *

The Eval is not needed and throws an eerror. You can just reference MyProperty. However, you must call DataBind on the page. Once you do that the above syntax works outside the repeater but returns empty inside the repeater.

Unfortunately, I'm no longer able to reproduce this bug. My hypothesis at this time is that the repeater didn't have any data bound to it, so it wasn't rendering.

Upvotes: 2

Views: 1179

Answers (2)

Curtis White
Curtis White

Reputation: 6353

Unfortunately, the syntax I was sure that didn't work appears to work just fine now. My hypothesis is that the repeater didn't have any data bound too it, so the data wasn't display. The page had several repeaters on it and that may have been the cause of my confusion. Once you Page.Databind() and also ensure the repeater has data the original syntax works.

Update: Interestingly, the code expression syntax <%: and <%= allow page properties to be accessed without data binding.

Upvotes: 1

TombMedia
TombMedia

Reputation: 1972

There is more going on here than meets the eye :) Take this code and paste it into a new empty asp.net webforms project brand new webforms page:

    <script runat="server">
        protected string MyProperty
        {
            get
            {
                return "SUCCESS";
            }
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            rTest.DataSource = new int[] { 1, 2, 3 };
            rTest.DataBind();
        }
    </script>
    <%: MyProperty  %><br />
    <asp:Literal ID="ltrlTest" runat="server" Text='<%# MyProperty %>' /><br />
    <asp:Repeater runat="server">
        <ItemTemplate>
            <asp:Literal runat="server" Text="<%# MyProperty %>" /><br />
        </ItemTemplate>
    </asp:Repeater>

You get as output:

SUCCESS

SUCCESS
SUCCESS
SUCCESS

Why? Well the syntax you have provided is for databinding, and only works when a control has its DataBind() method called. I tried and tried and could not get your original syntax to work (hence the empty line output) so unless I've got something wrong your code snippet would only work inside a usercontrol or a page/control that is being data-bound.

As you can see from the example you can totally access those properties from inside a repeater template, and you can output that property to the page using = or : but to get things to work the way you described needs more magic than what the default scenario is.

Take a look at this SO question, and take a look at the databinding expression syntax area on this msdn article.

Now if I add ltrlTest.DataBind() to page load I will get 5 successes output. Thats the only way the <%# syntax will work.

Hope that helps clear things up.

Upvotes: 3

Related Questions