User Control Inside Repeater

前端 未结 2 1599
悲哀的现实
悲哀的现实 2020-12-29 08:13

I have a UserControl inside a repeater. The repeater\'s datasource is from SQL Server.

User Control\'s .cs - MoviePanel.ascx.cs:


public int m         


        
2条回答
  •  隐瞒了意图╮
    2020-12-29 08:35

    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.

    
        
            
        
    
    
    public class Movie
    {
        public int MovieID { get; set; }
        public string MovieName { get; set; }
        public string MovieDescription { get; set; }
    }
    
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            Repeater1.DataSource = new List
            {
                new Movie {MovieID = 1, MovieName = "One", MovieDescription = "One hundred"},
                new Movie {MovieID = 2, MovieName = "Two", MovieDescription = "Two hundreds"},
                new Movie {MovieID= 3, MovieName = "Three", MovieDescription = "Three hundreds"},
            };
            Repeater1.DataBind();
        }
    }
    
    protected void Repeater1_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.Item ||
            e.Item.ItemType == ListItemType.AlternatingItem)
        {
            var movie = e.Item.DataItem as Movie;
    
            var control = e.Item.FindControl("MovieDetailPanel1") as MoviePanel;
            control.myMovieID = movie.MovieID;
            control.myMovieDescription = movie.MovieDescription;
            control.myMovieName = movie.MovieName;
        }
    }
    

    enter image description here

    Here is how I test your original question

    <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="WebDemo.WebForm1" %>
    
    <%@ Register src="MoviePanel.ascx" tagname="MoviePanel" tagprefix="uc1" %>
    
    
    
    
    
        
    
    
        
    namespace WebDemo { public partial class WebForm1 : System.Web.UI.Page { public class Movie { public int MovieID { get; set; } public string MovieName { get; set; } public string MovieDescription { get; set; } } protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { Repeater1.DataSource = new List { new Movie {MovieID = 1, MovieName = "One", MovieDescription = "One hundred"}, new Movie {MovieID = 2, MovieName = "Two", MovieDescription = "Two hundreds"}, new Movie {MovieID= 3, MovieName = "Three", MovieDescription = "Three hundreds"}, }; Repeater1.DataBind(); } } } } <%@ Control Language="C#" AutoEventWireup="true" CodeBehind="MoviePanel.ascx.cs" Inherits="WebDemo.MoviePanel" %>

    Inside Control:

    namespace WebDemo { public partial class MoviePanel : System.Web.UI.UserControl { 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; } } }

提交回复
热议问题