Controls in DataList

  • Thread starter Thread starter David
  • Start date Start date
D

David

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

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. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top