asp.net custom control with form field validation issue
I have created a custom asp .net control some fields have validation controal associated with them. The problem arised when I declare more than one initialization on a page. When I hit submit on one of the intialized control, form validation occur on all other control that were declared. Due to this issue I cannot submit a form. Here is the code
<%@ Control Language="C#" AutoEventWireup="true" CodeFile="FinancialAdvisorHelp.ascx.cs" Inherits="FinancialAdvisorHelp" %>
<table width="316" border="0" cellpadding="0" cellspacing="0">
<tr>
<td colspan="2" >
<asp:RequiredFieldValidator
ID="RequiredFieldValidatorMember" runat="server"
ErrorMessage="Pleae enter member name. " ControlToValidate="TextBox_Name"></asp:RequiredFieldValidator>
<asp:RequiredFieldValidator ID="RequiredFieldValidatorEmail" runat="server" ErrorMessage="Please enter email. " ControlToValidate="TextBox_email"></asp:RequiredFieldValidator>
<asp:RegularExpressionValidator ID="RegularExpressionValidatorDarryEmail" runat="server"
ControlToValidate="TextBox_email" ValidationExpression=".*@.*\..*"
ErrorMessage="<br>
Invalid Email."> </asp:RegularExpressionValidator>
</td>
</tr>
<tr>
<td width="165"><p style="font-family:Palatino Linotype; font-size:12px; margin:0; padding:0;">Member Name*</p>
<asp:TextBox ID="TextBox_Name" runat="server" CausesValidation="True"></asp:TextBox>
</td>
<td width="151" align="right"><p style="font-family:Palatino Linotype; font-size:12px; margin:0 0 0 4px; padding:0; text-align:left;">E-Mail Address*</p>
<asp:TextBox id="TextBox_email" runat="server" CausesValidation="True"></asp:TextBox>
</td>
</tr>
<tr>
<td colspan="2" align="right"><p style="font-family:Palatino Linotype; font-size:12px; margin:0; padding-right:50px;"><br />
Telephone Number</p>
<asp:TextBox id="TextBox_phone" runat="server" CausesValidation="True"></asp:TextBox>
</td>
</tr>
<tr>
<td colspan="2"><p style="font-family:Palatino Linotype; font-size:12px; margin:0; padding:0;">Ask Darryl your question</p>
<asp:TextBox ID="TextBox_question" runat="server" Rows="7" Columns="48"
style="width:310px" TextMode="MultiLine"></asp:TextBox>
</td>
</tr>
<tr>
<td colspan="2"><p style="font-size:10px; margin:0; padding:0; float:le开发者_开发百科ft; width:240px; line-height:12px;">Note: you should receive a response within two (2) business days.<br />
*Required information.</p>
<asp:ImageButton ID="ImageButton_Advisor" runat="server" alt="submit"
width="63" height="18"
style="border:0; padding:0; margin:10px 0 0 0; float:right;" ImageUrl="./images/investments/submit.gif"
/>
<asp:HiddenField ID="HiddenFieldAdvisorEmail" Value="" runat="server" />
</td>
<tr><td colspan="2">
</td></tr>
</tr>
</table>
Use the ValidationGroup attribute on all form elements that cause validation including the submit button. When validation is triggered, it will only check other elements with the same ValidationGroup specified.
To make sure it is unique across each instance of your control on the page use your UserControl's ID as the validation group in the code behind.
RequiredFieldValidatorEmail.ValidationGroup = this.ClientID;
More information can be found here: http://www.dotnet-guide.com/validationgroups.html
I'm having a hard time deciphering your question.
- Validation Groups
It sounds like you might want to take advantage of validation groups . When you have multiple triggers that should validate different controls, this is what to use.
- Check your Validators and Custom Controls
If you are still having issues, when validators have errors and you trigger validation, it appears that "nothing happens" (what really is happening is that the validators error out and the page kind of just stops). It can also be possible that your custom controls have issues.
- Check your Validators and ID's
The problem arised when I declare more than one initialization on a page
What do you mean by this? I wonder if you are declaring controls dynamically in your code? This could also be your problem. Each ASP.NET control (including validators, custom controls, ... ) needs it's own unique ID. You can also have more than one validator checking a single control, but a single validator can't validate multiple controls.
精彩评论