2013-11-22 65 views
14

我在中繼器內部有一個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。但是,如果我將標籤放在usercontrol下面,並使用​​設置文本,它就可以工作。您可能會認爲用戶控件可能是問題所在。但是,如果我手動鍵入某些內容,請將其替換爲<%# Eval("movieName") %>,它會傳遞給用戶控件並顯示出來。

我沒有CLUE!如果問題出在Eval()上,標籤也不應該獲取文本。或者,如果問題出在UserControl上,我的手冊文本不應該被傳遞。我不知道爲什麼Eval()的值沒有達到UserControl。

+0

可否請你讓我知道我應該如何傳遞值從後面的代碼在用戶控件?我可以分配轉發器的數據源,但數據源的值是如何傳遞給usercontrol的? – user1220169

+0

與論壇網站不同,我們不使用「謝謝」或「任何幫助表示讚賞」,或在[so]上簽名。請參閱「[應該'嗨','謝謝',標語和致敬從帖子中刪除?](http://meta.stackexchange.com/questions/2950/should-hi-thanks-taglines-and-salutations-be -removed-from-posts) –

+0

在頁面生命週期的哪一點你檢查是否設置了值?你正在使用數據綁定表達式,所以這些將在'DataBinding'階段被設置。 –

回答

13

您的代碼工作正常;我測試了它(請參閱頁面底部)。最糟糕的情況是您可以嘗試在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; 
    } 
} 

enter image description here

這裏是我如何測試你原來的問題

<%@ 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, 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" %> 

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

是的,但是您如何在用戶控件上使用綁定?這裏你使用的是Repeater上的綁定,但是在用戶控件上沒有使用Binding。相反,這段代碼是手動設置Text屬性。這只是沒有意義。如果不能使用綁定,使用用戶控件有什麼意義? – Rhyous

+0

@Rhyous這可能是OP想要與其他控件分離一些業務邏輯。 – Win

4

這裏是一個辦法,做這一切在後面的代碼。我不能說這是最好的做法,但它很乾淨。您可以使用ItemDataBind事件,投的項目你想要什麼,數據表,不管,然後創建用戶控制的新實例,並將其添加到中繼器的控件集合

頁源

public partial class _Default : Page 
{ 
    protected void Page_Load(object sender, EventArgs e) 
    { 
     var myList = new List<string>() { "one", "two", "three" }; 
     myRepeater.DataSource = myList; 
     myRepeater.DataBind(); 
    } 

    public void R1_ItemDataBound(Object Sender, RepeaterItemEventArgs e) 
    { 
     var items = (string)e.Item.DataItem; 
     var newcontrol = (WebUserControl1)Page.LoadControl("~/WebUserControl1.ascx"); 
     newcontrol.myTest = items; 
     myRepeater.Controls.Add(newcontrol);   
    } 
} 

頁HTML

<asp:Repeater ID="myRepeater" runat="server" OnItemDataBound="R1_ItemDataBound"> 
    <ItemTemplate> 

    </ItemTemplate> 
</asp:Repeater> 

用戶控制

<h1 id="myLabel" runat="server"></h1> 

public partial class WebUserControl1 : System.Web.UI.UserControl 
{ 
    public string myTest { get; set; } 
    protected void Page_Load(object sender, EventArgs e) 
    { 
     myLabel.InnerText = myTest; 
    } 
}