HOWTO · JavaScript

Fire Ontextchanged Event de un Asp: TextBox a través de Javascript

¿Cómo activar el evento de cambio de texto de un asp: TextBox a través de Javascript?

En esta página

Bueno, si no quiere hacerlo con Ajax o cualquier otra cosa y disfrutar de la devolución de datos normal de ASP.NET, puede hacer esto (sin usar ninguna otra biblioteca):

En el archivo de código, si usa C# y .NET 2.0 o superior, agregue la siguiente interfaz a la clase Page, para que se vea así:

public partial class Default : System.Web.UI.Page, IPostBackEventHandler {}

Luego, debe agregar esta función a su archivo de código.

public void RaisePostBackEvent(string eventArgument) {}

Escriba el siguiente código en su evento onclick en JavaScript.

var pageId = '<%=  Page.ClientID %>';
__doPostBack(pageId, argumentString);

Luego, el archivo de código llama al método RaisePostBackEvent con el argumento eventArgument, como el argumento String pasado por JavaScript. Ahora puede llamar a cualquier otro evento que desee.

Aquí está la posible solución al problema.

WebForm1.aspx

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="textbox.WebForm1" %>

<!DOCTYPE html>
<html xmlns="https://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>

   <script type="text/javascript">
    function RefreshIt(selectObj) {
      __doPostBack('<%= Page.ClientID %>', selectObj.name);
    }
   </script>
<body>
  <form id="form1" runat="server">
  <div>
     <asp:TextBox runat="server" AutoPostBack="True" ID="txtG1" OnTextChanged="txtG1_TextChanged"
      onmouseout="javascript:RefreshIt(this);" ></asp:TextBox>
    <br />
    &nbsp;<asp:Label ID="Label1" runat="server" Font-Size="Large"></asp:Label>

  </div>
  </form>
</body>
</html>

Cuando ejecute el código en el navegador, obtendrá un resultado como este:

Fire Ontextchanged Event de un Asp: TextBox a través de Javascript

cuando ingresa el texto en el campo de entrada, activará el evento ontextchange y dará una salida (es decir, en color rojo) como esta:

Activar evento Ontextchanged de un Asp: TextBox a través de Javascript - evento ontextchange

Puede cambiar la función RefreshIt a devolución de datos como argumento.

Es posible que necesite conocer el código que funciona detrás y agregar IPostBackEventHandler a la página y manejar la función RaisePostBackEvent.

WebForm1.aspx.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace textbox {
  public partial class WebForm1 : System.Web.UI.Page, IPostBackEventHandler {
    protected void Page_Load(object sender, EventArgs e) {}

    public void RaisePostBackEvent(string Arg) {
      if (txtG1.ID == Arg)
        txtG1_TextChanged(txtG1, null);
    }

    protected void txtG1_TextChanged(object sender, EventArgs e) {
      Label1.Text = txtG1.Text;
      Label1.ForeColor = System.Drawing.Color.Red;
      Label1.BackColor = System.Drawing.Color.White;
    }
  }
}

Puede usar el siguiente código mencionado para activar el evento ontextchanged() de asp:TextBox a través de JavaScript.