c# - How to get item bound to control by repeater? -


i have 'basketcontrol' control inside of repeater:

<asp:repeater runat="server" id="rptitems">     <itemtemplate>         <uc:basketcontrol runat="server" id="ucbasket" />     </itemtemplate> </asp:repeater> 

the list of basketobject bound repeater datasource property.

public class myparentcontrol : system.web.ui.usercontrol {     void page_load(object sender, eventargs e) {         if(!ispostback) {             rptitems.datasource = ...; // list<basketobject>             rptitems.databind();         }     } } 

how can access 'basketobject' inside of basketcontrol? want have access basketobject inside of class basketcontrol, example inside of 'page_load' method.

public class basketcontrol : system.web.ui.usercontrol {     void page_load(object sender, eventargs e) {         // here want access basketobject         basketobject basket = .... basketobject;     } } 

should simple, can't find tips in google...

p.s. should handle 'onitemdatabound' repeater event , assign data object control? or there quicker/easier way?

thanks

usually how handle kind of things handling itemdatabound event.

so have markup:

<asp:repeater runat="server" id="rptitems">     <itemtemplate>         <uc:basketcontrol runat="server" id="ucbasket" />     </itemtemplate> </asp:repeater> 

the code this:

protected override void oninit(eventargs e) {     base.oninit(e);      this.rptitems += new repeateritemeventhandler(rptitems_itemdatabound); }  void page_load(object sender, eventargs e) {     if(!ispostback) {         rptitems.datasource = ...; // list<basketobject>         rptitems.databind();     } }  void rptitems_itemdatabound(object sender, repeateritemeventargs e) {     basketcontrol ucbasket = e.item.findcontrol("ucbasket") basketcontrol;     basketobject basket = e.item.dataitem basketobject;      // use datasource/databind, use property/method of basketcontrol     // so: ucbasket.loadbasket(basket);     ucbasket.datasource = basket;     ucbasket.databind(); } 

hope that's clear. of course, use autoeventwireup="true" , put name of event handler in markup of repeater. basic idea still same.

edit in response clarification, you're going have explicitly wire them how. there isn't automatic way of doing it.

i suggest exposing basket property on basketcontrol. this:

<%@ import namespace="your webapp namespace here" %>  <asp:repeater runat="server" id="rptitems">     <itemtemplate>         <uc:basketcontrol runat="server" id="ucbasket" basket='<%# (basket)container.dataitem %>' />     </itemtemplate> </asp:repeater> 

and wouldn't need handle itemdatabound event. if there's way of doing this, i'm not sure is.


Comments

Popular posts from this blog

c# - SharpSVN - How to get the previous revision? -

c++ - Is it possible to compile a VST on linux? -

url - Querystring manipulation of email Address in PHP -