Autopostback 和 doPostBack 冲突
我已启用表单文本框之一的 AutoPostBack
属性,根据 w3 school 应该仅在我按 Enter 或 Tab 时触发回发。
根据用户对 javascript 提示的回答,我还在 pageLoad 上调用 __doPostBack()
。当我这样做时,Request.Form['__EventTarget']
不是我在调用 __doPostBack
时设置的值。
对我来说真正的问题是,如果我将 TextBox 的 AutoPostBack 属性设置为 false,则页面加载 __doPostBack 调用的问题就会消失。这不是我所期望的行为。关于导致问题的原因有什么想法吗?为什么启用 AutoPostBack 会有影响?
以下是一些代码:
asp:TextBox runat="server" ID="userName" OnTextChanged="UpdateTable" AutoPostBack="true"
script type="text/javascript"
//![CDATA[
var theForm = document.forms['form1'];
if (!theForm) {
theForm = document.form1;
}
function __doPostBack(eventTarget, eventArgument) {
if (!theForm.onsubmit || (theForm.onsubmit() != false)) {
theForm.__EVENTTARGET.value = eventTarget;
theForm.__EVENTARGUMENT.value = eventArgument;
theForm.submit();
}
}
//]]
/script
input type="hidden" name="__EVENTTARGET" id="__EVENTTARGET" value=""
input type="hidden" name="__EVENTARGUMENT" id="__EVENTARGUMENT" value=""
input type="hidden" name="__LASTFOCUS" id="__LASTFOCUS" value=""
function jsPrompt(name) {
var ans = confirm('really follow ' + name + '?');
if (ans) {
__doPostBack('follow', name);
}
}
然后在代码隐藏页面中:
if (Request.Form["__EventTarget"] == "follow")
followPerson(Request.Form["__EventArgument"]);
但是,我不断发现 Request.Form["__EventTarget"]
是 ","
,并且我已经在调试器中逐步执行 JavaScript。在 form.submit()
之前,参数不是 ","
I have enabled the AutoPostBack
property of one of my form's textboxes, which according to w3 schools should only trigger a postback when I press enter or tab.
I am also calling a __doPostBack()
on pageLoad, given a user's answer to a javascript prompt. When I do that, the Request.Form['__EventTarget']
is not what I set it to be in the call to __doPostBack
.
The real issue to me is that if I set the TextBox's AutoPostBack attribute to false, the problem with the pageload __doPostBack call goes away. This is not behavior I expected. Any ideas about what is causing the problem? Why would the AutoPostBack enabled have any influence?
Here is some of the code:
asp:TextBox runat="server" ID="userName" OnTextChanged="UpdateTable" AutoPostBack="true"
script type="text/javascript"
//![CDATA[
var theForm = document.forms['form1'];
if (!theForm) {
theForm = document.form1;
}
function __doPostBack(eventTarget, eventArgument) {
if (!theForm.onsubmit || (theForm.onsubmit() != false)) {
theForm.__EVENTTARGET.value = eventTarget;
theForm.__EVENTARGUMENT.value = eventArgument;
theForm.submit();
}
}
//]]
/script
input type="hidden" name="__EVENTTARGET" id="__EVENTTARGET" value=""
input type="hidden" name="__EVENTARGUMENT" id="__EVENTARGUMENT" value=""
input type="hidden" name="__LASTFOCUS" id="__LASTFOCUS" value=""
function jsPrompt(name) {
var ans = confirm('really follow ' + name + '?');
if (ans) {
__doPostBack('follow', name);
}
}
Then in the codebehind page:
if (Request.Form["__EventTarget"] == "follow")
followPerson(Request.Form["__EventArgument"]);
But, I keep getting that Request.Form["__EventTarget"]
is ","
, and I've stepped through the javascript in the debugger. Just before form.submit()
, the arguments are not ","
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
好吧...从哪里开始。您到底想在这里完成什么?我想不出在 ASP.NET 中查看 Request.Form["__EventTarget"] 的合理理由。我唯一一次看到这种情况是当老派 ASP 或 PHP 程序员第一次学习 ASP.NET 并且还没有完全掌握 ASP.NET 背后的事件驱动模型时。
另外,当你说你正在调用 __doPostBack() 时,你是什么意思?您也不应该需要手动执行此操作。如果您确实想在通常不会发生的情况下在 javascript 中创建回发,则需要使用 ClientScriptManager.GetPostBackEventReference()。因此,使用此方法并将其传递给您的 TextBox 以获得正确的回发代码。然后您可以在客户端脚本中的任何位置执行它。
希望这有帮助。如果我误解了这个场景,请添加评论,我会再试一次。 :)
OK... where to begin. What exactly are you trying to accomplish here? I can think of no legitimate reason to look at Request.Form["__EventTarget"] in ASP.NET. The only time I've seen this is when an old school ASP or PHP programmer first learns ASP.NET and hasn't quite grasped the event-driven model that underlies ASP.NET.
Also, what do you mean when you say you are calling __doPostBack()? You shouldn't need to do this manually either. If you really want to create a postback in javascript where it normally doesn't happen, you need to use ClientScriptManager.GetPostBackEventReference(). So, use this method and pass it your TextBox to get the proper postback code. Then you can execute it anywhere in your client side script.
Hope this helps. If I've misunderstood the scenario please add a comment and I'll try again. :)
您使用的代码在哪里?确保您使用的是类似:
HTH。
Where is the code you are using? Make sure you are using something like:
HTH.