TextBox导致ASP.NET中的Button Postback

时间:2022-09-21 03:20:06

ASP.NET 2.0, testing in FF3 and IE7.

ASP.NET 2.0,在FF3和IE7中测试。

When I hit the 'enter' button from a text box the corresponding "OnClick" event for the first ImageButton in the page is fired. If I remove that image button, it fires the next ImageButton OnClick event on the page.

当我从文本框中点击“输入”按钮时,会触发页面中第一个ImageButton的相应“OnClick”事件。如果我删除该图像按钮,它将触发页面上的下一个ImageButton OnClick事件。

From the FireBug console, if I use JavaScript to submit the Form, this does not happen. But for whatever reason hitting enter from the textbox triggers the unrelated ImageButton event.

在FireBug控制台中,如果我使用JavaScript提交表单,则不会发生这种情况。但无论出于何种原因,从文本框中输入Enter会触发无关的ImageButton事件。

I found this question which had a similar problem, however the proposed answer to that solution doesn't work since ImageButtons do not have a "UseSubmitBehavior" property on them.

我发现这个问题有类似的问题,但是由于ImageButtons没有“UseSubmitBehavior”属性,因此该解决方案的建议答案不起作用。

I don't understand why this event is firing. If I look at Request.Form, I can see that __EVENTTARGET is empty, and it is in fact posting the entire form contents (all of my textboxes), but also includes imageButton.x and imageButton.y key/value pairs.

我不明白为什么这个事件会被解雇。如果我查看Request.Form,我可以看到__EVENTTARGET是空的,它实际上是发布整个表单内容(我的所有文本框),但也包括imageButton.x和imageButton.y键/值对。

Why is this? I suppose I could detect "enter" key presses from these text boxes with javascript, but my experience in the past is this behavior is highly variable between browsers. Any suggestions?

为什么是这样?我想我可以通过javascript检测这些文本框中的“输入”按键,但我过去的经验是这种行为在浏览器之间变化很大。有什么建议?

8 个解决方案

#1


You could try setting a default button in an asp panel or on your form. This will let you control what happens when a user hits the enter key.

您可以尝试在asp面板或表单上设置默认按钮。这将让您控制用户点击回车键时发生的情况。

#2


here's a more elegant solution

这是一个更优雅的解决方案

<asp:TextBox ID="TextBox1" runat="server" onkeydown = "return (event.keyCode!=13);" > </asp:TextBox>

read the entire post here

在这里阅读整篇文章

#3


I'm having the same issue on my project.

我在我的项目中遇到了同样的问题。

This issue is caused because ASP.NET always will assume that the first element that inherits from IButton interface (Button and ImageButton) is the default button from the page.

导致此问题是因为ASP.NET始终假定从IButton接口(Button和ImageButton)继承的第一个元素是页面中的默认按钮。

Hipoteticaly, if you use an LinkButton instead of Button or ImageButton, this issue is solved.

Hipoteticaly,如果你使用LinkBut​​ton而不是Button或ImageButton,这个问题就解决了。

You can find more information here on MSDN.

您可以在MSDN上找到更多信息。

#4


You can disable the Enter key from being pressed, so the user will have to click on of your ImageButtons. Just paste this javascript block onto your page:

您可以禁用Enter键,因此用户必须单击ImageButtons。只需将此javascript块粘贴到您的网页上即可:

<script type="text/javascript">
function stopRKey(evt) { 
  var evt = (evt) ? evt : ((event) ? event : null); 
  var node = (evt.target) ? evt.target : ((evt.srcElement) ? evt.srcElement : null); 
  if ((evt.keyCode == 13) && (node.type=="text"))  {return false;} 
}
document.onkeypress = stopRKey;
</script>

#5


Recently, I've been doing more on the client with web services and fewer postbacks. By moving my controls outside of the form element (or eliminating it altogether), the problem goes away. It's inserted by default on aspx pages, but it didn't occur to me until recently that I don't need it for much of what I do.

最近,我在客户端上做了更多的Web服务和更少的回发。通过将我的控件移动到表单元素之外(或完全取消它),问题就消失了。它默认插入到aspx页面上,但直到最近我才发现它并不需要我做的很多事情。

#6


Its the default behaviour for an enter button press in a non text area to post back a form. You would have to handle it in a javascript method to stop the postback.

它是输入按钮按下非文本区域以回发表单的默认行为。您必须在javascript方法中处理它以停止回发。

You'd just need to check the window.event.keyCode property to see if its equal to 13. If it is, reset it to 0.

您只需要检查window.event.keyCode属性以查看它是否等于13.如果是,则将其重置为0。

function KeyPress()
  {
     if (window.event.keyCode == 13)
        {
           window.event.keyCode = 0;
        }
  }

#7


I suppose I could detect "enter" key presses from these text boxes with javascript

我想我可以通过javascript检测这些文本框中的“输入”按键

That's what I did to get around that behaviour and it works great in IE7 and FF3. It's just a little unnatural.

这就是我为解决这种行为而采取的措施,它在IE7和FF3中运行良好。这有点不自然。

Here is a generic exemple:

这是一个通用的例子:

    function TextBox1_KeyDown(sender, e)
    {
    var key;
        if(window.event)
            key = window.event.keyCode; //IE
        else
            key = e.which; //firefox
        if(key == 13 && $("#TextBox1").val() != "")
        {
            WebForm_DoPostBackWithOptions(new WebForm_PostBackOptions("TextBox1", "", true, "", "", false, true));
        }
        return (key != 13);
    }

I used WebForm_DoPostBackWithOptions because I needed validators to trigger. Otherwise, you might want to use __DoPostBack.

我使用了WebForm_DoPostBackWithOptions,因为我需要验证器来触发。否则,您可能想要使用__DoPostBack。

Here are the "prototypes":

这是“原型”:

function __doPostBack(eventTarget, eventArgument)

function WebForm_PostBackOptions(eventTarget, eventArgument, validation, validationGroup, actionUrl, trackFocus, clientSubmit)
{
    this.eventTarget = eventTarget;
    this.eventArgument = eventArgument;
    this.validation = validation;
    this.validationGroup = validationGroup;
    this.actionUrl = actionUrl;
    this.trackFocus = trackFocus;
    this.clientSubmit = clientSubmit;
}

function WebForm_DoPostBackWithOptions(options)

Hope it helps.

希望能帮助到你。

P.S.: I used JQuery here but $get would be the same.

P.S。:我在这里使用了JQuery但是$ get会是一样的。

#8


Here's an elegant solution I have found, in case anybody else has this problem (in case all other solution don't work for you, as they didn't work for me):

这是我找到的一个优雅的解决方案,以防其他任何人遇到此问题(如果所有其他解决方案都不适合您,因为它们对我不起作用):

<asp:UpdatePanel runat="server">
<ContentTemplate>
    <asp:Panel runat="server" DefaultButton="doNothingButton">
        <ul id="shopping-list-ul">
        </ul>
        <asp:Button CssClass="invisible" runat="server" ID="doNothingButton" OnClientClick="return false;" />
    </asp:Panel>
</ContentTemplate>

The textbox iself was inside the ul (generated by javascript).
Pressing enter will trigger the "doNothingButton", which will return false on client side, causing no postback at all!

文本框iself在ul内部(由javascript生成)。按Enter将触发“doNothingButton”,它将在客户端返回false,导致根本没有回发!

#1


You could try setting a default button in an asp panel or on your form. This will let you control what happens when a user hits the enter key.

您可以尝试在asp面板或表单上设置默认按钮。这将让您控制用户点击回车键时发生的情况。

#2


here's a more elegant solution

这是一个更优雅的解决方案

<asp:TextBox ID="TextBox1" runat="server" onkeydown = "return (event.keyCode!=13);" > </asp:TextBox>

read the entire post here

在这里阅读整篇文章

#3


I'm having the same issue on my project.

我在我的项目中遇到了同样的问题。

This issue is caused because ASP.NET always will assume that the first element that inherits from IButton interface (Button and ImageButton) is the default button from the page.

导致此问题是因为ASP.NET始终假定从IButton接口(Button和ImageButton)继承的第一个元素是页面中的默认按钮。

Hipoteticaly, if you use an LinkButton instead of Button or ImageButton, this issue is solved.

Hipoteticaly,如果你使用LinkBut​​ton而不是Button或ImageButton,这个问题就解决了。

You can find more information here on MSDN.

您可以在MSDN上找到更多信息。

#4


You can disable the Enter key from being pressed, so the user will have to click on of your ImageButtons. Just paste this javascript block onto your page:

您可以禁用Enter键,因此用户必须单击ImageButtons。只需将此javascript块粘贴到您的网页上即可:

<script type="text/javascript">
function stopRKey(evt) { 
  var evt = (evt) ? evt : ((event) ? event : null); 
  var node = (evt.target) ? evt.target : ((evt.srcElement) ? evt.srcElement : null); 
  if ((evt.keyCode == 13) && (node.type=="text"))  {return false;} 
}
document.onkeypress = stopRKey;
</script>

#5


Recently, I've been doing more on the client with web services and fewer postbacks. By moving my controls outside of the form element (or eliminating it altogether), the problem goes away. It's inserted by default on aspx pages, but it didn't occur to me until recently that I don't need it for much of what I do.

最近,我在客户端上做了更多的Web服务和更少的回发。通过将我的控件移动到表单元素之外(或完全取消它),问题就消失了。它默认插入到aspx页面上,但直到最近我才发现它并不需要我做的很多事情。

#6


Its the default behaviour for an enter button press in a non text area to post back a form. You would have to handle it in a javascript method to stop the postback.

它是输入按钮按下非文本区域以回发表单的默认行为。您必须在javascript方法中处理它以停止回发。

You'd just need to check the window.event.keyCode property to see if its equal to 13. If it is, reset it to 0.

您只需要检查window.event.keyCode属性以查看它是否等于13.如果是,则将其重置为0。

function KeyPress()
  {
     if (window.event.keyCode == 13)
        {
           window.event.keyCode = 0;
        }
  }

#7


I suppose I could detect "enter" key presses from these text boxes with javascript

我想我可以通过javascript检测这些文本框中的“输入”按键

That's what I did to get around that behaviour and it works great in IE7 and FF3. It's just a little unnatural.

这就是我为解决这种行为而采取的措施,它在IE7和FF3中运行良好。这有点不自然。

Here is a generic exemple:

这是一个通用的例子:

    function TextBox1_KeyDown(sender, e)
    {
    var key;
        if(window.event)
            key = window.event.keyCode; //IE
        else
            key = e.which; //firefox
        if(key == 13 && $("#TextBox1").val() != "")
        {
            WebForm_DoPostBackWithOptions(new WebForm_PostBackOptions("TextBox1", "", true, "", "", false, true));
        }
        return (key != 13);
    }

I used WebForm_DoPostBackWithOptions because I needed validators to trigger. Otherwise, you might want to use __DoPostBack.

我使用了WebForm_DoPostBackWithOptions,因为我需要验证器来触发。否则,您可能想要使用__DoPostBack。

Here are the "prototypes":

这是“原型”:

function __doPostBack(eventTarget, eventArgument)

function WebForm_PostBackOptions(eventTarget, eventArgument, validation, validationGroup, actionUrl, trackFocus, clientSubmit)
{
    this.eventTarget = eventTarget;
    this.eventArgument = eventArgument;
    this.validation = validation;
    this.validationGroup = validationGroup;
    this.actionUrl = actionUrl;
    this.trackFocus = trackFocus;
    this.clientSubmit = clientSubmit;
}

function WebForm_DoPostBackWithOptions(options)

Hope it helps.

希望能帮助到你。

P.S.: I used JQuery here but $get would be the same.

P.S。:我在这里使用了JQuery但是$ get会是一样的。

#8


Here's an elegant solution I have found, in case anybody else has this problem (in case all other solution don't work for you, as they didn't work for me):

这是我找到的一个优雅的解决方案,以防其他任何人遇到此问题(如果所有其他解决方案都不适合您,因为它们对我不起作用):

<asp:UpdatePanel runat="server">
<ContentTemplate>
    <asp:Panel runat="server" DefaultButton="doNothingButton">
        <ul id="shopping-list-ul">
        </ul>
        <asp:Button CssClass="invisible" runat="server" ID="doNothingButton" OnClientClick="return false;" />
    </asp:Panel>
</ContentTemplate>

The textbox iself was inside the ul (generated by javascript).
Pressing enter will trigger the "doNothingButton", which will return false on client side, causing no postback at all!

文本框iself在ul内部(由javascript生成)。按Enter将触发“doNothingButton”,它将在客户端返回false,导致根本没有回发!