• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 5221
  • Last Modified:

When adding "onclick" attribute to input type image on aspx file, code in .cs file is skipped

I am trying to add the "onclick" attribute to an input type of image. This input control is also bound to the following code in the .cs file:
private void SubmitBtn_ServerClick(object sender, ImageClickEventArgs e)

without the "onclick" attribute, the code behind on the .cs file is called (and everything works fine). But when I add the following:
onclick="return sendOptIns(this);"
the code behind for this image control is no longer called. Instead, only the script in the "OnClick" attribute is called, and the page is refreshed.

---- Here is the code without the "onclick" attribute:
<form id="Form1" runat="server">
<input type="image" runat="server" id="SubmitBtn" src="/images/button_next.gif" width="109" height="18">

---- Here is the code with the "onclick" attribute:
<form id="Form1" runat="server">
<input id=btnSubmit type=submit runat=server value="Complete" onclick="return sendOptIns(this);">

1 Solution
It looks to me like you are missing this onserverclick="SubmitBtn_ServerClick" in your input tag.
onserverclick will fire the SubmitBtn_ServerClick event handler when the page is posted back to the server.
onclick on the other hand will run a JavaScript function on the client.

That's how it work in ASP .Net 2.0 anyway not sure what 1.x does.
<%@ Page Language="C#" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
    protected void SubmitBtn_ServerClick(object sender, ImageClickEventArgs e)
        Response.Write("Hello !!!");
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Untitled Page</title>
    <script type="text/javascript">
        function sendOptIns(x)
            alert("Hello !!!");
            return true;
    <form id="form1" runat="server">
    <input type="image"   runat="server" id="SubmitBtn" src="http://www.mikeutterback.net/ImageHandler.ashx?text=text" 
    onclick="return sendOptIns(this)"
    onserverclick="SubmitBtn_ServerClick" />

Open in new window

>><input type="image" runat="server" id="SubmitBtn"
this is  a image and
>><input id=btnSubmit type=submit runat=server
this is a submit button .. but i think you mentioned above that you were using the same control, one with the client client click event and the other without that event ..

Can i ask you why you have a HTML control with runat = server instead of using a <asp:Imagebutton .. control .. you can add clientside event to that control in code behind in page load using
controlname.attributes.add("onclick", "return sendOptIns(this);")

If one of the fields on the form set to be autopostback then the __dopostback
function and __EVENTTARGET, __EVENTARGUMENT fields embedded into the
html code. In order to manually set postback you need to add those two fields to
the form and the __DoPostBack function. Without the __EVENTTARGET and
__EVENTARGUMENT fields the asp.net page wont know which control call
for the postback and for part of the controls which event to raise.


Featured Post

The 14th Annual Expert Award Winners

The results are in! Meet the top members of our 2017 Expert Awards. Congratulations to all who qualified!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now