I have a UserControl inside a repeater. The repeater's datasource is from SQL Server.
User Control's .cs - MoviePanel.ascx.cs:
public int myMovieID { get; set; }
public string myMovieName { get; set; }
public string myMovieDescription { get; set; }
protected void Page_Load(object sender, EventArgs e)
{
MovieIDLbl.Text = myMovieID.ToString();
MovieNameLbl.Text = myMovieName;
DescriptionLbl.Text = myMovieDescription;
}
ASPX Page:
<asp:Repeater ID="Repeater1" DataSourceID="ListOfMoviesDS" runat="server">
<ItemTemplate>
<uc1:MovieDetailPanel runat="server" myMovieID='<%# Eval("MovieID") %>'
myMovieName='<%# Eval("movieName") %>'
myMovieDescription='<%# Eval("movieDescription") %>'
id="MovieDetailPanel1" />
<asp:Label ID="Label1" runat="server"
Text='<%# Eval("MovieID") %>'></asp:Label>
<asp:Label ID="Label2" runat="server"
Text='<%# Eval("movieName") %>'></asp:Label>
<asp:Label ID="Label3" runat="server"
Text='<%# Eval("movieDescription") %>'></asp:Label>
</ItemTemplate>
</asp:Repeater>
Here something very strange happens. The values are not getting passed to the UserControl. However, if I place Labels below the usercontrol and set text with Eval()
it works. You might think the usercontrol might be the problem. But if I manually type something, say in place of <%# Eval("movieName") %>
it gets passed to the user control and gets displayed.
I have NO CLUE! If the problem is with Eval() Labels should not get the text as well. Or if the problem is with the UserControl my manual text shouldn't get passed. I have no idea why Eval()'s values is not reaching the UserControl.
here is one way, doing it all in the code behind. I can't say this is best practice, but it's clean. You use the ItemDataBind event, cast the item to what you want, datatable, whatever, then create a new instance of the user control and add it to the repeater's control collection
Page source
Page html
user control
and
Your code is working fine; I tested it (see the bottom of the page). The worst case you can try assigning those value inside ItemDataBound event.
Here is how I test your original question