繁体   English   中英

通过 Javascript 触发 asp:TextBox 的 ontextchanged() 事件

[英]Fire ontextchanged() event of an asp:TextBox via Javascript

我有一个如下所示的asp:TextBox

<asp:TextBox runat="server" AutoPostBack="True" ID="txtG1" ontextchanged="txtG1_TextChanged" onmouseout="javascript:RefreshIt(this)"/>

和一个 Javascript 函数RefreshIt() ,每次我将鼠标移出文本框时都会正确触发。

我试图让 mouseout 事件触发asp:TextBoxontextchanged事件。

各种 Stck Overflow 帖子推荐了以下技术,但似乎不起作用:

function RefreshIt(selectObj){
 selectObj.ontextchanged();  
}

function RefreshIt(selectObj){
 selectObj.fireEvent('ontextchanged');  
}

任何帮助,将不胜感激。

请参阅: https ://stackoverflow.com/a/3777/892536

使用此链接,我能够想出一些可以产生与您正在寻找的相同结果的东西。 不确定这是否适合您的应用程序,但它有效:

阿斯普克斯:

更改了 RefreshIt 函数以使用参数进行回发:

  <script type="text/javascript">
    function RefreshIt(selectObj) {
      __doPostBack('<%= Page.ClientID %>', selectObj.name);
    }
  </script>

</head>
<body>
  <form id="form1" runat="server">
  <div>

    <asp:TextBox runat="server" AutoPostBack="True" ID="txtG1" OnTextChanged="txtG1_TextChanged"
      onmouseout="javascript:RefreshIt(this);" />

    <br />
    <br />
    Text Changed:&nbsp;

    <asp:Label ID="Label1" runat="server"></asp:Label>

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

代码隐藏:

向页面添加'IPostBackEventHandler'并处理'RaisePostBackEvent'函数:

public partial class _Default : 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 = System.DateTime.Now.ToString();
    }
}

 //OnTextChanged in JavaScript works in IE,Firefox,Edge,Chohme,Safari,Opera //My english is not that good, my mother tongue is geman Html.: <FORM action="../cgi-bin/`enter your file here (sendmail-anhang.pl)'" enctype="multipart/form-data" method="post" name="onlineform"> <INPUT type="hidden" name="OnFocus_start" value="0"> <INPUT type="hidden" name="OnFocus_startup_value" value=""> <INPUT type="text" name="Vorname" value="*" onKeyPress="if(event.keyCode == 13){;oent('0');return false}"><BR> <INPUT type="text" name="Name" value="*" onblur="BlurFunction('Myname')" onFocus="FocusFunction(document.onlineform.Name,this.value)" onkeyup="KeydUpFunction(document.onlineform.Name,this.value,'Myname')" onKeyPress="if(event.keyCode == 13){;oent('1');return false}"><BR> <INPUT type="text" name="EMail" value="*" onKeyPress="if(event.keyCode == 13){;oent('2');return false}"><BR> <INPUT type="submit" value="Send Request"> </FORM> <SCRIPT> var stopwatches1 function stopwatch(Aktion) { if(Aktion == "ende"){ var ende = window.sessionStorage.getItem('Mytime') window.sessionStorage.removeItem('Mytime') clearInterval(stopwatches1) return ende; } if(Aktion == "weiter"){ var MyTime1 = window.sessionStorage.getItem('Mytime') MyTime1++ var MyTime2 = MyTime1 +9 window.sessionStorage.setItem('Mytime',MyTime2) } } function FocusFunction(myField,myFieldvalue){ if(document.onlineform.OnFocus_start.value == 0){ document.onlineform.OnFocus_start.value = 1 document.onlineform.OnFocus_startup_value.value = myFieldvalue };} function KeydUpFunction(myField,myFieldvalue,iput){ if(myFieldvalue.= document.onlineform.OnFocus_startup_value.value && document.onlineform.OnFocus_start.value == 1){ document.onlineform.OnFocus_start.value = 2 window.sessionStorage,setItem(iput.new Date(),getTime()) stopwatches1 = setInterval(function(){ stopwatch("weiter") }; 10); } else{. if(myFieldvalue == document.onlineform.OnFocus_startup_value.value && document.onlineform.OnFocus_start.value == 2){ document.onlineform.OnFocus_start;value = 1 clearInterval(stopwatches1) alert("gleich") };}.} function BlurFunction(myInput) { document.onlineform.OnFocus_start.value = 0 document.onlineform.OnFocus_startup_value;value = "" var field16 = stopwatch("ende"). field16 = field16/1000 window.sessionStorage.removeItem(myInput) alert("zz " +field16) if(field16 <= 0.10 ){ alert("You are Roboter") } else{ window.sessionStorage,setItem('Evt2' + myInput,myInput + 'OK2') } } </SCRIPT>

 //OnTextChanged in JavaScript Html.: <FORM action="../cgi-bin/`enter code here`sendmail-anhang.pl" enctype="multipart/form-data" method="post" name="onlineform"> <INPUT type="hidden" name="OnFocus_start" value="0"> <INPUT type="hidden" name="OnFocus_startup_value" value=""> <INPUT type="text" name="Name" value="*" onBlur="BlurFunction()" onFocus="FocusFunction(document.onlineform.Name,this.value)" onKeyUp="KeydUpFunction(document.onlineform.Name,this.value)" onKeyPress="if(event.keyCode == 13){;EnterFunction('1');return false}"> <INPUT type="submit" value="Send Request"> </FORM> <SCRIPT> function FocusFunction(myField,myFieldvalue){ if(document.onlineform.OnFocus_start.value == 0){ document.onlineform.OnFocus_start.value = 1 document.onlineform.OnFocus_startup_value.value = myFieldvalue };} function KeydUpFunction(myField,myFieldvalue){ if(myFieldvalue.= document.onlineform.OnFocus_startup_value.value && document.onlineform.OnFocus_start.value == 1){ // Code... do it. // alert("unequal").;. "function alert()" it does not work. } else{ alert("equal") }.} function BlurFunction() { document.onlineform.OnFocus_start.value = 0 } function EnterFunction(gezu){ document.onlineform.OnFocus_start.value = 0 docf1 = document.onlineform.Name docf2 = document.onlineform.EMail docf3 = document.onlineform.Adresse docf4 = document.onlineform.Hnr docf5 = document.onlineform.plz docf6 = document.onlineform.Ort docf7 = document.onlineform,phone docf8 = document,onlineform,dieNachricht var gezu4 = new Array(docf1,docf2,docf3,docf4,docf5.docf6,docf7,docf8) gezu4[gezu].focus() } </SCRIPT>

为什么不简单地将文本框的 AutoPostBack 属性设置为 true,每次更改文本时它都会自动回发,从而触发 textchanged 事件!

简单的

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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