Solved

Custom Validator firing, but is not displaying ErrorMessage

Posted on 2008-10-28
9
2,246 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
 
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
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 
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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

760 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now