locked
Button event not fired inside of user control when validation is enabled. RRS feed

  • Question

  • User-223976922 posted

    Hello,

    I have used a user control in a Tabpanel of Tabconatiner as below:


    <ajaxtoolkit:TabContainer ID="TabContainer1" runat="server" ActiveTabIndex="0"  AutoPostBack="true" OnActiveTabChanged="Tabs_ActiveTabChanged">
            <ajaxtoolkit:TabPanel runat="server" HeaderText="Week-2" ID="TabPanel1"  >
                <ContentTemplate>
                     <asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Conditional">
                     <ContentTemplate>
                        <uc1:Assessment_Input ID ="InputContainer1" runat="server" /> 
                     </ContentTemplate>
                     </asp:UpdatePanel>          
                </ContentTemplate>
            </ajaxtoolkit:TabPanel>
    ........

    The usercontrol inturn has a tabcontainer with a TabPanel like below:

    ....
    <ajaxtoolkit:TabPanel ID="TabPanel2" runat="server" HeaderText="Tab2">
            <ContentTemplate>
                X : <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
                <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ControlToValidate="TextBox1"
               CssClass="failureNotification" ErrorMessage="Please Select Value"
               ValidationGroup="tab2"></asp:RequiredFieldValidator>
                y: <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
                <asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server" ControlToValidate="TextBox2"
               CssClass="failureNotification" ErrorMessage="Please Select Value"
               ValidationGroup="tab2"></asp:RequiredFieldValidator>
                <asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" ValidationGroup="tab2" />
            </ContentTemplate>
        </ajaxtoolkit:TabPanel>

    ...

    Button1_click doesn't get fired when validationgroup is set, but if I disable by adding Causesvalidation=false, the event gets fired.

    Any inputs on this will be really helpful.







    Thursday, December 5, 2013 1:39 AM

Answers

All replies

  • User-933407369 posted

    hi udrjain ,

    i check out your code and test , but i don't come up your issue.  whether it has other Validation on the page ?

     i would suggest you try to check your other Validation,  if the page validation return false , the button cann't sumbit or postback.

    When the value of the CausesValidation property is set to true, you can also use the ValidationGroup property to specify the name of the validation group

    for which the Button control causes validation.

    Please refer to the link below:

    Button.CausesValidation Property

    http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.button.causesvalidation(v=vs.110).aspx

    Hope it helps you.

    Thursday, December 5, 2013 10:21 PM
  • User-223976922 posted

    Thanks for replying on this!

    I kinda analyzed this further and I think I know why its happening, lemme explain the issue clearly once again after my findings:

    I have a Tabcontainer Control with 2 Tabpanels.

    <asp:ScriptManager ID="ScriptManager1" runat="server"></asp:ScriptManager>

    <ajaxtoolkit:TabContainer ID="TabContainer1" runat="server"   
     ActiveTabIndex="0"  AutoPostBack="true" 
    OnActiveTabChanged="Tabs_ActiveTabChanged">

    <ajaxtoolkit:TabPanel runat="server" HeaderText="Week-2" ID="TabPanel1">

    <ContentTemplate>

         <asp:UpdatePanel runat="server" ID="UpdatePanel1"     UpdateMode="Conditional" >

               <ContentTemplate>

                        <uc1:Assessment_Input ID ="InputContainer1" runat="server" />  

                </ContentTemplate>

        </asp:UpdatePanel>           

    </ContentTemplate>

    </ajaxtoolkit:TabPanel>

    <ajaxtoolkit:TabPanel ID="TabPanel2" runat="server" HeaderText="Week0">

     <ContentTemplate>

          <asp:UpdatePanel runat="server" ID="UpdatePanel2" UpdateMode="Conditional">

               <ContentTemplate>

                         <uc1:Assessment_Input ID ="InputContainer2" runat="server" />  

               </ContentTemplate>

          </asp:UpdatePanel>    

    </ContentTemplate>

    </ajaxtoolkit:TabPanel>

    </ajaxtoolkit:TabContainer>

    Each of the Tabpanel is embedding  the same user control "uc1:Assessment_Input" in it.

    user control "uc1:Assessment_Input" is basically a form with few controls and a Submit button with Validationgroup set to "X",the same validationgroup is set on all the controls in the form.

    The problem is that my submit button doesnt fire an event even after filling all the fileds in the TabPanel1 which is "Week-2", as it trying to validate the fields in next tab "Week0" which is still empty.

    To Confirm this, I removed my Week0 TabPanel, then the button event in Week-2 TabPanel works as ecpected.

    or Incase I fill the fields in Week-2 Tabpanel and then fill the fields in TabPanel Week0, then the button event works again as expected.
     
    I have seen some issues w.r.t this, but which suggested  different validation  groups to be set for diff tabs, but in my case since same usercontrol is used in differnt tabpanels, How can i solve this issue?




     

     

    Thursday, December 5, 2013 11:55 PM
  • User-227760790 posted

    hi  udrjain,

    I‘'m sorry for not solving your issue.

    Until now I have not been able to reproduce your problem. have your check out the project in other machines?

    I assume that your required field validator fileds may be firing and preventing you from post back.

    Modify Display for required field validator as Static or Dynamic and add some text to required field validator or a validation summary control and check.

    You'd better provide a simple demo, In order to help us to understand your issue better. Thanks.

    Friday, December 6, 2013 3:53 AM
  • User-223976922 posted

    Thanks galeny for looking into this issue!

     

    Here is my Code:

    NewAssessment.aspx

    <h2>Assessments Summary</h2>

    <table>

    <tr>

    <td>Patient ID :</td>

    <td><asp:Label ID="lblPID" runat="server" Text=""></asp:Label></td>

    </tr>

     <tr>

    <td>Patient Name :</td>

    <td><asp:Label ID="lblPName" runat="server" Text=""></asp:Label></td>

    </tr>

     </table>

    <asp:ScriptManager ID="ScriptManager2" runat="server"></asp:ScriptManager>

    <ajaxtoolkit:TabContainer ID="TabContainer1" runat="server"    ActiveTabIndex="0" AutoPostBack="true" OnActiveTabChanged="Tabs_ActiveTabChanged">

    <ajaxtoolkit:TabPanel runat="server" HeaderText="Week-2" ID="TabPanel5">

     <ContentTemplate>

     <asp:UpdatePanel runat="server" ID="UpdatePanel1"     UpdateMode="Conditional" >     

    <ContentTemplate>

           <uc1:Assessment_Input ID ="Assessment_Input1" runat="server" /> 

     </ContentTemplate>

     </asp:UpdatePanel>          

     </ContentTemplate>

    </ajaxtoolkit:TabPanel>

    <ajaxtoolkit:TabPanel ID="TabPanel6" runat="server" HeaderText="Week0">

    <ContentTemplate>

    <asp:UpdatePanel runat="server" ID="UpdatePanel2" UpdateMode="Conditional">

    <ContentTemplate>

              <uc1:Assessment_Input ID ="Assessment_Input2" runat="server" /> 

    </ContentTemplate>

    </asp:UpdatePanel>   

    </ContentTemplate>

    </ajaxtoolkit:TabPanel>

    </ajaxtoolkit:TabContainer>

    The user control is defined as- Assessment_Input.ascx

    <ajaxtoolkit:TabContainer ID="TabContainer1" runat="server" ActiveTabIndex="0" AutoPostBack="true">

    <ajaxtoolkit:TabPanel runat="server" HeaderText="Test" ID="TabPanel1">

    <ContentTemplate>

    X :<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>

    <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ControlToValidate="TextBox1" CssClass="failureNotification" ErrorMessage="Please Select Value"  Display="Static" ValidationGroup="tab2"></asp:RequiredFieldValidator>

     Y:<asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>

    <asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server" ControlToValidate="TextBox2"

    CssClass="failureNotification" ErrorMessage="Please Select Value" Display="Static" ValidationGroup="tab2"></asp:RequiredFieldValidator>

    <asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" ValidationGroup="tab2" />         

    </ContentTemplate>

    </ajaxtoolkit:TabPanel>

    </ajaxtoolkit:TabContainer>

    For testing, I have the cs code with:

    protected void Button1_Click(object sender, EventArgs e)  

      {

    int i = 0;  

      }

    on running this code, the o/p webpage is like I have a Tabcontainer with Week-2 and Week0 Tabpanels, and each of them inturn has a tabcontainer with Test Tabpanel.

    Now, on submitting the empty form On week-2, I get the required validator msgs for both textboxes, I fill some value in both text box and press Button again and there is no postback, my breakpoint in Button1_Click doesnt get hit.

    I switch to next Tab Week0, on empty form I get the required validator msgs for both textboxes.But once I fill the values in text box and press Button again, the breakpoint in Button1_Click gets hit.

    So its like my Validation is running across Tabs Week-2 & Week0.

    Hope my question is clear now.

    Thanks!

     

     

    Friday, December 6, 2013 5:05 AM
  • User1734617369 posted

    Hi,

    The problem that you have is that the two user controls have the same ValidationGroup and that causes the validation to occurr for all controls within that group, to overcome this you have to set the ValidationGroup on the both user controls to be unique, which you can do either as a property that you assign when defining them or in the code for the user control, which in your case can be done like this for example:

    public partial class Assessment_Input : System.Web.UI.UserControl
        {
            protected void Page_Load(object sender, EventArgs e)
            {
                if(!IsPostBack)
                {
                    var guid = Guid.NewGuid();
                    TextBox1.ValidationGroup = guid.ToString();
                    TextBox2.ValidationGroup = guid.ToString();
                    RequiredFieldValidator1.ValidationGroup = guid.ToString();
                    RequiredFieldValidator2.ValidationGroup = guid.ToString();
                    Button1.ValidationGroup = guid.ToString();
                }
            }
        }

    Hope this helps!

    Best regards
    Johan

    Sunday, December 8, 2013 2:39 PM
  • User-223976922 posted

    Hi Valuja,

    Thanks for your reply!

    It may not be feasible to add the validation group property in the Pageload of User control, as the UC will have multiple tab panels with many fields.

    Hence i tried to solve it in other way, I added place holders and I am loading the place holders with usercontrol from server side in Pageload/tab change event.

    So far it looks good to me, just curious to know if  this a good way to do it? 

    Monday, December 9, 2013 9:37 PM
  • User-933407369 posted

    hi ,

    that's a way. had you tried to use The <Triggers> element.The <Triggers> element can contain any number each of two child nodes: <asp:AsyncPostBackTrigger> and<asp:PostBackTrigger>.

    please refer to the link below:

    Understanding ASP.NET AJAX UpdatePanel Triggers 

    http://www.asp.net/web-forms/tutorials/aspnet-ajax/understanding-asp-net-ajax-updatepanel-triggers

    Hope it is helpful for you.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, December 10, 2013 1:20 AM