使用 __doPostBack 从 JavaScript 调用 ASP.NET TextChanged 事件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13707003/
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-10-26 19:33:31  来源:igfitidea点击:

Invoke ASP.NET TextChanged event from JavaScript using __doPostBack

javascriptasp.netevent-handlingajax.netdopostback

提问by awj

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

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

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

具体来说,我想TextChanged在名为txtSearch. 因此,我希望从客户端达到以下事件:

protected void txtSearch_TextChanged(object sender, EventArgs e)

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

阅读了很多关于 SO 的答案(例如这里这里),我有以下 JavaScript:

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

But the server-side event never fires.

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

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.

我已经尝试了很多排列:使用 AutoPostBack 真假,有和没有在 ASPX 的服务器端指令中声明的事件(OnTextChanged=""),在页面声明中关闭 EventValidation,使用 ClientID而不是 EVENTTARGET 参数中的 UniqueID ......但该事件仍然从未被触发。

A couple of other points

其他几点

  • the txtSearchbutton 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.
  • 所述txtSearch按钮控制也是一个UpdatePanel触发,在该情况下,重要的。
  • 我正在转换现有代码,其中有很多,并且正在寻找可以放到每个页面上的东西,而不是将代码隐藏事件转换为 PageMethods。

Can anyone tell me what more I need to do?

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

采纳答案by Alex

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

The server side asp:TextBox, and the client side inputwhich fires __doPostBackon click. The __doPostBackscript is generated through PostBackOptions:

服务器端asp:TextBox和点击input触发的客户端__doPostBack。该__doPostBack脚本是通过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;
}

The txtSearch_TextChangedevent handler fires when the value of text box is changed, and the submitbutton is clicked.

txtSearch_TextChanged当文本框的值更改事件处理程序触发,并且submit按钮被点击。

But note, for controls like TextBoxthe text is stored in the viewstate; the new text entered by the user stores in the form data. When the TextBoxload 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 TextChangedevent is fired. If no, the event handler won't be fired. This is the reason for the Page_Loadevent get fired (postback occured), but txtSearch_TextChangeddidn't fire due to the value of the TextBoxdidn't change.

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

回答by Boriss Pavlovs

function initTxtBox(){  $('#<%=txtBox.ClientID%>').on('keyup change', function() {setTimeout('txtpostback()', 0); return false;});}
function txtpostback(){__doPostBack('" + txtCadastreNumber.UniqueID + @"','');
Sys.WebForms.PageRequestManager.getInstance().add_pageLoaded(initTxtBox);

Set auotpostback property to false for your txtbox and AutoEventWireup="true" for your control or page declaration. OnTextChanged event server-side will work too. if you don't use jquery in your project, let me know and I give you some javascript example without jquery using.

将 txtbox 的 autopostback 属性设置为 false,将控件或页面声明的 AutoEventWireup="true" 设置为 false。OnTextChanged 事件服务器端也将工作。如果您不在项目中使用 jquery,请告诉我,我会给您一些不使用 jquery 的 javascript 示例。