Solved

Custom Validator firing, but is not displaying ErrorMessage

Posted on 2008-10-28
9
2,300 Views
Last Modified: 2012-06-21
Custom Validator firing, but is not displaying ErrorMessage
0
Comment
Question by:knowlton
  • 5
  • 4
9 Comments
 
LVL 13

Expert Comment

by:sm394
ID: 22827145
Could you please post your code
0
 
LVL 5

Author Comment

by:knowlton
ID: 22827172
CODE BEHIND:



using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using OrangeSoda.Data;
using AjaxControlToolkit;
using System.Data;
 
public partial class UserControls_Workflow_AddAccountUser : System.Web.UI.UserControl
{
 
	public event EventHandler AddedNewUser;
 
 
  #region - Constants -
 
  /// <summary>
  /// ID of the Popup extender for the control.
  /// </summary>
  private const string POPUP_EXTENDER_ID = "AddAccountUserPopupExtender";
 
  /// <summary>
  /// Name of the CSS class to use as the popup background.
  /// </summary>
  private const string POPUP_BACKGROUND_CSS = "popupPanelDefaultBackground";
 
  #endregion - Constants -
  #region - Properties -
 
  /// <summary>
  /// Gets or sets the width of the control in pixels.
  /// </summary>
  public int Width
  {
    get { return (int)this.ControlPanel.Width.Value; }
    set { this.ControlPanel.Width = new Unit(value); }
  }
 
  /// <summary>
  /// Gets or set the id of the control that activate the Modal popup.
  /// </summary>
  public string PopupActivationControlID { get; set; }
 
  #endregion - Properties -
  #region - Events -
 
  /// <summary>
  /// Notifies that an account needs to be updated.
  /// </summary>
  public event UpdateAccountEventHandler UpdateAccount;
 
  #endregion - Events -
  #region - Event Handlers -
 
  #region - Page -
 
  protected void Page_Load(object sender, EventArgs e)
  {
    if (null != WorkflowSessionManager.CurrentContext.Workitem)
      AccountIDLabel.Text = WorkflowSessionManager.CurrentContext.Workitem.AccountID.ToString();
 
    this.CreatePopup();
    this.PopulateAccountUserRoles();
 
    //ScriptManager.GetCurrent(this.Page).RegisterPostBackControl(this.AddUserButton);
  }
 
  #endregion - Page -
  #region - Button -
 
  protected void AddUserButton_Click(object sender, EventArgs e)
  {
		if (Page.IsValid)
		{
			AccountUser accountUser = new AccountUser();
			accountUser.ID = 0;
			accountUser.FirstName = FirstNameTextBox.Text;
			accountUser.LastName = LastNameTextBox.Text;
			accountUser.Password = PasswordTextBox.Text;
			accountUser.Username = new OSMailAddress(UsernameTextBox.Text);
			//accountUser.Role = (RoleType)int.Parse(au_roleDropDownList.SelectedValue);
			accountUser.UserLevel.IntValue = int.Parse(RoleDropDownList.SelectedValue);
			accountUser.Teams.Add((int)Team.TeamEnum.EXTERNAL, new Team(Team.TeamEnum.EXTERNAL));
			//if (accountUser.Role == RoleType.Primary)
			if (accountUser.UserLevel.LevelValue == PermissionLevel.Level.STRATEGIST)
				accountUser.IsPrimary = true;
			else
				accountUser.IsPrimary = false;
			if (!string.IsNullOrEmpty(HomePhoneTextBox.Text))
				accountUser.HomePhone = HomePhoneTextBox.Text;
			if (!string.IsNullOrEmpty(CellPhoneTextBox.Text))
				accountUser.CellPhone = CellPhoneTextBox.Text;
 
			Account account = null;
 
 
			account = WorkflowSessionManager.CurrentContext.Account;
 
			if (account.Users == null)
				account.Users = new System.Collections.ObjectModel.Collection<AccountUser>();
			account.Users.Add(accountUser);
 
			if (null != this.UpdateAccount)
				this.UpdateAccount(this, new UpdateAccountEventArgs(account));
 
 
			this.FirstNameTextBox.Text = String.Empty;
			this.LastNameTextBox.Text = String.Empty;
			this.UsernameTextBox.Text = String.Empty;
			this.PasswordTextBox.Text = String.Empty;
			this.RoleDropDownList.SelectedIndex = 0;
			this.HomePhoneTextBox.Text = String.Empty;
			this.CellPhoneTextBox.Text = String.Empty;
 
			if (AddedNewUser != null)
			{
				AddedNewUser(this, null);
			}
		}
  }
 
  #endregion - Button -
 
  #endregion - Event Handlers -
  #region - Private Methods -
 
  /// <summary>
  /// Creates the popup extender for the modal dialog control.
  /// </summary>
  private void CreatePopup()
  {
    // Check to see if the control already exists.
    ModalPopupExtender popupExtender = this.FindControl(POPUP_EXTENDER_ID) as ModalPopupExtender;
 
    if (null == popupExtender)
    {
      popupExtender = new ModalPopupExtender();
      popupExtender.ID = POPUP_EXTENDER_ID;
      popupExtender.PopupControlID = this.ControlPanel.ID;
      popupExtender.CancelControlID = this.CancelButton.ID;
      popupExtender.DropShadow = true;
      popupExtender.BackgroundCssClass = POPUP_BACKGROUND_CSS;
      popupExtender.RepositionMode = ModalPopupRepositionMode.RepositionOnWindowResizeAndScroll;
      this.Controls.Add(popupExtender);
    }
 
    // Remove the popup extender if there is no control
    // to activate the popup otherwise set the activating
    // control for the extender.
    if (null == this.PopupActivationControlID)
      this.Controls.Remove(popupExtender);
    else
      popupExtender.TargetControlID = this.PopupActivationControlID;
  }
 
  private void PopulateAccountUserRoles()
  {
    DataSet ds = UserManager.SelectTeamPermissionProfiles((int)Team.TeamEnum.EXTERNAL, (int)PermissionLevel.Level.STRATEGIST, false);
    this.RoleDropDownList.DataSource = ds;
    this.RoleDropDownList.DataTextField = "display_name";
    this.RoleDropDownList.DataValueField = "user_level_id";
    this.RoleDropDownList.DataBind();
  }
 
  #endregion - Private Methods -
 
 
//THIS DOES FIRE -- BUT NO MESSAGE
	protected void CustomValidatorUserName_ServerValidate( object source, ServerValidateEventArgs args )
	{
		args.IsValid = UserManager.IsUsernameAvailable(UsernameTextBox.Text);
 
	}
}

Open in new window

0
 
LVL 5

Author Comment

by:knowlton
ID: 22827175
MARKUP:


<%@ Control Language="C#" AutoEventWireup="true" CodeFile="AddAccountUser.ascx.cs" Inherits="UserControls_Workflow_AddAccountUser" %>
 
<asp:Panel ID="ControlPanel" Width="300px" CssClass="popupPanelDefaultCSS" runat="server">
  <table width="100%" style="padding: 10px;">
    <tr>
      <td colspan="2" align="center"><h2>Add an Account User</h2></td>
    </tr>
    <tr>
      <td colspan="2"><hr /></td>
    </tr>
    <tr>
      <td style="width: 125px;">Account ID:</td>
      <td align="left" style="width: auto; padding: 5px;"><asp:Label ID="AccountIDLabel" Width="100%" runat="server" /></td>
    </tr>
    <tr>
      <td style="width: 125px;">First Name:</td>
      <td align="left" style="width: auto; padding: 5px;">
        <asp:TextBox ID="FirstNameTextBox" Width="100%" runat="server" MaxLength="16" />
        <asp:RequiredFieldValidator ID="FirstNameValidator" runat="server" ControlToValidate="FirstNameTextBox"
          Display="Dynamic" ErrorMessage="First Name is Required." ValidationGroup="accountUsersValidation"></asp:RequiredFieldValidator>
      </td>
    </tr>
    <tr>
      <td style="width: 125px;">Last Name:</td>
      <td align="left" style="width: auto; padding: 5px;">
        <asp:TextBox ID="LastNameTextBox" Width="100%" runat="server" MaxLength="16" />
        <asp:RequiredFieldValidator ID="LastNameValidator" runat="server" ControlToValidate="LastNameTextBox"
          Display="Dynamic" ErrorMessage="Last Name is Required." ValidationGroup="accountUsersValidation">
        </asp:RequiredFieldValidator>
      </td>
    </tr>
    <tr>
      <td style="width: 125px;">Username: </td>
      <td align="left" style="width: auto; padding: 5px;">
      <asp:TextBox ID="UsernameTextBox" Width="100%" runat="server" MaxLength="150" />
          <asp:RequiredFieldValidator ID="UserNameRequiredValidator" runat="server" ControlToValidate="UsernameTextBox"
            Display="Dynamic" ErrorMessage="Username is Required." ValidationGroup="accountUsersValidation">
          </asp:RequiredFieldValidator>
          <asp:RegularExpressionValidator ID="UserNameValidator" runat="server" ControlToValidate="UsernameTextBox"
            Display="Dynamic" ErrorMessage="Incorrect Email Format." ValidationExpression="\w+([_+.'-]\w+)*@\w+([.-]\w+)*\.\w+([.-]\w+)*"
            ValidationGroup="accountUsersValidation">
          </asp:RegularExpressionValidator>
      	<asp:CustomValidator ID="CustomValidatorUserName" runat="server" 
					ControlToValidate="UsernameTextBox" Display="Dynamic" 
					EnableClientScript="False" ErrorMessage="Username already taken" 
					onservervalidate="CustomValidatorUserName_ServerValidate" 
					ValidationGroup="accountUsersValidation"></asp:CustomValidator>
      </td>
    </tr>
    <tr>
      <td style="width: 125px;">Password: </td>
      <td align="left" style="width: auto; padding: 5px;">
        <asp:TextBox ID="PasswordTextBox" Width="100%" runat="server" MaxLength="16" />
        <asp:RequiredFieldValidator ID="PasswordRequiredValidator" runat="server" ControlToValidate="PasswordTextBox"
          Display="Dynamic" ErrorMessage="Password is Required." ValidationGroup="accountUsersValidation">
        </asp:RequiredFieldValidator>
      </td>
    </tr>
    <tr>
      <td style="width: 125px;">Role:</td>
      <td align="left" style="width: auto; padding: 5px;"><asp:DropDownList ID="RoleDropDownList" Width="100%" runat="server">
      </asp:DropDownList></td>
    </tr>
    <tr>
      <td style="width: 125px;">Home Phone: </td>
      <td align="left" style="width: auto; padding: 5px;">
      <asp:TextBox ID="HomePhoneTextBox" Width="100%" runat="server" MaxLength="20" />
          <asp:RegularExpressionValidator ID="HomePhoneValidator" runat="server" ControlToValidate="HomePhoneTextBox"
            Display="Dynamic" ErrorMessage="Not a valid Phone." ValidationExpression="\(?([0-9]{3})\)?[-. ]?([0-9]{3})[-. ]?([0-9]{4})"
            ValidationGroup="accountUsersValidation">
          </asp:RegularExpressionValidator>
      </td>
    </tr>
    <tr>
      <td style="width: 125px;">Cell Phone: </td>
      <td align="left" style="width: auto; padding: 5px;">
      <asp:TextBox ID="CellPhoneTextBox" Width="100%" runat="server" MaxLength="20" />
          <asp:RegularExpressionValidator ID="CellPhoneValidator" runat="server" ControlToValidate="CellPhoneTextBox"
            Display="Dynamic" ErrorMessage="Not a valid Phone." ValidationExpression="\(?([0-9]{3})\)?[-. ]?([0-9]{3})[-. ]?([0-9]{4})"
            ValidationGroup="accountUsersValidation">
          </asp:RegularExpressionValidator>
      </td>
    </tr>
    <tr>
      <td colspan="2" align="right" style="padding: 5px;">
        <asp:Button ID="AddUserButton" Text="Add User" OnClick="AddUserButton_Click" runat="server" ValidationGroup="accountUsersValidation"/>&nbsp;&nbsp;
        <asp:Button ID="CancelButton" Text="Cancel" runat="server" />
      </td>
    </tr>
  </table>
</asp:Panel>

Open in new window

0
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 5

Author Comment

by:knowlton
ID: 22827184
IN the CODE BEHIND at the bottom, notice:

//THIS DOES FIRE -- BUT NO MESSAGE
      protected void CustomValidatorUserName_ServerValidate( object source, ServerValidateEventArgs args )
      {
            args.IsValid = UserManager.IsUsernameAvailable(UsernameTextBox.Text);
 
      }
0
 
LVL 13

Expert Comment

by:sm394
ID: 22827263
try that
<asp:ValidationSummary ValidationGroup="accountUsersValidation" ID="someid" runat="server"  HeaderText="Please correct the following :" />
0
 
LVL 13

Expert Comment

by:sm394
ID: 22827269
just add that on top of the panel
0
 
LVL 5

Author Comment

by:knowlton
ID: 22827281
Not sure where you are heading ...  but I don't want to add a Validation Summary to the control.

Right now the Validation controls are showin-up next to the controls they are validating.  All of them are working except my CustomValidator.  Like I said, the method does fire ... but no message shows up next to the username text box.

0
 
LVL 13

Expert Comment

by:sm394
ID: 22827313
what this returns
UserManager.IsUsernameAvailable(UsernameTextBox.Text);
when it returns false message should displayed
0
 
LVL 13

Accepted Solution

by:
sm394 earned 500 total points
ID: 22827345
try placing error message there
ValidationGroup="accountUsersValidation">Error Message</asp:CustomValidator>
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

789 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question