c# – 中继器内部的用户控制

前端之家收集整理的这篇文章主要介绍了c# – 中继器内部的用户控制前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我在一个中继器里面有一个UserControl.中继器的数据源来自sql Server.

用户控件的.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页面

<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>

这里发生了很奇怪的事情.这些值不会传递给UserControl.但是,如果我将Label标签放在用户控件下面,并使用Eval()设置文本,它可以工作.您可能会认为用户控件可能是问题.但是,如果我手动输入一些内容,则说明代替<%#Eval(“movieName”)%>它被传递给用户控件并被显示.

我没有任何线索!如果问题在于Eval()标签也不应该得到文本.或者如果UserControl的问题是我的手动文本不应该被通过.我不知道为什么Eval()的值没有达到UserControl.

解决方法

你的代码正常工作我测试了(见页面底部).最糟糕的情况是您可以尝试在ItemDataBound事件中分配这些值.
<asp:Repeater ID="Repeater1" runat="server" OnItemDataBound="Repeater1_ItemDataBound">
    <ItemTemplate>
        <uc1:MoviePanel runat="server" id="MovieDetailPanel1" />
    </ItemTemplate>
</asp:Repeater>

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<Movie>
        {
            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;
    }
}

这是我如何测试你的原始问题

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="WebDemo.WebForm1" %>

<%@ Register src="MoviePanel.ascx" tagname="MoviePanel" tagprefix="uc1" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
        <asp:Repeater ID="Repeater1" runat="server">
            <ItemTemplate>
                <uc1:MoviePanel runat="server" mymovieid='<% #Eval("MovieID") %>'
                    mymoviename='<% #Eval("movieName") %>'
                    mymoviedescription='<% #Eval("movieDescription") %>'
                    id="MovieDetailPanel1" />
            </ItemTemplate>
        </asp:Repeater>
    </form>
</body>
</html>


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<Movie>
                {
                    new Movie {MovieID = 1,};
                Repeater1.DataBind();
            }
        }
    }
}

<%@ Control Language="C#" AutoEventWireup="true"
    CodeBehind="MoviePanel.ascx.cs" Inherits="WebDemo.MoviePanel" %>

<p>
    <strong>Inside Control</strong>:
    <asp:Label ID="MovieIDLbl" runat="server" />
    <asp:Label ID="MovieNameLbl" runat="server" />
    <asp:Label ID="DescriptionLbl" runat="server" />
</p>

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;
        }
    }
}

猜你在找的C#相关文章