Reputation: 490
I noticed a weird problem in C# and it's been bothering my head for weeks, so I thought I just put this question out there. So my question is basically how does C# handle multiple client <form>
's inside the server form <form runat="server">
? I've been writing a web site and I've encountered a very strange problem.
So here's the essential breakdown to recreate this problem.
1) Have a simple javascript method with an alert inside that fires if it is called.
eg: function tester() {
alert("adf");
}
2) Open a brand new Web Forms project in Visual Studios 2010. (It should have a <form runat="server">
already created inside for you.)
eg: <form runat="server"> <form/>
You can only have one of these.
3) Now inside this form tag, write an empty form tag that should theoretically do nothing:
eg: <form action=""><form/>
4) Now Create another form right after this form that calls the javascript method.
eg: <form action="javascript:tester()" >
<button type="submit" >Search</button>
</form>
5) Run this code now, and you should see the alert pop up after you press the button.
6) Now take out the empty form tags:
Delete: <form action=""><form/>
7) Run code again, and now you should find that your javascript call is not firing anymore, and it would rather be refreshing the page every time you press the button.
So this is my problem that has been bothering me, and I hope some guru may have an answer for me soon, for this has been bothering my head for a long time. I have no idea why it works with two forms, and why it doesn't with just one form. Hope someone can answer this soon. Thanks so much in advance!!!
Some Helpful Information:
According to this page, I can have multiple "client" based form tags and that shouldn't be a problem...
Why you can't have a page with multiple server-side Form tags?
...however... the problem somehow still manifests itself.
Upvotes: 1
Views: 1077
Reputation: 25763
Having a form
tag within a form
tag is against the html specifications, and thus any result would be undefined behavior.
For your particular problem, can you not do this?
<button onclick="tester();">Search</button>
Upvotes: 2