使用 __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
Invoke ASP.NET TextChanged event from JavaScript using __doPostBack
提问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 TextChanged
event 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
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.
- 所述
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 input
which fires __doPostBack
on click. The __doPostBack
script 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_TextChanged
event handler fires when the value of text box is changed, and the submit
button is clicked.
在txtSearch_TextChanged
当文本框的值更改事件处理程序触发,并且submit
按钮被点击。
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.
但请注意,对于像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 示例。