i want to apply onclick event on a image in the repeater.
i don't know how to do it.
anybody having any idea plz guide me.
thanks in advance

Dani AI

Generated

Two practical options that follow ’s ItemDataBound idea: a server-side click (useful when you need to run code on postback) or a client-side JavaScript handler (faster UX, no postback). Pick the approach that matches your need.

Server-side (use this when you must run code in code‑behind): put an ImageButton inside the Repeater and handle the Repeater's ItemCommand. Example markup and handler:

<asp:Repeater ID="rptThumbs" runat="server" OnItemCommand="rptThumbs_ItemCommand">
  <ItemTemplate>
    <asp:ImageButton ID="imgBtn" runat="server"
      ImageUrl='<%# Eval("ThumbnailUrl") %>'
      CommandName="ThumbClick"
      CommandArgument='<%# Eval("ItemId") %>' />
  </ItemTemplate>
</asp:Repeater>
protected void rptThumbs_ItemCommand(object sender, RepeaterCommandEventArgs e)
{
    if (e.CommandName == "ThumbClick")
    {
        string id = e.CommandArgument.ToString();
        // server-side processing here
    }
}

Client-side (no postback): call a JS function from the rendered IMG. This is handy for previews or client UI actions. Example JavaScript and markup:

<script type="text/javascript">
function preview(url) {
  window.open(url, 'preview', 'width=600,height=420');
  return false;
}
</script>

<!-- inside ItemTemplate -->
<img src='<%# Eval("ThumbnailUrl") %>' onclick="return preview('<%# Eval("ThumbnailUrl") %>')" />

Troubleshooting notes: ensure controls you reference have runat="server" when you need server access; use ImageButton or LinkButton for server clicks (asp:Image has no Click event); check ItemType in ItemDataBound (Item and AlternatingItem); guard against null from FindControl; and always JavaScript-encode any data you inline into JS to avoid breaking the script. This complements ’s reply and ties into ’s ItemDataBound work—choose the pattern above that fits whether you want a client action or a server postback.

Recommended Answers

All 4 Replies

You should define image in repeater itemdatabound.
And then you can give events to image.

In repeater you can define like
HtmlImage img = e.item.FindControl("img") as HtmlImage;
or
Image img = e.item.FindControl("img") as Image;

thanks for your help
i have done it like this

 protected void rptthumbimg_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.Item)
        {
            DataRowView drv = (DataRowView)e.Item.DataItem;
            dt = obj.searchimg(4, imgcatid);
            if (dt != null)
            {
                Image imgmem = (Image)e.Item.FindControl("imgthumb");
                if (dt.Rows.Count > 0)
                {
                    if (dt.Rows[0]["thumbnail"].ToString() == "")
                    {
                        imgmem.ImageUrl = impplaces + "memimg.png";
                    }
                    else
                    {
                        imgmem.ImageUrl = impplaces + dt.Rows[0]["thumbnail"].ToString();
                    }
                }
            }
        }
    }

i want to implement onclick event on this image and i dont know hoe to do it.

imgmem.Attributes.Add("onclick", "eventname");

thank you very much for all ur help.it was really helpfull to me.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.