'generate onclick event in image control in asp.net

i am have one datalist in asp.net

i m using vb with asp

now my code is just as follow

<table border="0" cellpadding="0" cellspacing="0" >

<tr>

    <br />

   <td>

   Question Number:

        <asp:Label ID="Label3" runat="server" Text='<%# Eval("Question_no") %>' />

        <br />

   Cust_id:

        <asp:Label ID="Cust_idLabel" runat="server" Text='<%# Eval("Cust_id") %>' />

        <br />

        Question:

        <asp:Label ID="Label1" runat="server" Text='<%# Eval("Question") %>' />

        <br />

        Time:

        <asp:Label ID="Label2" runat="server" Text='<%# Eval("Date_time") %>' />

        <asp:ImageButton ID="ImageButton1" runat="server" ImageUrl="~/ImagesFolde/Chrysanthemum.jpg" Height="50" Width="50" **OnClientClick="s()"**/>

all this in datalist as you can see

so it will be repeated..

i have put one image in it

now i want call a method when any image will be clicked

i make one sub called s() in vb

i want to call it

i use on clientclick() event but doesn't work

what to do?



Solution 1:[1]

you use onclientclick - have you made your functions with javascript/jquery ? ( the server side won't be fired)

for instance : if you call you client method like this :

OnClientClick="return myFunction();"

then your function should be in the header as follows:

<script type=text/javascript>
   function myFunction()
   {
       alert("activated here");
   }
</script>

Solution 2:[2]

Suppose you have a datalist:

ASPX:

<asp:DataList id="ItemsList" OnItemCommand="Item_Command" runat="server">
   <ItemTemplate>
      <asp:LinkButton id="SelectButton" Text="Select" CommandName="ModifyRow" 
      runat="server" CommandArgument='<%#Eval("Id")%>' />
   </ItemTemplate>
  </asp:DataList>

VB (Code Behind) : Here I am taking the id from '<%#Eval("Id")%>' and binding some products in bulleted list:

Protected Sub ItemsList_ItemCommand _
    (source As Object, e As RepeaterCommandEventArgs) _
    Handles Categories.ItemCommand
    If e.CommandName = "ModifyRow" Then
        ' Determine the CategoryID
        Dim categoryID As Integer = Convert.ToInt32(e.CommandArgument)
        ' Get the associated products from the ProudctsBLL and
        ' bind them to the BulletedList
        Dim products As BulletedList = _
            CType(e.Item.FindControl("ProductsInCategory"), BulletedList)
        Dim productsAPI As New ProductsBLL()
        products.DataSource = productsAPI.GetProductsByCategoryID(categoryID)
        products.DataBind()
    End If
End Sub

Solution 3:[3]

<asp:ImageButton  ID="imgexpand" runat="server" ImageAlign="Bottom" ImageUrl="~/img/plus.png" OnClick="s" />

you do not need the () in the control onclick statement this will fire the sub s() procedure in the vb code

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2
Solution 3 TheConstructor