databinding expression in asp.net
I have an VS2005 ASP.Net page with a repeater on it of Customers.
The following shows up in the of the repeater:
<span><%# Eval(GetAdLinks((Customer)Container.DataItem)) %></span>
The GetAdLinks is a protected method in the code behind which returns a co开发者_JS百科ntrol represented as a string. Is this possible?
I'm getting an error that says Customer does not contain a property with the name ...
Any ideas?
The better wayu to do this would be to add a Literal
to your Repeater
template and then implement the OnDataBinding method. This is exactly what a Literal
is for.
Here is an example:
<asp:Literal ID="litYourControl" runat="server" OnDataBinding="litYourControl_DataBinding" />
Then in your codebehind implement it:
protected void litYourControl(object sender, System.EventArgs e)
{
Literal lt = (Literal)(sender);
// Not sure what field you are binding to based on the example in your question
// so I will just make an assumption.
Customer cus = (Customer)(Eval("Container.DataItem"));
lt.Text = GetAdLinks(cus);
}
If it's a client-side (no runat="server") control, then it should be possible. But you should not include it in an Eval(). I'm guessing it should look somewhat like this:
<%# GetAdLinks(Container.DataItem) %>
The GetAdLinks method should be refactored to accept the Container.DataItem as a DataRowView object. Inside the method, you can cast it to a Customer object, which you can figure out with a little debugging.
Good luck!
精彩评论