skip to Main Content

I have two forms in my Default.aspx

<div class="form">
    <form id="WordFilterForm" >
        <label for="inputString">Enter a string:</label>
        <input type="text" id="inputString" name="inputString" required="required" />
        <br />
        <asp:Button ID="WordFilterButton" runat="server" Text="Filter" OnClick="WordFilterButton_Click" />

    </form>
</div>

<div class="WordCountContainer">
    <form id="WordCountForm">
         <label for="inputText">Enter a large string:</label>
         <textarea id="inputText" name="inputText" rows="10" cols="50" required="required"></textarea>
         <br />
        <asp:Button ID="WordCountButton" runat="server" Text="Count" OnClick="WordCountButton_Click" />
            </form>
<div>

WordFilterButton_Click and WordCountButton_Click are both defined with the protected signature in Default.aspx.cs.

Issue is, whenever the Count button is clicked in the second form (form id="WordCountForm"), nothing happens and the WordCountButton_Click doesn’t get executed according to the debugger.

However, the same button works if I put it in the first form.

Any ideas why the second form button wont execute the OnClick function?

2

Answers


  1. You are missing the runat="server" tag for both of the forms.

    In fact, if you don’ add that tag, you should receive an error like this:

    enter image description here

    So, its not at all clear why you not receiving above error.

    And, since you can’t nest form tags, then you can only have one per page.

    And since you REALLY only can have one "form" tag per page, then why do you have 2,and what is the goal of having 2?

    (its not allowed anyway).

    So, what is the goal here, and why are you attempting to have 2 form tags in a page?

    I fail to see say why just a simple div to layout or separate the sections you want will and would not suffice here.

    Using a "form" tag without runat=server not allowed, but then again, I don’t see the goal here at all.

    Perhaps you are confusing the "form" tag with that of formview tag?

    Dump the multiple use of form tag, don’t use them, just ALWAYS use the defaulted one form tag that VS creates for you, and place your content inside of that one form tag.

    Login or Signup to reply.
  2. The ASP.NET page can only have a single form tag with the (runat="server") attribute to make the event handlers work. So, it is necessary to add the aforementioned attribute to the first form, move all the server-side controls onto it, and remove the second form to properly implement a form/postback within an ASP.NET WebForms app.

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