0
I have this code in Aspx:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Index.aspx.cs" Inherits="WebSite.View.Index" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<script src="Scripts/jquery-3.4.1.min.js"></script>
<title>Projeto para Testes</title>
</head>
<body>
<form id="form1" runat="server">
<div id="dvEventos" class="dvEventos">
<h1>Apenas um teste</h1>
<asp:Button ID="btnEventoCsharp" CssClass="btnEventoCsharp" Text="Evento C#" OnClick="btnEventoCsharp_Click" runat="server" />
<asp:Button ID="btnEventoCsharpByJS" CssClass="btnEventoCsharpByJS" Text="Evento C# by JS" OnClick="btnEventoCsharpByJS_Click" runat="server" />
<button id="btnEventoJqueryClick" class="btnEventoJqueryClick">Evento C# by Jquery</button>
<button id="btnEventoJquery" class="btnEventoJquery">Evento Jquery</button>
</div>
</form>
</body>
<script>
$(document).ready(function () {
CarregaEventosIndex();
});
function CarregaEventosIndex() {
$('.btnEventoJquery').off('click').on('click', function () {
alert('Event click by Jquery');
});
$('.btnEventoJqueryClick').off('click').on('click', function () {
$("input[id*=btnEventoCsharpByJS]").click();
});
}
</script>
</html>
The file of mine Codebehind:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace WebSite.View
{
public partial class Index : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void MetodoSemParametroChamadoEventosDiferentes()
{
string passeiAqui = "sem parametro";
string evento = passeiAqui;
}
/// Evento de Teste
protected void btnEventoCsharpByJS_Click(object sender, EventArgs e)
{
MetodoSemParametroChamadoEventosDiferentes();
}
}
}
I am using Webforms, and I need to call a Codebehind event by jQuery, in case, using this button btnEventoJqueryClick
. But there’s a catch, if I use that code $("input[id*=btnEventoCsharpByJS]").click();
through the browser console, using breakpoint in Codebehind realize that the event is called, but clicking the button is not being called the event. What am I doing wrong? Or, how can I call a Codebehind event by jQuery differently?
Looking at the browser console, does any "network" request appear? I created an example here, exactly how you put it there and this working.
– Bruno Warmling
To run anything that is on the server side by the browser, you will need to make a request for it, a GET or POST for example.
– Leandro Angelo
Helped you to solve?
– novic
I managed using another solution
– Braian Freitas