set linkbutton as default button for asp:panel in asp.net [duplicate]

风格不统一 提交于 2019-11-29 11:44:53

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.

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/

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.

Steven Stefanik

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!