1

I have an asp:TextBox that looks like the following

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

and a Javascript function RefreshIt() that correctly fires each time I mouseout of the textbox.

I'm trying to have the mouseout event trigger the ontextchanged event of the asp:TextBox.

Various Stck Overflow posts have recommended the following techniques, which do not seem to work:

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

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

Any help would be appreciated.

3 Answers 3

4

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

Using this link, I was able to come up with something that produced the same results you are looking for. Not sure if this is okay for your application or not, but it works:

Aspx:

Changed the RefreshIt function to do a postback with an argument:

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

Code Behind:

Added 'IPostBackEventHandler' to the page and handled the 'RaisePostBackEvent' function:

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();
    }
}
Sign up to request clarification or add additional context in comments.

3 Comments

ontextchanged fires properly when the conditions are met to normally raise an ontextchanged event. I'm having trouble getting an onmouseout event to also raise (force) a ontextchanged event.
Okay, I see what you're saying. I made a simple project and did some testing. I updated my answer with my findings.
Thanks for the help. This got me moving in the right direction.
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>

Comments

0

Why dont you simply set the AutoPostBack property of the textbox to true and it will automatically postback each time the text is altered thereby firing the textchanged event !

Simple

2 Comments

It is not issue of "automatically postback". The question is about use java-script ontextchange event on asp:textbox..
Because that's not how OnTextChanged works. The control also needs to lose focus in order to fire the event.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.