Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I have a normal image for delete and an Asp.Net button. If I click the image which is inside the javascript I need to make the Asp.Net button click and perform it's operation.

Is there any way to do that from the client side: Here is my normal Html button:

This is my Asp.Net button:

<asp:Button ID="Button1" runat="server" Text="Button" />

This is my code behind:

  Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
                //Do something//
  End Sub
share|improve this question
why do you need both the buttons? can you remove asp button? and use any ajax library to call the server side code when HTML button is clicked? – xgencoder Nov 10 '11 at 18:21
I dont understand why you have two. Create the button in design view and double click it. It will generate the code for the button. – javasocute Nov 10 '11 at 18:22
Actually I don't need an Html button and instead of that I use Image which I must use in javascript.And If I click that Image I make the buton automatically click and able to perform its operartion. – coder Nov 10 '11 at 18:23
@javasocute I knew that it's creates a codebehind for the button but I have an image instead of button in the javascript and when t is clicked I need to make the Asp.Net button click. – coder Nov 10 '11 at 18:25

3 Answers

up vote 1 down vote accepted

Try this:

var btn = document.getElementById("<%=Button1.ClientID%>");
if (btn){
    btn.click();
}
share|improve this answer
Thanks James it worked very well! – coder Nov 10 '11 at 18:36
You're welcome. Glad it worked :) – James Johnson Nov 10 '11 at 18:39

I'd try this:

<input type="button" id="mybutton" onclick="document.getElementById('<%= Button1.ClientID %>_input').click();">
share|improve this answer
Thanks KmKemp it also worked! – coder Nov 10 '11 at 18:37

I think the most proper way would be to do this:

In your code-behind:

Protected ReadOnly Property ButtonClickScript() As String
    Get
        Return Page.ClientScript.GetPostBackEventReference(Button1, "")
    End Get
End Property

Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click

End Sub

In aspx:

<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Button" />
<img onclick="<%=ButtonClickScript() %>" />
share|improve this answer
Thanks kevev22 it woks pretty good! – coder Nov 10 '11 at 18:39

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.