Ethan The Brave
Ethan The Brave

Reputation: 287

Asp.net Button Firing Other Button's Event

I have a set of options on an ASP.net page where the user may open up one of two lightboxes - either the "Add" lightbox or the "Edit" lightbox.

Each one has a small form for the user to fill out, and then they can hit a button at the bottom of the lightbox to hit submit.

I have the buttons set up within the lightboxes like so:

<asp:Button Id="btnSubmitAdd" runat="server" Text="Submit" OnClick="btnSubmitAdd_Click" />

... then later in the other lightbox...

<asp:Button Id="btnSubmitEdit" runat="server" Text="Submit" OnClick="btnSubmitEdit_Click" />

When i click the "Add" lightbox's submit button, everything behaves just fine.

When i click the "Edit" lightbox's submit button however, it fires "btnSubmitAdd_Click" instead of its own "...Edit_Click" event!

I have checked and re-checked all of the names and events and everything is set up correctly. Anyone have any ideas why this is happening?

Upvotes: 2

Views: 1716

Answers (1)

Ethan The Brave
Ethan The Brave

Reputation: 287

Thanks to @MikeGuthrie for leading me down the correct path!

The issue seems to be with asp.net defaulting buttons to type "submit" which submits the entire form, and apparently that means it simply hit the first button's event before the second.

I added have modified the buttons like so and things are working now:

<asp:Button Id="btnSubmitAdd" runat="server" Text="Submit" OnClick="btnSubmitAdd_Click" UseSubmitBehavior="false"/>

<asp:Button Id="btnSubmitEdit" runat="server" Text="Submit" OnClick="btnSubmitEdit_Click" UseSubmitBehavior="false"/>

Upvotes: 5

Related Questions