当按下enter时,UpdatePanel不会触发

时间:2022-09-20 14:42:16

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.

我有一个面板,其中有一些文本框和一个submit按钮,以及一个updatepanel,当按钮被按下时,它将被设置为触发器。

If I press the button, it works as normal. However if I press enter on a control, it posts back the entire page.

如果我按下按钮,它就正常工作了。但是,如果我在控件上按enter键,它就会返回整个页面。

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

我将该按钮设置为面板中的默认按钮,但它仍将返回整个页面。什么好主意吗?

2 个解决方案

#1


3  

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.

您可以轻松地使用上面的javascript,而无需将其设置为静态方法。

#2


0  

In the UpdatePanel properties, set the following:

在UpdatePanel属性中,设置以下内容:

ChildrenAsTriggers = True UpdateMode = Always

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.

如果这样做,您不必指定何时触发更新面板,当您的任何控件引起回发时,将自动触发更新面板。

#1


3  

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.

您可以轻松地使用上面的javascript,而无需将其设置为静态方法。

#2


0  

In the UpdatePanel properties, set the following:

在UpdatePanel属性中,设置以下内容:

ChildrenAsTriggers = True UpdateMode = Always

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.

如果这样做,您不必指定何时触发更新面板,当您的任何控件引起回发时,将自动触发更新面板。