规避asp事件处理程序
我想通过更改 VB.Net 页面中某些 javascript 中的“window.location”值来引起浏览器重定向。当我单击“Go”按钮时,下面的代码可以正常工作:出现警报并且浏览器已成功重定向。如果我从文本中按“输入”,也会发生同样的情况,框这不起作用。我确实看到了警报,但浏览器仍停留在同一页面上。
如果我删除对 ASP 母版页的引用,“输入”功能就会开始工作,所以也许其中的某些内容正在处理按键事件并终止重定向指令?
顺便说一句,我最初是使用 .Net 组件“正常”执行此操作,并在单击提交按钮时执行回发。但奇怪的是,这导致目标页面加载两次,导致页面闪烁,并且通常效率低下。无论如何,我认为没有任何理由涉及所有 .Net 开销,因此我希望通过简单的客户端脚本来完成它。
有谁知道如何在 .Net 干扰的情况下让“输入”键功能正常工作?或者我是不是找错了方向,应该回到常规的 .Net 回发?
<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="server">
<script type="text/javascript">
function clickHandler() {
var dropdown = document.getElementById("dropdown");
var entityID = document.getElementById("textOne").value;
var destination = dropdown.options(dropdown.selectedIndex).value + entityID;
alert(destination);
location = destination;
}
function goIfEnterPressed() {
if ((event.which && event.which != 13) || (event.keyCode && event.keyCode != 13)) {
return false;
}
clickHandler();
}
</script>
</asp:Content>
<asp:Content ContentPlaceHolderID="ContentPlaceHolder2" runat="server">
<select id="dropdown">
<option value="Page1.aspx?typeOneID=">One</option>
<option value="Page2.aspx?typeTwoID=">Two</option>
<option value="Page3.aspx?typeThreeID=">Three</option>
</select>
<br />
<input id="textOne" onkeypress="goIfEnterPressed()"/>
<br />
<input type="button" value="Go" onclick="clickHandler()" />
</asp:Content>
I want to cause a browser redirect by changing the value of "window.location" in some javascript in a VB.Net page. The code below works correctly when I click the "Go" button: The alert appears and the browser is successfully redirected. The same should happen if I press "enter" from inside the text, box this doesn't work. I do see the alert, but the browser stays on the same page.
The "enter" functionality starts working if I remove the references to ASP master pages, so maybe something in there is handling the keypress event and killing the redirect instruction?
Btw, I was originally doing this "normally" using .Net components and doing a postback when the submit button is clicked. But that was weirdly causing the target page to load twice, causing page flicker and generally being inefficient. I didn't see any reason to involve all that .Net overhead anyway, so I hoped to do it with straightforward client-side scripting.
Does anyone know how I can get the "enter" key functionality to work in spite of .Net interference? Or am I barking up the wrong tree and should go back to a regular .Net postback?
<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="server">
<script type="text/javascript">
function clickHandler() {
var dropdown = document.getElementById("dropdown");
var entityID = document.getElementById("textOne").value;
var destination = dropdown.options(dropdown.selectedIndex).value + entityID;
alert(destination);
location = destination;
}
function goIfEnterPressed() {
if ((event.which && event.which != 13) || (event.keyCode && event.keyCode != 13)) {
return false;
}
clickHandler();
}
</script>
</asp:Content>
<asp:Content ContentPlaceHolderID="ContentPlaceHolder2" runat="server">
<select id="dropdown">
<option value="Page1.aspx?typeOneID=">One</option>
<option value="Page2.aspx?typeTwoID=">Two</option>
<option value="Page3.aspx?typeThreeID=">Three</option>
</select>
<br />
<input id="textOne" onkeypress="goIfEnterPressed()"/>
<br />
<input type="button" value="Go" onclick="clickHandler()" />
</asp:Content>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您的 Enter 事件可能会被 .Net javascript 内容捕获。通常在这种情况下,我会在开始工作之前阻止事件的默认行为。我不知道你是否使用 jQuery,但代码会是这样的:
Your enter event might be getting caught by the .Net javascript stuff. Normally in a case like this I would to prevent the default behaviour of the event before doing my work. I don't know if you use jQuery or not, but the code would be something like:
您的 MasterPage 内容中是否有一个按钮?这听起来不像是 .NET 问题,听起来更像是对 HTML 标准的误解。由于您的所有内容都位于单个
Is there a button somewhere inside your MasterPage's content? This doesn't sound like it's a .NET issue, it sounds more like it could be a misunderstanding of HTML standards. Because all your content is inside a single
<form>
element, the browser will submit the page based on the first<submit>
button it finds on your page. You can work around this by grouping your content inside an<asp:Panel>
and setting itsDefaultButton
property to the ID of your submit button. You may also be able to set theValidationGroup
property of both your textbox and button to accomplish the same thing, but I can't remember for sure if that works in this situation or if that only affects validation controls.您没有将事件传递到事件处理程序中。这可能是您问题的一部分 - 至少在 IE 以外的浏览器中是这样。
另外,你真的想为非输入键返回 false 吗?看来您正试图取消该活动,但我认为这不会像所写的那样有效。但是,如果它确实有效,您将阻止在文本框中输入任何文本。似乎是一种奇怪的行为。
You aren't passing the event into the event handler. This may be part of your problem - at least in browsers other than IE.
Also, did you really want to return false for non-enter keys? It looks like you're trying to cancel the event, but I don't think it would quite work as written. But then, if it did work, you would be preventing any text from being typed into the textbox. Seems like an odd behavior.