Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>As far as I can tell, there are two ways to accomplish what you are looking to do. Either use an <code>asp:ImageButton</code> server control and wire up the <code>onclick</code> client event using the <code>OnItemDataBound</code> event, or simply use an <code>&lt;input type="image" /&gt;</code> control and wire up the <code>ClientID</code> inline. The following example shows both approaches:</p> <pre><code>&lt;%@ Page Language="C#" AutoEventWireup="true" %&gt; &lt;!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt; &lt;html xmlns="http://www.w3.org/1999/xhtml"&gt; &lt;head runat="server"&gt;&lt;title&gt;OnClick Test&lt;/title&gt;&lt;/head&gt; &lt;body&gt; &lt;form id="form1" runat="server"&gt; &lt;div&gt; &lt;asp:ListView ID="lv1" OnItemDataBound="lv1_ItemDataBound" runat="server"&gt; &lt;ItemTemplate&gt; &lt;asp:Label ID="label1" Text="&lt;%# Container.DataItem %&gt;" runat="server" /&gt; &lt;asp:ImageButton ID="btn1" ImageUrl="myimage.jpg" AlternateText="Show Text" runat="server" /&gt; &lt;input type="image" src="myimage.jpg" alt="Show Text" onclick="alert(document.getElementById('&lt;%# Container.FindControl("label1").ClientID %&gt;').innerText);" /&gt; &lt;br /&gt; &lt;/ItemTemplate&gt; &lt;/asp:ListView&gt; &lt;/div&gt; &lt;/form&gt; &lt;/body&gt; &lt;/html&gt; &lt;script runat="server"&gt; public void Page_Load(object sender, EventArgs e) { if (Page.IsPostBack) return; lv1.DataSource = new[] {"Manny", "Moe", "Jack"}; lv1.DataBind(); } protected void lv1_ItemDataBound(object sender, ListViewItemEventArgs e) { var label1 = e.Item.FindControl("label1") as Label; var btn1 = e.Item.FindControl("btn1") as ImageButton; if (label1 == null || btn1 == null) return; btn1.Attributes.Add("onclick", "alert(document.getElementById('" + label1.ClientID + "').innerText);"); } &lt;/script&gt; </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload