使用__doPostBack从JavaScript调用ASP.NET TextChanged事件 [英] Invoke ASP.NET TextChanged event from JavaScript using __doPostBack

查看:131
本文介绍了使用__doPostBack从JavaScript调用ASP.NET TextChanged事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

像许多其他人一样,我正在尝试从JavaScript调用.NET控件的服务器端事件。

Like many others, I'm trying to invoke a .NET control's server-side event from JavaScript.

具体来说,我要解雇 TextChanged 名为 txtSearch 的TextBox上的事件。因此,我希望从客户端到达以下事件:

Specifically, I want to fire the TextChanged event on a TextBox named txtSearch. Therefore, I'm looking to reach the following event from client-side:

protected void txtSearch_TextChanged(object sender, EventArgs e)

在SO上阅读了很多答案(例如 here 这里)我有以下JavaScript:

Having read many answers on SO (for example here and here) I have the following JavaScript:

__doPostBack('ctl00$ctl00$Container$Main$txtSearch', 'TextChanged');

但是服务器端事件永远不会触发。

But the server-side event never fires.

我尝试了很多排列:AutoPostBack为true和false,在ASPX( OnTextChanged =)的服务器端指令中声明和不使用EventValidation在页面声明中关闭,使用ClientID而不是EVENTTARGET参数中的UniqueID ...但事件仍然永远不会被触发。

I've tried numerous permutations: with the AutoPostBack true and false, with and without the event declared in the server-side instructions on the ASPX (i.e. OnTextChanged=""), with the EventValidation turned off in the page declaration, using the ClientID rather than the UniqueID in the EVENTTARGET parameter... but the event is still never fired.

其他几点


  • txtSearch 按钮控件也是UpdatePanel的触发器,如果​​重要的话。

  • 我正在转换现有的代码,其中有很多代码,我正在寻找可以放到每个页面上的东西,而不是将代码隐藏事件转换为PageMethods。

  • the txtSearch button control is also the trigger for an UpdatePanel, in case that matters.
  • I'm converting existing code, of which there's quite a lot, and am looking for something I can drop onto each page rather than converting the code-behind events to PageMethods.

谁能告诉我还需要做什么?

Can anyone tell me what more I need to do?

推荐答案

我试过这个,它对我有用:

I've tried this, and it works for me:

<asp:TextBox runat="server" ID="txtSearch" OnTextChanged="txtSearch_TextChanged"></asp:TextBox>
<input type="button" value="submit" onclick="<%= GetOnChangedScript() %>" />

服务器端 asp:TextBox ,以及客户端输入点击后会触发 __ doPostBack __ doPostBack 脚本是通过 PostBackOptions

The server side asp:TextBox, and the client side input which fires __doPostBack on click. The __doPostBack script is generated through PostBackOptions:

protected string GetOnChangedScript()
{
    var options = new PostBackOptions(txtSearch, string.Empty);
    options.AutoPostBack = true;
    options.RequiresJavaScriptProtocol = true;
    var script = Page.ClientScript.GetPostBackEventReference(options);
    return script;
}

txtSearch_TextChanged 事件处理程序在更改文本框的值时触发,并单击 submit 按钮。

The txtSearch_TextChanged event handler fires when the value of text box is changed, and the submit button is clicked.

但请注意,对于像 TextBox 这样的控件,文本存储在viewstate中;用户输入的新文本存储在表单数据中。当 TextBox 加载视图状态数据时,它将获取旧值。将其与表单中的新值进行比较。如果值不同,则会触发 TextChanged 事件。如果不是,则不会触发事件处理程序。这是 Page_Load 事件被触发(回发发生)的原因,但 txtSearch_TextChanged 由于 TextBox 的值没有改变。

But note, for controls like TextBox the text is stored in the viewstate; the new text entered by the user stores in the form data. When the TextBox load the viewstate data, it gets the old value. This is compared with the new value that comes in the form. If the values are different, the TextChanged event is fired. If no, the event handler won't be fired. This is the reason for the Page_Load event get fired (postback occured), but txtSearch_TextChanged didn't fire due to the value of the TextBox didn't change.

这篇关于使用__doPostBack从JavaScript调用ASP.NET TextChanged事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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