You may have faced a common problem were you want to put a validation controls inside a TabContainer. and you want to add a postback button (e.g. Save button, which will be used as an example) to validate all the controls, but the problem, if you have an invalid control, is how to select the correct Tab, based on the Validation status (you may want to select the first invalid Tab).
I'm talking about when you have the same ValidationGroup in all the controls which are spread through multiple tabs, and ofcourse you have the Save button with the same ValidationGroup.
First thing I'll try to do is to remove the validation group from the Save button.
Then I'll do the Validation in the server side :
Page.Validate();
and then I'll ask if all the conrtols in the page are valid,and if so I'll Save the data :
if (Page.IsValid)
{
Save();
}
well, this can be great if it works as I expect, but it didn't.
The active tab was the last one when I clicked the Save button.
so I tried to change the tab accordingly, for that I had to add methods to check if the data in each tab is valid .
example:
private bool IsTab1DataValid()
{
if (TextBox1.Text.Length == 0)
return false;
return true;
}
and then in the Save() method I did:
protected void Save()
{
Page.Validate();
if ( ! IsTab1DataValid())
{
tabContainer.ActiveTab = TabPanel1;
return;
}
if ( ! IsTab2DataValid())
{
tabContainer.ActiveTab = TabPanel2;
return;
}
Save();
}
[Note]: you may assign different Validation group for different tabs, then you can use Page.Validate("ValidationGroupForTab1"); and so on ... instead of previous methods
it should be Ok now, but if you have ValidatorCallout in the Page then it's not Ok, the ValidatorCallout will be misplaced if you switch tabs (Bug?).
what to do now?
well, it's about the time for some JavaScript.
first thing Remove the Page.Validate(); line from the Save() method, and then we need to place this script to validate the page in client side:
That's it.
Note: if you have the TabContainer inside an UpdatePanel, then you need to validate the page after each request:
Next: I'll try to do it all in Javascript.
validation in ajaxtoolkit tabcontainer
,Javascript,asp,asp.net,ajax,Validate