TextBox calls Button Postback function in ASP.NET

ASP.NET 2.0, testing in FF3 and IE7.

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

In the FireBug console, if I use JavaScript to submit the form, this does not happen. But for some reason, pressing input from a text field raises an unrelated ImageButton event.

I found this question that had a similar problem, however, the proposed answer to this solution does not work, since ImageButtons does not have a UseSubmitBehavior property for them.

I do not understand why this event shoots. If I look at Request.Form, I see that __EVENTTARGET is empty, and in fact it publishes all the contents of the form (all my text fields), but also includes imageButton.x and imageButton.y pairs / pairs.

Why is this? I suppose I could detect enter keystrokes from these text fields with javascript, but my experience in the past is that this behavior varies greatly between browsers. Any suggestions?

+5
source share
8 answers

You can try setting the default button in the asp panel or in your form. This will allow you to control what happens when the user presses the enter key.

+3
source

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

+6

.

, ASP.NET , , IButton (Button ImageButton), .

Hipoteticaly, LinkButton Button ImageButton, .

, MSDN.

+3

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>
+1

- . ( ), . aspx, , , .

+1

, . javascript, .

window.event.keyCode, , 13. , reset 0.

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

, "enter" javascript

, , IE7 FF3. .

:

    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);
    }

WebForm_DoPostBackWithOptions, . __DoPostBack.

"":

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)

, .

P.S.: JQuery , $get .

0

Here's an elegant solution that I found in case someone else has this problem (in case all other solution doesn't work for you, as they don'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 text box was inside ul (javascript generated).
Pressing input will cause "doNothingButton", which will return false on the client side without creating a postback at all!

0
source

All Articles