1

I have a control that should prompt the user to choose either the session's customerId or the page's old viewstate customerId. To do this the control has a validate in the code behind based on

On postback, how can I add a error message to validation summary?

When is step through the code I see the err.IsValid is set to false. But when I get to Page.IsValid and look at the validators it is set to true. Any information that can be provided to help me understand why this is not proceeding as I expect would be appreciated.

code behind

public partial class CustomerChanged : System.Web.UI.UserControl
{
    protected override void OnInit(EventArgs e)
    {
        base.OnInit(e);
        this.Page.PreLoad += Page_PreLoad;
    }

    void Page_PreLoad(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            ViewState.Add("CustID", Globals.CurrentCust.CustId);
        }
        if (IsPostBack)
        {
            if (Convert.ToInt32(ViewState["CustID"]) != Globals.CurrentCust.CustId)
            {
                btnOldCustId.Text = "Old CustID \n" + ViewState["CustID"].ToString();
                btnNewCustId.Text = "New CustID \n" + Globals.CurrentCust.CustId.ToString();
                btnOldCustId.OnClientClick = string.Format("return changeCustomer({0},'{1}');", ViewState["CustID"].ToString(), Globals.GeneralSiteUrl);

                System.Web.UI.WebControls.CustomValidator err = new System.Web.UI.WebControls.CustomValidator();
                err.IsValid = false;
                err.ErrorMessage = "The customer has changed.";
                Page.Validators.Add(err);
            }
        }
    }

    protected void Page_Load(object sender, EventArgs e)
    {            
        if (IsPostBack)
        {
            Page.Validate();
            if (!Page.IsValid)
            {                    
                ScriptManager.RegisterStartupScript(this, this.GetType(), "CustomerChangedModalDialog", "ShowCustomerChangedModalDialog();", true);
            }
        }
    }
    protected void btnNewCustId_Click(object sender, EventArgs e)
    {
        Response.Redirect(Request.RawUrl);
    }
}
Community
  • 1
  • 1
user1161495
  • 77
  • 1
  • 2
  • 10

1 Answers1

2

You can add an error message to the validation summary using the following:

if (IsPostBack)
{
    Page.Validate();
    var valid = CustomValidate();

    if(valid && Page.IsValid)
    {
    }               
}

protected bool CustomValidate()
{
     var valid = true;
     ///do your validation here

     var validator = new CustomValidator();
     validator.IsValid = false;
     valid = validator.IsValid;
     Validator.ErrorMessage = "Error....";        
     this.Page.Validators.Add(validator);
     return valid;
}
Alex Angas
  • 59,219
  • 41
  • 137
  • 210
Dan Hunex
  • 5,172
  • 2
  • 27
  • 38
  • 1
    Thanks for the quick response but I don't see a functional difference in what I have and what you suggest. – user1161495 Jan 10 '13 at 16:51
  • I m sorry I miss the same part....but put the above thing in a method and call it after you did page.validate. Page validate does validation and will reset the isvalid to true – Dan Hunex Jan 10 '13 at 16:58
  • http://stackoverflow.com/questions/3753904/page-isvalid-always-returning-true-with-validationgroup-and-dynamic-customvalida confirms what you have said about the order. I have tried it by moving Page.Validate() to the line before I create the validator. Page.IsValid can be false now. But I don't get the modal. Maybe I should make another question for that. – user1161495 Jan 10 '13 at 17:52