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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11217468/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 17:00:51  来源:igfitidea点击:

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

javascriptc#ajaxevent-handlingdom-events

提问by xelco52

I have an asp:TextBoxthat looks like the following

我有一个asp:TextBox看起来像下面的

<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 mouseoutof the textbox.

以及RefreshIt()每次鼠标移出文本框时都会正确触发的 Javascript 函数。

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

我想有mouseout事件触发ontextchanged的事件asp:TextBox

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

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

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

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

Any help would be appreciated.

任何帮助,将不胜感激。

采纳答案by tehDorf

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

参见: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:

ASP:

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

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

Code Behind:

背后的代码:

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

向页面添加了“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();
    }
}

回答by Rizwi

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 !

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

Simple

简单的