2011-06-30 14 views
5

Qualcuno può spiegare il modo più semplice per fornire dati a un controllo utente all'interno di un ripetitore?Come fornire "dati" al controllo utente all'interno del ripetitore?

Ho il seguente:

Default.aspx

<!-- this.GetData() returns IEnumerable<Object> --> 
<asp:Repeater runat="server" datasource='<%#this.GetData()%>'> 
    <ItemTemplate> 
     <my:CustomControl runat="server" datasource='<%#Container.DataItem %> 
    </ItemTemplate> 
</asp:Repeater> 

Codebehind

protected void Page_Load(object sender, EventArgs e) 
    { 
     this.DataBind(); 
    } 

CustomControl.ascx

<!-- Object has property Title --> 
<h1><%#this.DataSource.Title%></h1> 

Codebehind:

[System.ComponentModel.DefaultBindingProperty("DataSource")] 
public partial class CustomControl : System.Web.UI.UserControl 
{ 
    public Item DataSource { get; set; } 

    protected void Page_Load(object sender, EventArgs e) 
    { 
     var x = this.DataSource; //null here 
    } 

    protected void Page_PreRender(object sender, EventArgs e) 
    { 
     var x = this.DataSource; //still null 
    } 
} 
+0

è 'this.GetData()' chiamato prima o dopo 'Page_Load' dove sta avvenendo la tua associazione dati. Puoi semplicemente spostare '.GetData()' appena prima del tuo collegamento dati in 'Page_Load'? – Jay

+0

this.GetData() viene chiamato con l'espressione di associazione '#'. Quindi si chiama ON databinding ... – Ropstah

risposta

5

Si potrebbe aggiungere proprietà al controllo utente quindi impostare questi durante la databind.

come questo:

<!-- this.GetData() returns IEnumerable<Object> --> 
<asp:Repeater runat="server" datasource='<%#this.GetData()%>'> 
    <ItemTemplate> 
     <my:CustomControl runat="server" title='<%#Container.DataItem.title %> 
    </ItemTemplate> 
</asp:Repeater> 

Codebehind

protected void Page_Load(object sender, EventArgs e) 
{ 
    this.DataBind(); 
} 

CustomControl.ascx

<!-- Object has property Title --> 
<h1><%#this.Title%></h1> 

Codebehind:

[System.ComponentModel.DefaultBindingProperty("DataSource")] 
public partial class CustomControl : System.Web.UI.UserControl 
{ 
    public Item DataSource { get; set; } 

    public string title { get; set; } 

    protected void Page_Load(object sender, EventArgs e) 
    { 
     var x = this.DataSource; //null here 
    } 

    protected void Page_PreRender(object sender, EventArgs e) 
    { 
     var x = this.DataSource; //still null 
    } 
} 
+0

Questo è quello che sto facendo giusto? In qualche modo non viene passato ... – Ropstah

+0

Quanto sopra è leggermente diverso in quanto non dipende da database all'interno di usercontrol, solo sul database della pagina. Ho in passato che un page.databind non si è propagato all'usercontrol. Non posso dirlo con certezza, ma sono abbastanza sicuro che sia così. –

-1

Ho trovato una soluzione elegante su un post diverso qui: ASP.NET Loading a User Control in a Repeater da Anthony Pegram

Sommario:

Utilizzando ItemDataBound del ripetitore (object sender, RepeaterItemEventArgs e) evento per spingere informazioni nelle proprietà Web di controllo utente (che hai creato).

Problemi correlati