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

时间:2012-06-26 23:41:23

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

我有asp:TextBox,如下所示

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

以及每次 mouseout 文本框时正确触发的javascript函数RefreshIt()

我正在尝试让mouseout事件触发ontextchanged的{​​{1}}事件。

各种SO帖子推荐了以下技术,这些技术似乎不起作用。

asp:TextBox

任何帮助都将不胜感激。

3 个答案:

答案 0 :(得分:4)

请参阅: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();
    }
}

答案 1 :(得分:1)

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

答案 2 :(得分:0)

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

简单