.NET two "forms" on a page and their validators - c#

I have got a page that has 2 "forms" (What I mean by a form here is a Panel consisting of: textboxes, validators and a button).
(I got 2 here because one of them is actually on the MasterPage, shown all the time)
The problem is when you try to submit to one of the form, it will validate the other form, which of course is blank and invalid.
How do you solve this problem?
Thank you.

Your problem can be solved with asp.net ValidationGroups.
http://weblogs.asp.net/scottgu/archive/2004/10/24/246945.aspx
Basically, you group the controls to be validated using a uniquely named validation group. Like so:
<asp:Textbox ID="txt" runat="server" />
<asp:RequiredFieldValidator id="rfv" runat="server" ControlToValidate="txt" ValidationGroup="masterGroup">* Required!</asp:RequiredFieldValidator>
<br />
<asp:Button id="btnSubmitMaster" runat="server" Text="Submit!" ValidationGroup="masterGroup" />
If you group your inputs like this, then assign the validation group to the control that submits the form, the inputs in the other validation groups won't be validated.

Related

How to handle two different forms in an asp.net web page?

I have a login page in my asp.net website (using C#) and it has a "login form" which has an email text box, a password text box and a login button.
In addition, I have a "search form" at the top of the web page which has a search text box and a search button.
*All of the controls are in the same form because of the asp.net limit for one runat="server" form.
The problem is that when I type something to search for and click ENTER (and not directly the button) it doesn't do anything, only runs the Page_Load again. Same thing when I click ENTER in the login section instead of directly on the login button.
I have tried different solutions but they were problematic because of the different functionality of the two "forms".
I have no idea how to solve this, any suggestions?
my web forms is a little rusty but I think something like this:
<asp:Panel ID="loginPanel" runat="server" DefaultButton="loginButton">
<%-- Login Stuff--%>
<asp:Button ID="loginButton" runat="server" />
</asp:Panel>
<asp:Panel ID="loginPanel" runat="server" DefaultButton="searchButton">
<%-- Search Stuff --%>
<asp:Button ID="searchButton" runat="server" />
</asp:Panel>
should work
Only one form with runat='server' available on aspx page. So, if you really need another form, you may use its without runat='server' and use for search jQuery ajax call.

Custom DotNetNuke module development form validation

I am using the Module Creator module within DotNetNuke to create a very basic contact form. The form works as intended, but I'm having trouble with the validation.
When one submits the form without properly filling out the required fields, it invokes the form validation and displays the appropriate error messages. However, if I'm not trying to fill out the form and instead am selecting admin features of DotNetNuke to administer the site, the form validation for my custom module fires and prevents me from using the core functionality...
How do I prevent my form from being submitted when DotNetNuke admin functionality is what is being clicked?
Thanks for any help provided.
Solution is ValidationGroup
Use the validationgroup on your controls that has to be checked and at the validationgroup to your button.
Control with Requiredfieldvalidator:
<asp:textbox id="tbName" runat="Server"/>
<asp:requiredfieldvalidator id="rfvName" controltovalidate="tbName" validationgroup="Save" errormessage="Enter your name."
runat="Server"/>
Button:
<asp:LinkButton ID="lbSave" ValidationGroup="Save" resourcekey="cmdAdd" runat="server" class="dnnPrimaryAction" OnClick="Onclick_lbSave" /></li>
Thank you JK84! The use of ValidationGroup on my required field validators did the trick.

Multiple forms on page asp.net

I have a problem with multiple forms on my asp.net website. On the Master page i have a small form for every page and on some pages i also want to have a form. I know it is not possible to have for form tags with runat=server, so i am searching for a solution.
Is it possible to deactivate the validators from one of the forms, so the other form can pass? Then i could manage the 2 forms together in the server-side c# code.
Is this possible or are there any other solutions u know?
you can give your validation control a group name like
<asp:RequiredFieldValidator ControlToValidate="TextBox2" ValidationGroup="Login" ID="RequiredFieldValidator2" runat="server" ErrorMessage="RequiredFieldValidator"></asp:RequiredFieldValidator>
and use that group name to the button you want to validate on click like
<asp:Button ID="Button1" runat="server" Text="Login" ValidationGroup="Login" />

use Javascript or C# to validate a webform

Would like to validate more than one control on one button click. I would like something to validate whether a textbox has contents if a checkbox is checked or not but the checkbox doesn't necessarily have to be checked and in that case I don't want to check the textbox. I tried validation group but each button needs to control the different groups and i need this all to be under one button.
I'm open to ideas of how to do this c#,javascript...etc. Heres some code: Button3 is the save which validates whether checkbox 1 is checked and if so textbox10 cant be empty. I have about four other instances of this but are independent of each other.
<asp:Button ID="Button3" runat="server" Height="24px"
Text="Save" Visible="False" Width="67px" Font-Bold="True"
causesvalidation="true"
validationgroup="required"
runat="Server" />
<asp:CheckBox ID="CheckBox1" runat="server"
oncheckedchanged="CheckBox1_CheckedChanged" Text=" Breach Letter Sent"
ValidationGroup="required" AutoPostBack="True" Enabled="False" />
You want to use the CustomValidator control which can validate both on the server and the client. There is an example in the docs here - http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.customvalidator.aspx
I would never do form validation in JavaScript. Believe it or not, but some people actually turn off JavaScript! Use validators to validate the field content. Of course this means a round trip to the server in most cases, but you get reliable and well integrated validation.
you can use validation with Ajax (in Ajax postback occures but you will not sense)

Handling the submit action of two TextBoxes

I have an ASP.net page.
That has an Ajax Toolkit Tab Control.
That has tabs.
That have custom ascx controls I wrote.
I have a text box that perform a search action. It is declared like this:
<asp:TextBox ID="txtPrereqSearch" runat="server"
ontextchanged="txtPrereqSearch_TextChanged"></asp:TextBox>
Nothing fancy. This format has been working for months. There's no submit button. It just posts back when I hit enter. The problem appeared when I added a second custom control using the same type of feature. Now browsers don't postback when I type something in either of these textboxes and press enter.
It seems that browsers have a default way of handling one textbox in one form, but that behavior changes when the number reaches two.
Is there an easy way around this? I guess I can create a hidden submit button but it seems like there is probably a better way to deal with this when the functionality is in two separate custom controls.
Your feedback is appreciated!
Check this out: http://www.allasp.net/enterkey.aspx
The default behavior with no submit button seems to depend on the browser, and the behavior can indeed depend on the number of input controls.
I would add hidden "submit" button (e.g. style="display:none;") which should ensure that it always gets submitted.
The answer was a little different than I expected, but philosophically like my original idea that #jamietre reinforced.
I had to surround the controls with an <asp:Panel> tag with a DefaultButton attribute. A-like-a so:
<asp:Panel ID="ButtonPanel" runat="server" DefaultButton="btnSubmit">
<asp:Label ID="Label1" runat="server" Text="Course:"></asp:Label>
<asp:TextBox ID="txtPrereqSearch" runat="server"
ontextchanged="txtPrereqSearch_TextChanged"></asp:TextBox>
<asp:TextBoxWatermarkExtender ID="txtPrereq_TextBoxWatermarkExtender"
runat="server" Enabled="True" TargetControlID="txtPrereqSearch"
WatermarkCssClass="Watermark" WatermarkText="e.g., MATH201"></asp:TextBoxWatermarkExtender>
<asp:Button ID="btnSubmit" CssClass="InvisibleSubmit" runat="server" Text="Submit" OnClick="txtPrereqSearch_TextChanged"/>
</asp:Panel>

Categories