how to get the control ID embedded in a repeater?

D

Dan

Hi,

I have a repeater which contains an imagebutton like this:

<asp:Repeater ID="Repeater1" runat="server" DataSourceID="SqlDataSource1">
<ItemTemplate>
<asp:ImageButton ID="ImageButton1" runat="server" ImageUrl='<%#
Eval("myfield","mf\{0}") %>' " runat="server" />
</ItemTemplate>
</asp:Repeater>

I try to get its id in code-behind; i tried this ithout succes:

Dim img As ImageButton
img = Repeater1.FindControl("imagebutton1")

Thanks
Dan
 
N

Nathan Sokalski

First of all, you cannot get the individual IDs until the Repeater is
DataBound, so the best place to do what you are doing would probably be in
the ItemDataBound event.

protected void Repeater1_ItemDataBound(object sender, RepeaterItemEventArgs
e)

You would use something like the following:

img=(ImageButton)e.Item.FindControl("ImageButton1");
imgID=img.ClientId;

The ItemDataBound event is called once for each record in the DataSource.
That about as much as I can say with what you've given us, so hopefully you
can take it from here. Good Luck!
 
G

George

You must understand that when you put your control into Repeater or Grid
then that control will be repeated many time... once for each record in your
DataSource.

After you understand that you will easily understand that code
img = Repeater1.FindControl("imagebutton1")
does not make much sense. Since there are more than one ImageButton1

There are several solutions depending on what you want to do. If you want to
modify the image based on the data in a row then you need to subscribe to
ItemDataBound event.
That will get you access to particular row during binding. and then using
FindControl you will be able to find that ImageButton.


George.
 
D

Dan

thanks
George said:
You must understand that when you put your control into Repeater or Grid
then that control will be repeated many time... once for each record in
your DataSource.

After you understand that you will easily understand that code
img = Repeater1.FindControl("imagebutton1")
does not make much sense. Since there are more than one ImageButton1

There are several solutions depending on what you want to do. If you want
to modify the image based on the data in a row then you need to subscribe
to ItemDataBound event.
That will get you access to particular row during binding. and then using
FindControl you will be able to find that ImageButton.


George.
 

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

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,074
Latest member
StanleyFra

Latest Threads

Top