开发者

UpdatePanel doesn't trigger when enter is pressed

开发者 https://www.devze.com 2022-12-08 02:32 出处:网络
I have a panel with some text boxes and a submit button, and an updatepanel that is set to trigger when the button is pressed.

I have a panel with some text boxes and a submit button, and an updatepanel that is set to trigger when the button is pressed.

If I 开发者_如何学JAVApress the button, it works as normal. However if I press enter on a control, it posts back the entire page.

I set the button as the default button in the panel, but it still posts back the entire page. Any ideas?


Setting a single button on a panel as a default is always a bit tricky. I've kept a method in a common library around for just this issue:

public static void EnterOnKeyDown(WebControl targetControl, WebControl controlToPress)
{
    targetControl.Attributes.Add("onkeydown",
        "if(event.which || event.keyCode){if ((event.which == 13)" +
        "|| (event.keyCode == 13)) {document.getElementById('" +
        controlToPress.ClientID + "').click();return false;}} else {return true};");
}

You can place this method anywhere you like and call it as follows:

EnterOnKeyDown(someTextBoxInYourPanel, yourSubmitButton);

You could just as easily use the javascript above on its own without setting it in a static method.


In the UpdatePanel properties, set the following:

ChildrenAsTriggers = True UpdateMode = Always

If you do this, you don't have to specify when to trigger the update panel, it will automatically be triggered when any of your controls causes a postback.

0

精彩评论

暂无评论...
验证码 换一张
取 消