Executing an OnClick event in Asp.Net using JavaScript

I am working on a project that involves a normal image for delete and an Asp.Net button. The requirement is that when the user clicks on the image which is located inside the JavaScript code, I need to trigger a click event on the Asp.Net button so that it can perform its operation.

Is there any way to achieve this functionality from the client side? Here is the HTML representation of my normal button:

This is how my Asp.Net button looks like:

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

Below is the code behind for handling the button click event:

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

Answer №1

Give this a shot:

const submitBtn = document.getElementById("btnSubmit");
if (submitBtn){
    submitBtn.click();
}

Answer №2

Here's an alternative approach:

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

Answer №3

To achieve this, the following approach can be taken:

Within the backend code:

Protected ReadOnly Property ButtonClickAction() 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 the aspx file:

<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Button" />
<img onclick="<%=ButtonClickAction() %>" />

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Guide to running a NextJS app alongside an Express server backend on the same localhost port

I'm working on hosting my NextJS app, which is built with React, on the same localhost port as my express api-server backend. Within my express server API settings, I have configured my API server to listen on: http://localhost:3000/graphql How can ...

Angular 7+ boasts the exciting feature of integrating Promises within Observables

I'm faced with a challenging issue that I can't seem to resolve on my own. During the ngOnInit event, I'm monitoring the URL parameter in search of a specific folder within firebase-storage. Upon retrieval, I compile a list of folders and f ...

The supported browser is unable to import ES6 modules

Attempting to incorporate moment.js as an es6 module. The most recent version of Chrome is being utilized. Referring to the conversation here, I experimented with the src path (es6) import * as moment from './node_modules/moment/src/moment' A ...

As you scroll, this smooth marquee effect gently shimmers with each movement

I've been trying out this code to create a scrolling marquee text, but the issue is that after 7000 milliseconds it starts to jitter, which doesn't make the text look good. Any suggestions on how I can fix this problem? Let me know! <script ...

JavaScript data structures used to hold all modified input fields within a document

I am currently working on a project where I have a dynamically generated document using Odoo. The document consists of multiple divs, each containing different types of input boxes such as checkboxes, text fields, and numbers. Currently, every time a user ...

How to incorporate click and drag feature for rotating a 3D object using vue.js

Whenever I click a button, the rotation function is activated and allows me to rotate an object by hovering over it with my mouse. Now, I want to modify this behavior so that the object rotates only when I click on it and move the mouse (while holding dow ...

Discover ways to retrieve an ajax response from a different domain by submitting specific data to the controller method while operating on an internet server

I am facing an issue where I am unable to retrieve array data from a Codeigniter controller using an Ajax request. The data is being posted to the controller to fetch related data from the database, and it works perfectly fine on my local server. However, ...

Top method for effectively handling transactions using Promise

I'm in the process of developing a utility class for NodeJs to streamline database transactions handling. My plan is to implement a method similar to this: transactionBlock(params) { let _db; return mySqlConnector.getConnection(params.db) ...

How can you spot the conclusion of different lines that refuse to remain in place?

Currently, I am facing a jquery issue that has left me stumped. The website I am working on is structured as follows: <div class="Header"></div> <div class="main"><?php include('content.html'); ?></div> <div clas ...

The 'click' event is not triggering after adding elements to the DOM using AJAX

$(".btn-close").on('click', function () { alert('click'); var win = $(this).closest("div.window"); var winID = win.attr("id"); $(win).find("*").each(function () { var timerid = $(this).attr("data-timer-id"); ...

Update the page once the Ajax call is completed

On my website, I have a setup with two pages. The first page allows users to select an item using a <select> element. Upon selecting an item, a form is shown automatically via an AJAX call. This form pulls data from a MySQL database and displays it ...

Surprising sequencing in Sequelize's INSERT and UPDATE queries

Initially, I will provide a brief explanation of my objective and the related models. To start, with an array containing trackingIds (10 elements), generate a Chromosome using each trackingId along with a "free" Palette Key considerations: Project.hasMa ...

"Unlocking the Next Slide: Activating the Next Button in Google Slides using

I am currently facing a challenge with my Google Docs presentations, as I am attempting to use JavaScript to trigger the next slide. To address this issue, I am experimenting with creating a web server on my local network that can be accessed from my phone ...

Exploring ways to share the current URL from Next.js on social media platforms

For the purpose of generating URLs, I have developed a helper function as shown below: function generateUrl(platform: string) { const currentUrl = typeof window !== 'undefined' ? window.location.href : '' switch (platform) { c ...

The error message "Cannot read property '$scope' of undefined" indicates that there is an issue

After receiving HTML content in an Angular app, I inject it into the HTML document but struggle to retrieve it. For example, you can see a minimized code on plunker here and in JavaScript here Below is my controller: class ReadCtrl constructor: (@$sco ...

An object is not defined in the following scenario

I currently have a custom function that includes the following code snippet: 1: var object = get_resource($scope, CbgenRestangular, $stateParams.scheme_id); 2: console.log(object) This function triggers the following code: get_resource = function ($sc ...

What is the best way to implement callback functionality within a UI dialog?

I am currently working with two pages: main.aspx and register.aspx. When the register page is loaded within a UI dialog on main.aspx, I encounter issues with callback functions. Main.aspx seems to utilize the register's callback and raisecallback fun ...

Adjust text at multiple positions using Javascript

I am working on parsing a complex query and developing a tool using the following sample: var str =" SELECT 'Capital Return' AS Portfolio_Classification , '(Gross)' AS Portfolio_Type , '0.21%& ...

Sanitizing computed properties in VueJS

Would it be considered safe to add a URL in Vue through a computed property when the link is sourced from an external API service? For instance: <img :src="imgURL"> VueJS computed: { imgURL(){ return `https://exampleur.com${poster.4by4 ...

ASP.NET Core MVC - Storing data permanently in a Dictionary within an Action

Currently, I am developing an ASP.NET Core MVC web application. Within my Model, there is a Dictionary that I manipulate by adding new elements in one Action and then attempting to access them in subsequent actions. However, after the initial action is exe ...