Link to home
Start Free TrialLog in
Avatar of Tom Knowlton
Tom KnowltonFlag for United States of America

asked on

Custom Validator firing, but is not displaying ErrorMessage

Custom Validator firing, but is not displaying ErrorMessage
Avatar of Faizan Sarwar
Faizan Sarwar
Flag of United Kingdom of Great Britain and Northern Ireland image

Could you please post your code
Avatar of Tom Knowlton

ASKER

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

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

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);
 
      }
try that
<asp:ValidationSummary ValidationGroup="accountUsersValidation" ID="someid" runat="server"  HeaderText="Please correct the following :" />
just add that on top of the panel
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.

what this returns
UserManager.IsUsernameAvailable(UsernameTextBox.Text);
when it returns false message should displayed
ASKER CERTIFIED SOLUTION
Avatar of Faizan Sarwar
Faizan Sarwar
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial