skip to Main Content

Suppose I have this form:

<form id="myForm" runat="server">
    <!-- Form content goes here -->
    <a type="submit">Submit</a>
</form>

I want to use the anchor link to submit the form,but cannot find how. I tried adding OnServerSubmit() as an attribute to the form, but it still is not hitting my backend function.

Any help would be appreciated.

2

Answers


  1. With pure JavaScript:

    <form id="myForm" runat="server">
        <!-- Form content goes here -->
        <a href="#" id="submitLink">Submit</a>
    </form>
    
    <script>
    document.getElementById("submitLink").onclick = function(){
        document.getElementById("myForm").submit();
    }
    </script>
    

    With jQuery:

    <form id="myForm" runat="server">
        <!-- Form content goes here -->
        <a href="#" id="submitLink">Submit</a>
    </form>
    
    <script>
    $("#submitLink").click(function(){
        $("#myForm").submit();
    });
    </script>
    

    If you use jQuery don’t forget to add the source files, example:

    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
    
    Login or Signup to reply.
  2. The "a" tag is what we call a HTML hyper link tag. Probably the "oldest" and most well known tag since the dawn of the internet and HTML!!!

    The REAL question is WHAT do you want to occur on submit? In other words, you NOT just submitting the page, but you are submitting the page FOR A VERY good reason!!!

    Do you want some code to run?

    Do you want to navigate to a different page?

    WHAT is your goal here? You don’t just submit a web page in some strange "vacuum" of a reason here, do you?

    In the case of an "a" tag, (a HTML hyper-link), you can most certainly have the hyper link trigger a post-back, and you would acheive that goal by adding a URL (href) to the a tag, say like this:

            <a href="WebForm4.aspx" ></a>
    

    So, now, when you click on the hyper-link it will of course post-back, and in fact navigate to the web page called WebForm4.

    another way (without a asp.net button – and hoper you have a BEYOND FANTASTIC reason for a post-back), then you could use a HTML button, say like this:

            <input id="ButtonTest" type="submit" value="Submit" />
    

    Once again the above will post the page, but this begs the original question:

    What do you want to do when you click that button in the first place?

    You can also use JavaScript code, and again have the page post-back, say like this:

            <input id="Button1" type="button" value="button"
                onclick="form.submit()"
                />
    

    However, at the end of the day?

    You NOT just going to submit a page, but ARE doing so for a reason. Since you going do a post-back, trigger a whole page round trip and re-load?

    Then in 99% of cases (at least in asp.net webforms land), then a REASON for the submit exists. And since a REASON exists?

    Then I suggest using a asp.net button, since then you can not only submit the page, but have a particular and chosen code behind stub to run for you.

    So, probably best in 99% of cases to drag + drop into the web page a asp.net button, and thus you have this:

            <asp:Button ID="cmdAdd" runat="server" Text="add invoice"
                OnClick="cmdAdd_Click"
                
                />
    

    And thus upon page post, then you have a code stub that runs based on above, eg this:

        protected void cmdAdd_Click(object sender, EventArgs e)
        {
            // code here to add the new invoice
        }
    

    So, like any coding endeavor?

    You don’t just "post-back" a page, but you have a particular reason for doing so, and since you have a good reason (we assume, right???) for the post-back, then in 99% of cases, you want that post-back for "something" to occur. And since you want something to occur, then in near all cases, you want to use a asp.net button, since then you get a nice neat clean code stub in code behind that runs upon the page post-back.

    So, the above shows multiple ways to trigger a post-back and do so without a asp.net button, but as noted, as the end of the day, in most cases you are posting back for A GOOD reason, and in thus in most cases what follows that goal and good reason is some code behind to run upon that page post back, and thus from that logic, what follows is to use a asp.net button.

    Of course there ARE other reasons for trigger of a page post back, but those reasons are on a case by case basis, and not a "general" code answer ,but a specific use case. You may well have that "special use case", but you not provided any good reason for not using a simple plain jane asp.net button.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search