Fire ontextchanged()事件的一个asp:TextBox通过javascript [英] Fire ontextchanged() event of an asp:TextBox via javascript

查看:128
本文介绍了Fire ontextchanged()事件的一个asp:TextBox通过javascript的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 asp:TextBox ,看起来像以下

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

和一个javascript函数 RefreshIt()正确地在文本框的 mouseout 之间触发。

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

我试图让mouseout事件触发 asp的 ontextchanged 事件:文本框

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

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

Various SO posts have recommended the following techniques, which do not seem to work.

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

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

任何帮助将不胜感激。

推荐答案

请参阅: 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:

RefreshIt函数用参数进行回发:

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>



代码背后:



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

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();
    }
}

这篇关于Fire ontextchanged()事件的一个asp:TextBox通过javascript的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆