locked
Link button - UseSubmitbutton RRS feed

  • Question

  • User-1664485818 posted

    Hi folks, how do you get a link button to submit like a normal button i.e when a user hits the enter key on a keyboard?

    Wednesday, August 10, 2016 8:12 PM

Answers

  • User475983607 posted

    brucey

    Hi folks, how do you get a link button to submit like a normal button i.e when a user hits the enter key on a keyboard?

    A link button is nothing more than a hyperlink with a JavaScript handler.  Use  developer tools to look at the source code generated by the LinkButton service control.  Then simply subscribe to a button press and invoke the same JavaScript code that the Linkbutton uses.

    Edit: 13 is the enter button value.

    <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="LinkButtonEnter.aspx.cs" Inherits="TestingWeb.LinkButtonEnter" %>
    
    <!DOCTYPE html>
    
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title></title>
        <script src="//code.jquery.com/jquery-1.11.0.min.js"></script>
        <script type="text/javascript">
            $(function () {
                $(document).keypress(function (event) {
                    if (event.which == 13) {
                        __doPostBack('LinkButton1', '');
                    }
                });
            })
        </script>
    </head>
    <body>
        <form id="form1" runat="server">
        <div>
            <asp:LinkButton ID="LinkButton1" runat="server" OnClick="LinkButton1_Click">LinkButton</asp:LinkButton>
        </div>
        </form>
    </body>
    </html>
    

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, August 10, 2016 8:36 PM

All replies

  • User475983607 posted

    brucey

    Hi folks, how do you get a link button to submit like a normal button i.e when a user hits the enter key on a keyboard?

    A link button is nothing more than a hyperlink with a JavaScript handler.  Use  developer tools to look at the source code generated by the LinkButton service control.  Then simply subscribe to a button press and invoke the same JavaScript code that the Linkbutton uses.

    Edit: 13 is the enter button value.

    <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="LinkButtonEnter.aspx.cs" Inherits="TestingWeb.LinkButtonEnter" %>
    
    <!DOCTYPE html>
    
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title></title>
        <script src="//code.jquery.com/jquery-1.11.0.min.js"></script>
        <script type="text/javascript">
            $(function () {
                $(document).keypress(function (event) {
                    if (event.which == 13) {
                        __doPostBack('LinkButton1', '');
                    }
                });
            })
        </script>
    </head>
    <body>
        <form id="form1" runat="server">
        <div>
            <asp:LinkButton ID="LinkButton1" runat="server" OnClick="LinkButton1_Click">LinkButton</asp:LinkButton>
        </div>
        </form>
    </body>
    </html>
    

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, August 10, 2016 8:36 PM
  • User283571144 posted

    Hi brucey,

    brucey

    Hi folks, how do you get a link button to submit like a normal button i.e when a user hits the enter key on a keyboard?

    As far as I know, you could set HtmlForm.DefaultButton Property in form tag.

    HtmlForm.DefaultButton Property:

    Gets or sets the child control of the HtmlForm control that causes postback when the ENTER key is pressed.

    More details, you could refer to follow link and codes:

        <form id="form1" runat="server" defaultbutton="LinkButton1">
        <div>
            <asp:LinkButton ID="LinkButton1" runat="server" OnClick="LinkButton1_Click">LinkButton</asp:LinkButton>
        </div>
        </form>

    Code-behind:

            protected void LinkButton1_Click(object sender, EventArgs e)
            {
                Response.Write("111111111");
            }

    Link:https://msdn.microsoft.com/en-us/library/system.web.ui.htmlcontrols.htmlform.defaultbutton(v=vs.110).aspx

    Notice:Specifying a LinkButton control as a default button. Only Button and ImageButton controls are supported.

    Best Regards,

    Brando

    Thursday, August 11, 2016 6:19 AM