Click here to Skip to main content
15,889,863 members
Please Sign up or sign in to vote.
4.50/5 (2 votes)
See more:
Hello all!

So I am running into a seriously troublesome issue. I am using an <asp:Repeater> within my page and binding it to a collection of a custom class. For some reason the repeater seems to be able to bind to the "DisplayText" property of my class without a problem, but when it tries to bind to the "NavigationUrl" property of my class it gives me the following error:

DataBinding: 'myNameSpace.helper' does not contain a property with the name 'NavigationUrl'.

I don't have the faintest idea as to why this could be happening to one property without happening to the other. Here are some code snippets:

The custom class

C#
public class helper
    {
        public string NavigationUrl, DisplayText;

        public helper(string inputUrl, string inputText)
        {
            NavigationUrl = inputUrl;
            DisplayText = inputText;
        }
    }


The method that calls databinding

C#
public void setData()
        {
            Collection<helper> dataToBind = pushDataIntoHelper(returnedData);

            if (dataToBind.Count() > 0)
            {
                rptrMerchandising.DataSource = dataToBind;
                rptrMerchandising.DataBind();
            }
        }


The repeater with eval statements

XML
<asp:Repeater ID="rptrMerchandising" runat="server">
    <ItemTemplate>
        <div class="suggestion">
            <a href='<%# Eval("NavigationUrl") %>'><%# Eval("DisplayText") %></a>
        </div>
    </ItemTemplate>
</asp:Repeater>


If I attach a OnItemDataBound method to the repeater and use e.Item.FindControl with an <asp:Hyperlink> control instead of an anchor tag I can access the properties no problem. It only seems that when I try to evaluate it in the page that I don't have visibility.

Please let me know if you can share some insight as to what's going on here. This page is going to be awfully sloppy if I'm switching between populating via Eval statements and populating via OnItemDataBound events ><<br mode="hold" />
Thank you in advance! :)
Posted
Updated 12-Apr-11 4:42am
v3
Comments
web works 12-Apr-11 10:29am    
Could you please try this way and see whether it works or not? Let me know what error you get this time.
Just to identify the problem.
<%# Eval("NavigationUrl") %>
suprcodr 12-Apr-11 10:45am    
web works,

If I'm interpreting your request correctly (you're asking me to try just placing that single Eval statement within the ItemTemplate?) then I already have tried that and got the same error. I have also tried using the DataBinder.Eval(Container.DateItem, "NavigationUrl") approach as well. Thank you for your comment(s)!
web works 12-Apr-11 11:29am    
Some how i am not able to write the code here.
I just wanted to try to put 'NavigationUrl' in place of 'DisplayText' and 'DisplayText' in place of 'NavigationUrl'
I mean just change the position for testing and see what happens. Or try what i solution i have provided below.

As the error message says: DataBinding: 'myNameSpace.helper' does not contain a property with the name 'NavigationUrl'.

Looking at the helper class this is also very correct.
C#
public class helper
    {
        public string NavigationUrl, DisplayText;
        public helper(string inputUrl, string inputText)
        {
            NavigationUrl = inputUrl;
            DisplayText = inputText;
        }
    }


You need to convert NavigationUrl, DisplayText to properties instead of public variables.

Good luck!
 
Share this answer
 
Comments
suprcodr 12-Apr-11 11:27am    
Oh nice! I had the understanding that the titles of "property" and "variable" could be used interchangeably in C# but apparently I'm wrong. Do you know of any good reference material that I could look at that spells out differences like this? I've been more thrown into a "hands on" environment with my current situation and haven't had the chance to pour over the necessary pre-requisites yet. Thank you for the info!
E.F. Nijboer 12-Apr-11 11:50am    
Well, actually it is considered best practice to always use properties over variables (even a read/write property). This way the implementation within the class can change without noticeable for other code that is using the class. I guess that the problem you faced here had to do with the fact that the value internally is read using reflection. But anyway, sometimes you need to read the exception a few times and just paste it laterally into google. Of course, here at codeproject.com there is also assistance that can help.
About properties, since .net 3.0 there are auto-implemented properties that are as easy as declaring a variable. Have a look here: http://msdn.microsoft.com/en-us/library/bb384054.aspx
thatraja 12-Apr-11 12:48pm    
Good answer. 5!

Hey man glad to see you again here(Q/A) after a short break. Where were you? :)

BTW one suggestion....Use reply link in comment if you want reply OP's comment.

Cheers.
E.F. Nijboer 13-Apr-11 2:52am    
Thanks. Normally I use reply but it is new so I have to get a little used to it :-)
Best way, try to create 'Properties' instead of just creating Public variables.

Like

C#
private string mNavigationUrl;
public string NavigationUrl
{
    get
    {
        return mNavigationUrl;
    }
    set
    {
        mNavigationUrl = value;
    }
}
private string mDisplayText;
public string DisplayText
{
    get
    {
        return mDisplayText;
    }
    set
    {
        mDisplayText = value;
    }
}
 
Share this answer
 
Comments
suprcodr 12-Apr-11 11:27am    
Thank you for the clarification web works but unfortunately somebody else got to it first >< Much appreciated though!
web works 12-Apr-11 11:32am    
Never mind. Good Luck.
Happy Coding.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900