4

我正在尝试在 UpdatePanel 中将 LinkBut​​tons 与 ASP.NET 面板的 DefaultButton 属性一起使用。我已经阅读并使用了描述点击事件连接的各种其他答案,因此不会完成完整的回发而不是部分回发。当页面加载时,我连接了 LinkBut​​ton 的 .click 函数,这样 ASP.NET 面板的 DefaultButton 属性就可以工作了。

这一切都很好,直到您将 UpdatePanel 加入其中。对于 UpdatePanel,如果存在部分回发,则在部分回发中不会调用连接 .click 函数的脚本,并且按 Enter 会恢复为导致表单的完整提交而不是触发 LinkBut​​ton。

如何在部分回发后执行 javascript 以重新连接 LinkBut​​ton 的 .click 函数?

我制作了一个显示问题的示例页面。有两个警报显示 1) 当调用连接 .click 函数的代码时,以及 2) 当调用 .click 函数时(这仅在事件连接后在文本框中按 Enter 时发生)。要测试此代码,请在文本框中输入内容并按 Enter。文本将被复制到标签控件,但不会显示“连接事件单击”警报。添加另一个字母,再次按回车键,您将获得完整的回发,而不会将文本复制到标签控件(因为没有调用 LinkBut​​ton)。因为那是一个完整的回发,所以 Wiring Up Event Click 事件将被再次调用,并且表单将在下一次再次正常工作。

这是通过 ASP.NET 3.5 完成的。

测试用例代码:

<%@ Page Language="C#" Inherits="System.Web.UI.Page" Theme="" EnableTheming="false" AutoEventWireup="true" %>

<script runat="server">
    void cmdComplete_Click(object sender, EventArgs e)
    {
        lblOutput.Text = "Complete Pressed: " + txtInput.Text;
    }

    void cmdFirstButton_Click(object sender, EventArgs e)
    {
        lblOutput.Text = "First Button Pressed";
    }

    protected override void OnLoad(EventArgs e)
    {
        HookupButton(cmdComplete);
    }

    void HookupButton(LinkButton button)
    {
        // Use the click event of the LinkButton to trigger the postback (this is used by the .click code below)
        button.OnClientClick = Page.ClientScript.GetPostBackEventReference(button, String.Empty);

        // Wire up the .click event of the button to call the onclick function, and prevent a form submit
        string clickString = string.Format(@"
            alert('Wiring up click event'); 
            document.getElementById('{0}').click = function() {{ 
                alert('Default button pressed'); 
                document.getElementById('{0}').onclick(); 
            }};", button.ClientID, Page.ClientScript.GetPostBackEventReference(button, ""));
        Page.ClientScript.RegisterStartupScript(button.GetType(), "click_hookup_" + button.ClientID, clickString, true);
    }
</script>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
    <title>DefaultButton/LinkButton Testing</title>
    <style type="text/css">
        a.Button { line-height: 2em; padding: 5px; border: solid 1px #CCC; background-color: #EEE; }
    </style>
</head>
<body>
    <h1>
        DefaultButton/LinkButton Testing</h1>
    <form runat="server">
    <asp:ScriptManager runat="server" />
    <asp:UpdatePanel ID="UpdatePanel1" runat="server">
        <ContentTemplate>
            <div style="position: relative">
                <fieldset>
                    <legend>Output</legend>
                    <asp:Label runat="server" ID="lblOutput" />
                </fieldset>
                <asp:Button runat="server" Text="First Button" ID="cmdFirstButton" OnClick="cmdFirstButton_Click" UseSubmitBehavior="false" />
                <asp:Panel ID="Panel1" runat="server" DefaultButton="cmdComplete">
                    <label>
                        Enter Text:</label>
                    <asp:TextBox runat="server" ID="txtInput" />
                    <asp:LinkButton runat="server" CssClass="Button" ID="cmdComplete" OnClick="cmdComplete_Click" Text="Complete" />
                </asp:Panel>
            </div>
        </ContentTemplate>
    </asp:UpdatePanel>
    <asp:Button runat="server" ID="cmdFullPostback" Text="Full Postback" />
    </form>
</body>
</html>
4

1 回答 1

2

我最终找到了解决方案 - 我应该一直使用ScriptManager.RegisterStartupScript而不是Page.ClientScript.RegisterStartupScript- 这在UpdatePanels.

于 2010-06-02T08:22:57.243 回答