将 linkbutton 设置为 asp.net 中 asp:panel 的默认按钮

发布于 2024-09-16 08:38:40 字数 2252 浏览 3 评论 0原文

可能的重复:
页面上的链接按钮并将其设置为默认按钮,在 IE 中工作正常,但在 Mozila 中不行

如何将链接按钮设置为 asp.net 中 asp:panel 的默认按钮?我知道可以将按钮设置为默认按钮,但我的应用程序对所有表单都使用链接按钮。任何建议如何完成。

编辑:

现在我尝试了这个, 它也适用于 Firefox,但我的 javascript 验证(即)客户端单击我的链接按钮不起作用,为什么?

var __defaultFired = false;

        function WebForm_FireDefaultButton(event, target) {
            var element = event.target || event.srcElement;

            if (!__defaultFired && event.keyCode == 13 && !(element && (element.tagName.toLowerCase() == "textarea"))) {
                var defaultButton;

                if (__nonMSDOMBrowser)
                    defaultButton = document.getElementById(target);
                else
                    defaultButton = document.all[target];

                if (defaultButton) {
                    if (typeof (defaultButton.click) != "undefined")
                        defaultButton.click();
                    else
                        eval(unescape(defaultButton.href.replace("javascript:", "")));

                    event.cancelBubble = true;

                    if (event.stopPropagation) event.stopPropagation();
                    return false;
                }
            }
            return true;
        }

第二次编辑:

我能够使我的自定义链接按钮控件正常工作,但无法将 OnClientClick 挂钩到它。来源 using-panel- defaultbutton-property-with-linkbutton-control-in-asp-net

我这样做了,

<%@ Register Namespace="App_Code" TagPrefix="ac" %>
<asp:Label runat="server" ID="lblHello" />
<asp:Panel runat="server" DefaultButton="lbHello">
    First name: <asp:TextBox runat="server" ID="txtFirstName" />
    <ac:LinkButtonDefault ID="lbHello" runat="server" Text="Click me" 
        OnClientClick="javascript:alert('hai');" OnClick="lbHello_Click" />
</asp:Panel>

我的客户端功能不起作用,为什么?任何建议。

Possible Duplicate:
Link Button on the page and set it as default button, work fine in IE but not in Mozila

How to set linkbutton as default button for asp:panel in asp.net? I know a button can be set as default but my application uses linkbuttons for all forms. Any suggestion how it can be done.

EDIT:

Now i tried this,
It works in firefox as well but my javascript validation (ie) onclient click of my linkbutton doesn't work why?

var __defaultFired = false;

        function WebForm_FireDefaultButton(event, target) {
            var element = event.target || event.srcElement;

            if (!__defaultFired && event.keyCode == 13 && !(element && (element.tagName.toLowerCase() == "textarea"))) {
                var defaultButton;

                if (__nonMSDOMBrowser)
                    defaultButton = document.getElementById(target);
                else
                    defaultButton = document.all[target];

                if (defaultButton) {
                    if (typeof (defaultButton.click) != "undefined")
                        defaultButton.click();
                    else
                        eval(unescape(defaultButton.href.replace("javascript:", "")));

                    event.cancelBubble = true;

                    if (event.stopPropagation) event.stopPropagation();
                    return false;
                }
            }
            return true;
        }

Second EDIT:

I was able make my custom linkbutton control work but couldn't able to hook OnClientClick to it. Source using-panel-defaultbutton-property-with-linkbutton-control-in-asp-net.

and i did this,

<%@ Register Namespace="App_Code" TagPrefix="ac" %>
<asp:Label runat="server" ID="lblHello" />
<asp:Panel runat="server" DefaultButton="lbHello">
    First name: <asp:TextBox runat="server" ID="txtFirstName" />
    <ac:LinkButtonDefault ID="lbHello" runat="server" Text="Click me" 
        OnClientClick="javascript:alert('hai');" OnClick="lbHello_Click" />
</asp:Panel>

My Clientside function doesn't work why? Any suggestion.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

好听的两个字的网名 2024-09-23 08:38:40

您可以采用添加属性来处理文本框的 onKeyPress 事件的简单方法,而不是使用自定义控件。这可以正确处理从文本框中按 Enter 并触发 LinkBut​​ton 的事件。这种方法的缺点是,任何 LinkBut​​ton OnClientClick 事件都不会在 Firefox 中触发,这与您链接到的博客文章中描述的问题有关。仅当用户实际用鼠标单击链接时才会触发它。但是,在 IE 中,它将从文本框和直接单击触发。

解决方案 #1 - 添加属性的代码如下:

protected void Page_Load(object sender, EventArgs e)
{
    txtFirstName.Attributes.Add("onKeyPress",
        "javascript:if (event.keyCode == 13) __doPostBack('" + lbHello.ClientID + "','')");
}

尝试一下,看看它是否符合您的需求。请记住我之前描述的限制。

现在,如果您希望消除上述限制,该博客文章中的一条评论显示了一种似乎可以正常工作的方法。我对其进行了修改,删除了 StringBuilder 并将其转换为 C#。

解决方案 #2 - 添加该功能并注册的代码如下:

protected void Page_PreRender(object sender, EventArgs e)
{
    string addClickFunctionScript = @"function addClickFunction(id) {
           var b = document.getElementById(id);
           if (b && typeof(b.click) == 'undefined')
             b.click = function() {
               var result = true;
               if (b.onclick) result = b.onclick();
               if (typeof(result) == 'undefined' || result)
                 eval(b.getAttribute('href'));
             }
         };";

    string clickScript = String.Format("addClickFunction('{0}');", lbHello.ClientID);

    Page.ClientScript.RegisterStartupScript(this.GetType(), "addClickFunctionScript", addClickFunctionScript, true);
    Page.ClientScript.RegisterStartupScript(this.GetType(), "click_" + lbHello.ClientID, clickScript, true);
}

页面标记 -
对于上述两种解决方案,页面标记是相同的:

<asp:Label runat="server" ID="lblHello" />
<asp:Panel ID="Panel1" runat="server" DefaultButton="lbHello">
    First name:
    <asp:TextBox runat="server" ID="txtFirstName" />
    <asp:LinkButton ID="lbHello" runat="server" Text="Click me" OnClick="lbHello_Click"                 
         OnClientClick="javascript:alert('Hello, World!');"/>
</asp:Panel>

在这两种情况下都不需要自定义控件来实现此类功能。保持简单。

Rather than use a custom control you could take the simple approach of adding an attribute to handle the textbox's onKeyPress event. This correctly handles pressing Enter from the textbox and triggering the LinkButton's event. The downside to this approach is that any LinkButton OnClientClick event will not be triggered in Firefox, which is related to the issue described in that blog post you linked to. It will only be triggered when the user actually clicks on the link with their mouse. However, in IE, it will trigger from both the textbox and from being clicked on directly.

Solution #1 - The code to add the attribute is as follows:

protected void Page_Load(object sender, EventArgs e)
{
    txtFirstName.Attributes.Add("onKeyPress",
        "javascript:if (event.keyCode == 13) __doPostBack('" + lbHello.ClientID + "','')");
}

Try that and see if it fits your needs. Just bear in mind the limitation I described earlier.

Now, if you want the above limitation to go away, one of the comments from that blog post showed an approach that appears to work correctly. I've modified it to get rid of the StringBuilder and converted it to C#.

Solution #2 - The code to add the function and register it is as follows:

protected void Page_PreRender(object sender, EventArgs e)
{
    string addClickFunctionScript = @"function addClickFunction(id) {
           var b = document.getElementById(id);
           if (b && typeof(b.click) == 'undefined')
             b.click = function() {
               var result = true;
               if (b.onclick) result = b.onclick();
               if (typeof(result) == 'undefined' || result)
                 eval(b.getAttribute('href'));
             }
         };";

    string clickScript = String.Format("addClickFunction('{0}');", lbHello.ClientID);

    Page.ClientScript.RegisterStartupScript(this.GetType(), "addClickFunctionScript", addClickFunctionScript, true);
    Page.ClientScript.RegisterStartupScript(this.GetType(), "click_" + lbHello.ClientID, clickScript, true);
}

Page Markup -
The page mark-up is the same for both of the aforementioned solutions:

<asp:Label runat="server" ID="lblHello" />
<asp:Panel ID="Panel1" runat="server" DefaultButton="lbHello">
    First name:
    <asp:TextBox runat="server" ID="txtFirstName" />
    <asp:LinkButton ID="lbHello" runat="server" Text="Click me" OnClick="lbHello_Click"                 
         OnClientClick="javascript:alert('Hello, World!');"/>
</asp:Panel>

In both cases a custom control is not needed to achieve this type of functionality. Keep it simple.

梦明 2024-09-23 08:38:40

看看这个

<asp:Panel runat="server" DefaultButton="lbHello">
    First name: <asp:TextBox runat="server" ID="txtFirstName" />
    <asp:LinkButton ID="lbHello" runat="server" Text="Click me" OnClick="lbHello_Click" />
</asp:Panel>

其余的答案可以在这里找到。它展示了如何解决 FireFox 出现的问题
http://kpumuk .info/asp-net/using-panel-defaultbutton-property-with-linkbutton-control-in-asp-net/

Take a look at this

<asp:Panel runat="server" DefaultButton="lbHello">
    First name: <asp:TextBox runat="server" ID="txtFirstName" />
    <asp:LinkButton ID="lbHello" runat="server" Text="Click me" OnClick="lbHello_Click" />
</asp:Panel>

The rest of the answer can be found here. It shows how to fix the problem that arises with FireFox
http://kpumuk.info/asp-net/using-panel-defaultbutton-property-with-linkbutton-control-in-asp-net/

忱杏 2024-09-23 08:38:40

另一种方法可能是像这样伪造 onclick 事件。

jQuery(document).ready(function () {
    jQuery('#<%= txtFirstName.ClientID %>').keypress(function (event) {
        if (event.keyCode == 13) {
            eval($('#<%=lbHello.ClientID %>').attr('href'));
        }
    });
});

缺点是您需要在所有需要创建默认按钮的地方使用它。

Another approach could be to fake the onclick event like this.

jQuery(document).ready(function () {
    jQuery('#<%= txtFirstName.ClientID %>').keypress(function (event) {
        if (event.keyCode == 13) {
            eval($('#<%=lbHello.ClientID %>').attr('href'));
        }
    });
});

The downside is that you need to use this for all places you need to make default buttons.

撩人痒 2024-09-23 08:38:40

请参阅此

页面上的链接按钮并将其设置为默认按钮,在 IE 中工作正常,但在 Mozila 中不行

我看来,虚拟按钮答案是最干净的解决方案。

See this

Link Button on the page and set it as default button, work fine in IE but not in Mozila

The dummy button answer is the cleanest solution IMO.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文