【问题标题】:Fire ontextchanged() event of an asp:TextBox via Javascript通过 Javascript 触发 asp:TextBox 的 ontextchanged() 事件
【发布时间】:2012-06-26 23:41:23
【问题描述】:

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

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

和一个 Javascript 函数 RefreshIt() 每次我 mouseout 文本框时都会正确触发。

我正在尝试让 mouseout 事件触发 asp:TextBoxontextchanged 事件。

各种 Stck 溢出帖子都推荐了以下技术,这些技术似乎不起作用:

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

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

任何帮助将不胜感激。

【问题讨论】:

    标签: javascript c# ajax event-handling dom-events


    【解决方案1】:

    见:https://stackoverflow.com/a/3777/892536

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

    aspx:

    将 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 在满足正常引发ontextchanged 事件的条件时正确触发。我无法让 onmouseout 事件同时引发(强制)ontextchanged 事件。
    • 好的,我明白你在说什么。我做了一个简单的项目并做了一些测试。我用我的发现更新了我的答案。
    • 感谢您的帮助。这让我朝着正确的方向前进。
    【解决方案2】:

    //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>

    【讨论】:

      【解决方案3】:

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

      简单

      【讨论】:

      • 不是“自动回发”的问题。问题是关于在 asp:textbox.. 上使用 java-script ontextchange 事件
      • 因为这不是 OnTextChanged 的​​工作方式。控件还需要失去焦点才能触发事件。
      猜你喜欢
      • 2015-07-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-11-03
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多