0

I have a series of usercontrols nested in an ajaxToolkit:TabContainer that need to be validated. The user control has a txtFlightFrom and a txtFlightTo control and I need to make sure that is their is data in the txtFlightTo if there is data in txtFlightFrom (you can't fly out of one airport without a destination airport). I'm trying the asp:CompareValidator control for the first time but my real issue is how I triggering the validator when I proceed to the next tab. I tried doing it from my aspx page but that just causes problems and logically doesn't make sense to me.

ascx:

<asp:CompareValidator ID="CompareValidator1" runat="server" ControlToValidate="txtFlightFrom" ControlToCompare="txtFlightTo" Type="String" ErrorMessage="CompareValidator" />
<asp:Label ID="lblCompareTOFROM" runat="server" />

<asp:TextBox ID="txtFlightFrom" runat="server" />
<asp:TextBox ID="txtFlightTo" runat="server" />

aspx:

<ajaxToolkit:TabContainer ID="TabContainer1" runat="server" AutoPostBack="true" OnActiveTabChanged="TabContainer1_ActiveTabChanged">
    <ajaxToolkit:TabPanel ID="TabPanel1" runat="server" HeaderText="Flights">
        <ContentTemplate>
            <ucFlight:FlightControl id="FlightControl1" Runat="server" />
            <ucFlight:FlightControl id="FlightControl2" Runat="server" />
        </ContentTemplate>
    </ajaxToolkit:TabPanel>

<ajaxToolkit:TabPanel ID="TabPanel2" runat="server" HeaderText="Cars">
    stuff
</ajaxToolkit:TabPanel>

ascx.cs

public string ValidateToFrom
{
    get { return lblCompareTOFROM.Text; }
    set { lblCompareTOFROM.Text = value; }        
}

aspx.cs

if (Page.IsValid)
{
    FlightControl1.ValidateToFrom = "Not Valid";
}

I've also tried variations of this code in the ascx.cs but that also doesn't make sense because the event is happening in the aspx.cs

Any thoughts?

Jeremy
  • 45
  • 3
  • 9

1 Answers1

0

The trick I used months ago was to fire the validation function on the client side calling a javascript function when the user changes the active tab. A quick search on the net lead me to this example (I can't do a test at this moment, sorry):

<ajaxToolkit:TabContainer runat="server" ID="Tabs" OnClientActiveTabChanged="ActiveTabChanged">


//Javascript function  
function ActiveTabChanged(sender, e) {  
    if (Page_ClientValidate() == false) {  
        var ctrl = $find("Tabs");  
        var tabpanel = ctrl.get_tabs()[0];  
        handleTabChange = false;  
        ctrl.set_activeTab(tabpanel);  
    }  
}   
Ghidello
  • 1,863
  • 20
  • 21
  • So you'd put the script on the aspx page and not the usercontrol itself? (Which I guess would make sense seeing as the event is happening there.) – Jeremy Nov 19 '10 at 15:03
  • The validation controls will still be into your inner webcontrols. On the page (i.e. inside the tabcontainer control) you'll simply fire the page validation function – Ghidello Nov 19 '10 at 16:08