ModalPopup, web user control, and validators

Last post 07-06-2009 3:24 PM by iqueiroz. 2 replies.

Sort Posts:

  • ModalPopup, web user control, and validators

    07-03-2009, 12:29 PM
    • Member
      17 point Member
    • iqueiroz
    • Member since 05-29-2009, 3:55 PM
    • Posts 31

    I have a web user control, and each control within the web user control has some type of validatior. I also have a "Save" button, which fires the validators. 

    On my web form, I have two modalboxextenders, and the panel for each modalbox has an intance of the web user control above.

    Now suppose that I bring up the first modalbox and leave all controls blank. I then click on "Save," and as expected, the validation fails. So far, so good. The problem is that, the validation summary comes up twice, once for the ModalBox that failed validation, and once for the modalbox which is not visible!

    I'm not sure whether my description is good enough, so you can try to reproduce the behavior by using the simple example below:

    1 - Web User Control:

    <%@ Control Language="vb" AutoEventWireup="false" CodeBehind="WebUserControl1.ascx.vb" Inherits="WebSiteName.WebUserControl1" %>
    <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
    <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ControlToValidate="TextBox1" ErrorMessage="Field is required">*</asp:RequiredFieldValidator>
    <asp:Button ID="Button1" runat="server" Text="Save" /><br />
    <asp:ValidationSummary ID="ValidationSummary1" runat="server" ShowMessageBox="True" ShowSummary="False" />

    2 - Web Form:

    <%@ Page Language="vb" AutoEventWireup="false" CodeBehind="WebForm1.aspx.vb" Inherits="WebSiteName.WebForm1" %>

    <%@ Register Src="WebUserControl1.ascx" TagName="WebUserControl1" TagPrefix="uc1" %>


    <%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="cc1" %>

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

    <html xmlns="http://www.w3.org/1999/xhtml" >
    <head runat="server">
    <title>Untitled Page</title>
    </head>
    <body>
    <form id="form1" runat="server">
    <div>
    <asp:ScriptManager ID="ScriptManager1" runat="server">
    </asp:ScriptManager>

    <cc1:ModalPopupExtender ID="ModalPopupExtender1" runat="server" TargetControlID="btnModalBox1" OkControlID="Button1" PopupControlID="Panel1">
    </cc1:ModalPopupExtender>

    <asp:Button ID="btnModalBox1" runat="server" Text="Modal Box1" />

    <asp:Panel ID="Panel1" runat="server" Style="display:none;">

    <uc1:WebUserControl1 id="WebUserControl1_1" runat="server">
    </uc1:WebUserControl1><br /><br />
    <asp:Button ID="Button1" runat="server" Text="Close Modal 1" />

    </asp:Panel>


    <cc1:ModalPopupExtender ID="ModalPopupExtender2" runat="server" TargetControlID="btnModalBox2" OkControlID="Button2" PopupControlID="Panel2">
    </cc1:ModalPopupExtender>

    <asp:Button ID="btnModalBox2" runat="server" Text="Modal Box2" />

    <asp:Panel ID="Panel2" runat="server" Style="display:none;">

    <uc1:WebUserControl1 id="WebUserControl1_2" runat="server">
    </uc1:WebUserControl1><br /><br />
    <asp:Button ID="Button2" runat="server" Text="Close Modal 2" />

    </asp:Panel>

    </div>

    </form>
    </body>
    </html>


    Any ideas? Any help is appreciated.






  • Re: ModalPopup, web user control, and validators

    07-04-2009, 3:58 AM
    Answer
    • Participant
      1,066 point Participant
    • stevew1975
    • Member since 05-29-2009, 2:51 PM
    • Bristol, UK
    • Posts 189

    I think i know how to solve this.  You are saying that the second button on your form runs the validation on the 1st validation control.

    To solve this, you need to use validationgroup. 

    Within the usercontrol in the codebehind add a new property called ValidationGroup which is a string, within this, set all the controls within the web control to have this validation.

    i.e.

    this is C# so you need to do the vb
    
    public function ValidationGroup
    {
       set{
            Textbox1.ValidationGroup = value;
            RequiredFieldValidator1.ValidationGroup = value
            Button1.validationgroup = value
            }
    
    within your html on the form, there will now be a new property called validation group, set it to a different name for each call to your web user control, this will stop it linking.
    
    <uc1:WebUserControl1 id="WebUserControl1_1" runat="server" ValidationGroup="vgroup1">
    
    and 
    
    <uc1:WebUserControl1 id="WebUserControl1_2" runat="server" ValidationGroup="vgroup2">            
      





    Please mark this post as answered if it helped you!
  • Re: ModalPopup, web user control, and validators

    07-06-2009, 3:24 PM
    • Member
      17 point Member
    • iqueiroz
    • Member since 05-29-2009, 3:55 PM
    • Posts 31

    Thanks! This does work! I should have thought of it Embarassed !!

Page 1 of 1 (3 items)