简体   繁体   中英

Asp.Net OnClick perform through javascript

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

Try this:

var btn = document.getElementById("<%=Button1.ClientID%>");
if (btn){
    btn.click();
}

我会尝试这样的:

<input type="button" id="mybutton" onclick="document.getElementById('<%= Button1.ClientID %>_input').click();">

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() %>" />

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM