how to get the control ID embedded in a repeater?

  • Thread starter Thread starter Dan
  • Start date Start date
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
 
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!
 
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.
 
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.
 
Back
Top