Controls in DataList

Discussion in 'ASP .Net' started by David, Jan 27, 2005.

  1. David

    David Guest

    Hello.

    I have one question about DataList control:
    I have one DaaList control on my page in which I put some controls, for
    example I put two labels in header section and five labels in items section.
    But now I can't access this labels from code by their names:(
    So how can I access controls in DataList control form code?

    Thank you.
     
    David, Jan 27, 2005
    #1
    1. Advertisements

  2. David

    Ollie Riches Guest

    System.Web.UI.WebControls.DataList derives from System.Web.UI.Control and
    this has method called 'FindControl' this allows you to access web controls
    contained\hosted in the current control\page by there name (id field)

    e.g.

    if you had this defined in the aspx page

    <asp:DataList id="ItemsList" runat="server">

    <HeaderStyle BackColor="#aaaadd">
    </HeaderStyle>

    <AlternatingItemStyle BackColor="Gainsboro">
    </AlternatingItemStyle>

    <HeaderTemplate>

    List of items

    </HeaderTemplate>

    <ItemTemplate>

    <asp:Image id="ProductImage"
    ImageUrl='<%# DataBinder.Eval(Container.DataItem,
    "ImageValue") %>'
    runat="server"/>

    </ItemTemplate>

    </asp:DataList>


    in the code behind you could do this

    System.Web.UI.WebControls.Image imgCtrl = null;
    imgCtrl =
    (System.Web.UI.WebControls.Image)ItemsList.Findcontrol("ProductImage");

    HTH

    Ollie Riches
     
    Ollie Riches, Jan 27, 2005
    #2
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.