views:

61

answers:

1

Hi, I have some fields to be validated on the server (CustomValidator) which is in a separate user control and my Submit button is in the page not in the user control.

What I have done is, I placed all my Validation Methods in the code behind of my usercontrol (ascx.cs) and the page validation is in the code behind of the page (aspx.cs). I put them in the same ValidationGroupName (fields and Submit button). usercontrol (ascx) is a child of my page (aspx). I have already placed CauseValidation="true" to my Button and UseSubmitBehavior="true". The problem is it does not validate. What could be the problem in this?

Note: I cannot put the the Button to be part of the usercontrol.

Edit:

in my aspx page, click event of the button has this.Page.Validate(ValidationGroupName) and all of my validators are on the fields on a separate control (ascx) which is a child of the aspx page.

protected void Button1_Command(object sender, CommandEventArgs e)
{
    if(e.CommandName.Equals("Validate", StringComparison.Ordinal))
    {
        this.Page.Validate("MyValidationGroup");
        if(this.Page.IsValid)
        {
            // I'll change my View here.
        }
    }
}

My Button looks like

<asp:Button ID="Button1" runat="server" UseSubmitBehavior="true" CommandName="Validate" 
                    Text="Submit" OnCommand="Button1_Command" CausesValidation="true" ValidationGroup="MyValidationGroup" />

The above snippet is located in my aspx page.

I've tried to put the same button on the ascx page, it works fine. My thought is since the ascx page is under the aspx page. When the button event on aspx page is fired (Button1), the rest of the event in ascx page is not fired. I've tried to put breakpoints on the button event and validator events, if the button of the page (aspx) is the one I click, it will not stop on my validator events, if the button on the control (ascx) I click it will stop to the validator events. What could be the remedy for this?

+1  A: 

Have you tried calling ucName.Page.Validate(); in the button click event?

(where ucName is whatever you called the user control in the markup)

Edit: OK this simple code below works fine for me, the only way I broke the validation was setting my user control to visible = false; Can you post more of your code?

Parent page markup:

<uc:ucTest id="ucName" runat="server">
<asp:Button ID="btnTest" runat="server" Text="Test validation" onclick="btnTest_Click" />

code behind:

protected void btnTest_Click(object sender, EventArgs e)
{
    if (Page.IsValid)
    {
        Response.Write("Hi!");
    }
}

user control markup:

<asp:TextBox ID="txtDummy" runat="server"></asp:TextBox>
<asp:CustomValidator ID="CustomValidator1" runat="server" 
     ErrorMessage="*"
     ControlToValidate="txtDummy" 
     onservervalidate="CustomValidator1_ServerValidate">
</asp:CustomValidator>

code behind:

protected void CustomValidator1_ServerValidate(object source, ServerValidateEventArgs args)
{
    if (txtDummy.Text.Length > 0)
    {
        args.IsValid = false;
    }
}
JumpingJezza
ucName.Page.Validate() is the same as Validate() in the in the button click event
Tim Schmelter
I've tried this approach yet still it don't validate.
rob waminal
thanks, this works! the first time you answered already works but the problem at my end is that the fields are inside an updatepanel, I remove the update panel and it works fine now.. :D
rob waminal