• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2575
  • Last Modified:

Custom Validator firing, but is not displaying ErrorMessage

Custom Validator firing, but is not displaying ErrorMessage
0
Tom Knowlton
Asked:
Tom Knowlton
  • 5
  • 4
1 Solution
 
sm394Commented:
Could you please post your code
0
 
Tom KnowltonWeb developerAuthor Commented:
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
 
Tom KnowltonWeb developerAuthor Commented:
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
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 
Tom KnowltonWeb developerAuthor Commented:
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
 
sm394Commented:
try that
<asp:ValidationSummary ValidationGroup="accountUsersValidation" ID="someid" runat="server"  HeaderText="Please correct the following :" />
0
 
sm394Commented:
just add that on top of the panel
0
 
Tom KnowltonWeb developerAuthor Commented:
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
 
sm394Commented:
what this returns
UserManager.IsUsernameAvailable(UsernameTextBox.Text);
when it returns false message should displayed
0
 
sm394Commented:
try placing error message there
ValidationGroup="accountUsersValidation">Error Message</asp:CustomValidator>
0

Featured Post

The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

  • 5
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now