Monday, March 26, 2012

Tab control question

Does anyone know how to force the user to "stay" in a tab if they try to click another tab? I'm doing validation but don't want them to be able to move to another tab until required fields are filled out.

Thanks,

Doug

http://forums.asp.net/t/1086839.aspx

This post might help. You can intercept the ActiveTabChanged event and decide whether you want to allow the tab to change or not.

Jason


Thanks for that link but I'm still unsure how to prevent the user from getting to another tab. I tried setting the "ActiveTabIndex" within that event and it still goes to the other tab.Tongue Tied

Does anyone have any sample code that would help me?

Thanks!

Doug


Hi Doug,

You may keep the previous selectedIndex in a variable, and assign it back to TabContainer if validation fails.

For instance:

int previousIndex = 0; // variable storing previously selected index
protected void Page_Load(object sender, EventArgs e)
{
previousIndex = TabContainer1.ActiveTabIndex;
}
protected void TabContainer1_ActiveTabChanged(object sender, EventArgs e)
{
bool validationResult = true;
// compute validationResult
if(!validationResult) // if validation fails
TabContainer1.ActiveTabIndex = previousIndex;
}

Hope this helps.

No comments:

Post a Comment