This is a migrated thread and some comments may be shown as answers.

fix: asp:ImageButton onclick problem

6 Answers 124 Views
Grid
This is a migrated thread and some comments may be shown as answers.
muhammad
Top achievements
Rank 1
muhammad asked on 12 Oct 2008, 08:30 PM
i have a  telerik:GridTemplateColumn in grid in which i put a  <ItemTemplate> that contains <asp:ImageButton>. i want to change the image in the function onClick.

Compiler Error Message: CS0123: No overload for 'OnClick matches delegate 'System.Web.UI.ImageClickEventHandler', When i change the Onclick(sender, EventArgs) it runs ok, but i dont get the methods to change the image url of that templete column.


 

6 Answers, 1 is accepted

Sort by
0
Kevin Babcock
Top achievements
Rank 1
answered on 13 Oct 2008, 05:24 AM
Hello Muhammad,

I was able to use the GridTemplateColumn to add an ImageButton and handle its OnClick event in the example below.

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="_Default" %> 
 
<%@ Register assembly="Telerik.Web.UI" namespace="Telerik.Web.UI" tagprefix="telerik" %> 
 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
 
<html xmlns="http://www.w3.org/1999/xhtml" > 
<head runat="server"
    <title></title
</head> 
<body> 
    <form id="form1" runat="server"
        <telerik:RadScriptManager ID="RadScriptManager1" Runat="server" /> 
        <telerik:RadGrid ID="RadGrid1" runat="server"  
            DataSourceID="SqlDataSource1" 
            AutoGenerateColumns="false"
            <MasterTableView 
                DataSourceID="SqlDataSource1"
                <Columns> 
                    <telerik:GridTemplateColumn> 
                        <ItemTemplate> 
                            <asp:ImageButton ID="ImageButton1" runat="server" 
                                 OnClick="ImageButton1_Click" 
                                 ImageUrl='<%# Bind("ImageUrl") %>' /> 
                        </ItemTemplate> 
                    </telerik:GridTemplateColumn> 
                </Columns> 
            </MasterTableView> 
        </telerik:RadGrid> 
         
        <asp:SqlDataSource ID="SqlDataSource1" runat="server"  
            ConnectionString="<%$ ConnectionStrings:SqlConnectionString %>"  
            SelectCommand="SELECT * FROM [Images]"
        </asp:SqlDataSource> 
    </form> 
</body> 
</html> 
 

using System.Web.UI; 
using System.Web.UI.WebControls; 
 
public partial class _Default : System.Web.UI.Page 
 
    protected void ImageButton1_Click(object sender, ImageClickEventArgs e) 
    { 
        var imageButton = sender as ImageButton; 
        if (imageButton != null) 
        { 
            imageButton.ImageUrl = "~/Images/04.jpg"
        } 
    } 
 


I'm not sure what your problem might be (without seeing your code), but hopefully this example demonstrates how to implement the scenario you described. I hope this helps. If you have more questions, please let me know.

Sincerely,
Kevin Babcock
0
muhammad
Top achievements
Rank 1
answered on 15 Oct 2008, 08:29 AM
i want to trigger ImageButton1_Click on client side, either by

"onclick" event or by "onClientClick" events, and not on server side??
0
Shinu
Top achievements
Rank 1
answered on 15 Oct 2008, 01:01 PM
Hi Muhammad,

You can try the following code snippet to achieve the desired scenario.

ASPX:
<telerik:GridTemplateColumn    HeaderText="TempCol"  UniqueName="TempCol" > 
 <ItemTemplate> 
     <asp:ImageButton ID="ImageButton1" runat="server" ImageUrl="~/Images/pic4.gif"  /> 
 </ItemTemplate> 
</telerik:GridTemplateColumn> 

CS:
protected void RadGrid1_ItemDataBound(object sender, Telerik.Web.UI.GridItemEventArgs e) 
    { 
 
        if (e.Item is GridDataItem) 
        { 
            GridDataItem item = (GridDataItem)e.Item; 
            ImageButton imgbtn = (ImageButton)item["TempCol"].FindControl("ImageButton1"); 
            imgbtn.Attributes.Add("OnClick", "return Change('" + imgbtn .ClientID+ "');"); 
        } 
    } 

JS:
<script type="text/javascript" language="javascript" > 
function Change(Imagebtn) 
 var Imagebtn=document.getElementById(Imagebtn); 
 alert(Imagebtn) 
 Imagebtn.src="Images/pic2.gif" ; 
 return false; 
</script> 


Regards
Shinu.
0
muhammad
Top achievements
Rank 1
answered on 18 Oct 2008, 09:41 AM

i have not fixed the problem yet. The onClick function has some functionality according to my application, so i cant return the change function with onclick, i want to do that on onclientClick. imgbtn.ClientID as you told me in RadGrid1_ItemDataBound. why onclientclick is not firing??

the other option is to do the same in OnClientClick(sender, eventargs){} to change the image url. Is it possible to get the client id of image in this function. How??


the following is the source code i am using. plz do help me???


Each tel me how to bound the OnClientClick event with

protected
void OnClick(Object sender, ImageClickEventArgs e)

 

{

 

this.Response.Write( " some functionality click");

 

}



 

protected void RadGrid1_ItemDataBound(object sender, Telerik.Web.UI.GridItemEventArgs e)

 

{

 

if (e.Item is GridDataItem)

 

{

 

GridDataItem item = (GridDataItem)e.Item;

 

 

ImageButton imgbtn = (ImageButton)item["AlertIcon"].FindControl("AlertIcon1");

 

 

imgbtn.Attributes.Add("OnClientClick", "return Change('" + imgbtn.ClientID + "' );");

 


}}

 

<telerik:GridTemplateColumn

 

 

HeaderText="mybutton" >

 

 

<ItemTemplate>

 

 

<asp:ImageButton ID="mybutton1" runat="server" ImageUrl='<%# Eval("somevar") %>'

 

 

OnClick="OnClick" />

 

 

 

 

</ItemTemplate>

 

 

</telerik:GridTemplateColumn>

 

0
mano
Top achievements
Rank 1
answered on 19 Oct 2008, 09:30 PM

When the onclick is fired,

1) first the icon is changed
2) that row in which the icon is placed is deleted on next refresh.

how can i mark the row in onclick event?? so that i can delete it next it. plz help

0
muhammad
Top achievements
Rank 1
answered on 20 Oct 2008, 09:22 AM

When the onclick is fired, i want to add the following functionality.

1) first the icon is changed 
    as you told in imagebutton1.click event.

can you give me any clue to do the 2,

2) that row in which the icon is placed is deleted on next refresh.

how can i mark the row in onclick event?? so that i can delete it next it.

plz help

Tags
Grid
Asked by
muhammad
Top achievements
Rank 1
Answers by
Kevin Babcock
Top achievements
Rank 1
muhammad
Top achievements
Rank 1
Shinu
Top achievements
Rank 1
mano
Top achievements
Rank 1
Share this question
or