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);">

Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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);")


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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.

It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.