设置LinkBut​​ton的作为ASP的默认按钮:在asp.net面板 [英] set linkbutton as default button for asp:panel in asp.net

查看:275
本文介绍了设置LinkBut​​ton的作为ASP的默认按钮:在asp.net面板的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能重复:结果
  <一href=\"http://stackoverflow.com/questions/938957/link-button-on-the-page-and-set-it-as-default-button-work-fine-in-ie-but-not-in\">Link页面上的按钮,将其设置为默认按钮,在IE浏览器工作正常,但不是在Mozila

如何设置的LinkBut​​ton为ASP默认按键:面板asp.net?我知道一个按钮可以设置为默认值,但我的应用程序使用linkbuttons所有形式。任何建议如何可以完成的。

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.

编辑:

现在我想这一点,
它的工作原理在Firefox很好,但我的JavaScript验证(即)我的LinkBut​​ton的onclient点击不起作用,为什么?

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

第二个编辑:

我可以让我的自定义LinkBut​​ton控件的工作,但不能能够钩的OnClientClick它。来源<一个href=\"http://kpumuk.info/asp-net/using-panel-defaultbutton-property-with-linkbutton-control-in-asp-net/\"相对=nofollow>用面板一defaultbutton属性与 - LinkBut​​ton的控制,在ASP网。

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.

和我这样做,

<%@ 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.

推荐答案

而不是使用自定义的控制,你可能需要增加一个属性来处理的简单的方法文本框的安其preSS 事件。这正确处理pressing <大骨节病>输入从文本框和触发的LinkBut​​ton 的事件。这样做的缺点做法是,任何的LinkBut​​ton 的OnClientClick 事件将不会在Firefox中,这是关系到这个问题被触发在您链接到博客帖子描述。当用户与他们的鼠标在链接上实际点击它只会被触发。然而,在IE浏览器,它会触发这两个文本框,被点击的直接。

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.

解决方案#1 - 的code以添加属性如下:

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.

现在,如果你想要,上述限制走开,<一个href=\"http://kpumuk.info/asp-net/using-panel-defaultbutton-property-with-linkbutton-control-in-asp-net/#comment-232193\">one从博客文章的评论的显示,似乎正常工作的方法。我已经修改了它摆脱的的StringBuilder ,并将其转换为C#。

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#.

解决方案#2 - 的code以添加功能并注册其计算方法如下:

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.

这篇关于设置LinkBut​​ton的作为ASP的默认按钮:在asp.net面板的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆