using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using MSJ.Utility;
using MSJ.BaseControl;
namespace MSJ.Controls
{
public abstract class TabbedWizardBase : System.Web.UI.UserControl
{
protected WizardBasePage _myPage = null;
#region -- Abstract Functions -- For Implementation of Inheritors --
/// <summary>
/// Resets the menu images to disabled mode.
/// </summary>
protected abstract void ResetMenuImagesToDisabled();
/// <summary>
/// Sets the current selected image based on its ID.
/// </summary>
/// <param name="ID">The ID.</param>
protected abstract void SetActiveImageByImageID(string imageID);
/// <summary>
/// Sets the current selected image based on its index.
/// The index can also be the ActiveStepIndex of the WizardPartnerControl.
/// </summary>
/// <param name="index">The index of the tab. 0-Based</param>
protected abstract void SetActiveImageByIndex(int index);
protected abstract ImageButton GetDefaultTabButton();
#endregion -- Abstract Functions -- For Implementation of Inheritors --
protected void Page_PreRender(object sender, EventArgs e)
{
try
{
Initialize();
}
catch (Exception ex)
{
throw ex;
}
}
private void Initialize()
{
if (ViewState["CurrentStepIndex"] == null)
{
CurrentStepIndex = 0;
NextStepIndex = 1;
}
}
/// <summary>
/// Initializes the partner controls. Must be called by WizardBasePage subclasses
/// in their Page_Init event
/// </summary>
/// <param name="thePage">The page.</param>
public void InitPartnerControls(WizardBasePage thePage)
{
_myPage = thePage;
WizardPartnerControl.ActiveStepChanged += new EventHandler(wizard_ActiveStepChanged);
WizardPartnerControl.NextButtonClick += new WizardNavigationEventHandler(WizardPartnerControl_NextButtonClick);
WizardPartnerControl.SideBarButtonClick += new WizardNavigationEventHandler(WizardPartnerControl_SideBarButtonClick);
WizardPartnerControl.FinishButtonClick += new WizardNavigationEventHandler(WizardPartnerControl_FinishButtonClick);
}
void WizardPartnerControl_FinishButtonClick(object sender, WizardNavigationEventArgs e)
{
bool fContinue = false;
if (_myPage.ValidateThis(e, false)) fContinue = true;
_myPage.FinishButtonClickFinalize(sender, e);
if (fContinue)
{
ImageButton btn = GetDefaultTabButton();
if (btn != null)
{
_SetActiveTabImageAndWizardIndex(btn);
}
}
}
void WizardPartnerControl_SideBarButtonClick(object sender, WizardNavigationEventArgs e)
{
_myPage.ValidateThis(e, true);
}
void WizardPartnerControl_NextButtonClick(object sender, WizardNavigationEventArgs e)
{
_myPage.ValidateThis(e, false);
}
protected Wizard WizardPartnerControl
{
get { return _myPage.Wizard; }
}
void wizard_ActiveStepChanged(object sender, EventArgs e)
{
try
{
CurrentStepIndex = 0;
NextStepIndex = 1;
//ASSUMPTION:
//The imageID of the steps of the wizard ends with sequential numeric value
//like 1, 2, 3, etc.
/***************************************
//NOTE: This is 0-Based!!!!!!
****************************************/
string id = WizardPartnerControl.ActiveStep.ID;
int index = GetIndexOfWizardStep(id);
if (index != -1)
{
CurrentStepIndex = index;
NextStepIndex = index + 1;
}
else if (id.ToLower().Contains(GetIDOfWizardCompleteStep().ToLower()))
{
CurrentStepIndex = NextStepIndex = GetVisibleTabCount();
}
}
catch (Exception ex)
{
throw ex;
}
}
public int CurrentStepIndex
{
get
{
object currentStepIndex = ViewState["CurrentStepIndex"];
return (currentStepIndex == null) ? 0 : (int)currentStepIndex;
}
protected set
{
ViewState["CurrentStepIndex"] = value;
_SetActiveImageByIndex(value);
}
}
/// <summary>
/// NextStepIndex = 0-Based; this is the target index of the ID
/// </summary>
public int NextStepIndex
{
get
{
object nextStepIndex = ViewState["NextStepIndex"];
return (nextStepIndex == null) ? 1 : (int)nextStepIndex;
}
protected set
{
ViewState["NextStepIndex"] = value;
}
}
protected void Menu_Click(object sender, ImageClickEventArgs e)
{
try
{
ImageButton btn = (ImageButton)sender;
_SetActiveTabImageAndWizardIndex(btn);
}
catch (Exception ex)
{
throw ex;
}
}
private void _SetActiveTabImageAndWizardIndex(ImageButton btn)
{
_SetActiveImageByImageID(btn.ID);
_SetActiveStepIndexOfWizardByTabImageID(btn.ID);
}
private void _SetActiveImageByImageID(string ID)
{
try
{
ResetMenuImagesToDisabled();
SetActiveImageByImageID(ID);
}
catch (Exception ex)
{
throw ex;
}
}
private void _SetActiveImageByIndex(int index)
{
try
{
ResetMenuImagesToDisabled();
SetActiveImageByIndex(index);
}
catch (Exception ex)
{
throw ex;
}
}
private void _SetActiveStepIndexOfWizardByTabImageID(string imageID)
{
try
{
//NOTE:
// NextStepIndex = 0-based
// ActiveStepIndex = 0-based
// targetStepIndex = 0-based
int oldCurrentStepIndex = CurrentStepIndex;
int targetStepIndex = -1;
targetStepIndex = GetActiveTabIndexByImageID(imageID);
if (oldCurrentStepIndex != targetStepIndex)
{
/**************************************************
//RULE: we only validate if we are going a step forward
**************************************************/
if (oldCurrentStepIndex < targetStepIndex)
{
bool fContinue = true;
int tmpNextStepIndex = oldCurrentStepIndex;
/**************************************************
//check if we skipped an intermediate step;
//The difference between the current ID and the target ID should always be 1
//if it is > 1 we skipped a step; therefore we need to validate that too
**************************************************/
while (tmpNextStepIndex < targetStepIndex)
{
WizardPartnerControl.ActiveStepIndex = tmpNextStepIndex;
if (!_myPage.ValidateThis(new WizardNavigationEventArgs(oldCurrentStepIndex, tmpNextStepIndex), true))
{
/**************************************************
//Validation failed:
//- restore image;
//- prevent changing the ActiveStepIndex
**************************************************/
_SetActiveImageByIndex(WizardPartnerControl.ActiveStepIndex);
fContinue = false;
break;
}
tmpNextStepIndex++;
}
if (fContinue)
{
//everything is ok; we can set the active ID to the target ID
WizardPartnerControl.ActiveStepIndex = targetStepIndex;
}
}
else
{
WizardPartnerControl.ActiveStepIndex = targetStepIndex;
}
}
}
catch (Exception ex)
{
throw ex;
}
}
private int GetVisibleTabCount()
{
try
{
/**************************************************
//Retrieve these from the config file
**************************************************/
string visibleTabCount = ConfigurationManager.AppSettings[TabImageID.VisibleTabCount.ToString()];
if (String.IsNullOrEmpty(visibleTabCount)) throw new Exception("Missing VisibleTabCount entry in the appSettings of the web.config file");
int tabs = 0;
Int32.TryParse(visibleTabCount, out tabs);
return tabs;
}
catch (Exception ex)
{
throw ex;
}
}
/// <summary>
/// Gets the ID of wizard step.
/// </summary>
/// <param name="step">The wizard step. This is 0-based.</param>
/// <returns></returns>
private string GetIDOfWizardStep(int step)
{
try
{
string wizardStepID = ConfigurationManager.AppSettings[string.Format("{0}{1}", WizardConstants.WebConfigKey_WizardStepIDPrefix, step)];
//if (String.IsNullOrEmpty(wizardStepID)) throw new Exception(string.Format("Missing {0} entry in the appSettings of the web.config file", StringConstants.WebConfigKey_WizardStepIDPrefix));
return wizardStepID;
}
catch (Exception ex)
{
throw ex;
}
}
private int GetIndexOfWizardStep(string ID)
{
try
{
string theID = String.Empty;
int max = GetVisibleTabCount();
//NOTE: The count starts at 0 up to max-1
for (int i = 0; i < max; i++)
{
theID = GetIDOfWizardStep(i);
if (!string.IsNullOrEmpty(theID)
&& theID.ToLower() == ID.ToLower()) return i;
}
return -1;
}
catch (Exception ex)
{
throw ex;
}
}
private string GetIDOfWizardCompleteStep()
{
try
{
/**************************************************
//TODO: Retrieve these from the config file
**************************************************/
string wizardCompleteStep_ID = ConfigurationManager.AppSettings[WizardStepID.WizardCompleteStepID.ToString()];
if (String.IsNullOrEmpty(wizardCompleteStep_ID)) throw new Exception("Missing WizardCompleteStepID entry in the appSettings section of the web.config file");
return wizardCompleteStep_ID;
}
catch (Exception ex)
{
throw ex;
}
}
/// <summary>
/// Gets the active tab index by image ID. 0-Based.
/// May be overriden by subclasses to implement their own method of getting the active tab index.
/// </summary>
/// <param name="imageID">The image ID.</param>
/// <returns></returns>
protected virtual int GetActiveTabIndexByImageID(string imageID)
{
try
{
/**************************************************
//TODO: Retrieve these from the config file
**************************************************/
string tabImageIDDefault = ConfigurationManager.AppSettings[TabImageID.TabImageIDDefault.ToString()];
string tabImageID0 = ConfigurationManager.AppSettings[TabImageID.TabImageButtonID0.ToString()];
string tabImageID1 = ConfigurationManager.AppSettings[TabImageID.TabImageButtonID1.ToString()];
string tabImageID2 = ConfigurationManager.AppSettings[TabImageID.TabImageButtonID2.ToString()];
string tabImageID3 = ConfigurationManager.AppSettings[TabImageID.TabImageButtonID3.ToString()];
string tabImageID4 = ConfigurationManager.AppSettings[TabImageID.TabImageButtonID4.ToString()];
string tabImageID5 = ConfigurationManager.AppSettings[TabImageID.TabImageButtonID5.ToString()];
string tabImageID6 = ConfigurationManager.AppSettings[TabImageID.TabImageButtonID6.ToString()];
string tabImageID7 = ConfigurationManager.AppSettings[TabImageID.TabImageButtonID7.ToString()];
string tabImageID8 = ConfigurationManager.AppSettings[TabImageID.TabImageButtonID8.ToString()];
string tabImageID9 = ConfigurationManager.AppSettings[TabImageID.TabImageButtonID9.ToString()];
string tabImageID10 = ConfigurationManager.AppSettings[TabImageID.TabImageButtonID10.ToString()];
int targetStepIndex = -1;
if (!String.IsNullOrEmpty(tabImageID0) && imageID.ToLower() == tabImageID0.ToLower()) targetStepIndex = 0;
if (!String.IsNullOrEmpty(tabImageID1) && imageID.ToLower() == tabImageID1.ToLower()) targetStepIndex = 1;
if (!String.IsNullOrEmpty(tabImageID2) && imageID.ToLower() == tabImageID2.ToLower()) targetStepIndex = 2;
if (!String.IsNullOrEmpty(tabImageID3) && imageID.ToLower() == tabImageID3.ToLower()) targetStepIndex = 3;
if (!String.IsNullOrEmpty(tabImageID4) && imageID.ToLower() == tabImageID4.ToLower()) targetStepIndex = 4;
if (!String.IsNullOrEmpty(tabImageID5) && imageID.ToLower() == tabImageID5.ToLower()) targetStepIndex = 5;
if (!String.IsNullOrEmpty(tabImageID6) && imageID.ToLower() == tabImageID6.ToLower()) targetStepIndex = 6;
if (!String.IsNullOrEmpty(tabImageID7) && imageID.ToLower() == tabImageID7.ToLower()) targetStepIndex = 7;
if (!String.IsNullOrEmpty(tabImageID8) && imageID.ToLower() == tabImageID8.ToLower()) targetStepIndex = 8;
if (!String.IsNullOrEmpty(tabImageID9) && imageID.ToLower() == tabImageID9.ToLower()) targetStepIndex = 9;
if (!String.IsNullOrEmpty(tabImageID10) && imageID.ToLower() == tabImageID10.ToLower()) targetStepIndex = 10;
targetStepIndex = targetStepIndex == -1 ? 0 : targetStepIndex;
return targetStepIndex;
}
catch (Exception ex)
{
throw ex;
}
}
}
}
Subscribe to:
Post Comments (Atom)
What a line of code
I didn't know this line of code (in any language) will make sense but apparently it does: auto l = [](){}; Look at all those bracke...
-
After reading and playing around with the steps mentioned in Scott Mitchell's excellent tutorial on 3-Tier ASP.NET Architecture (the fir...
-
using System; using System.Data; using System.Configuration; using System.Web; using System.Web.Security; using System.Web.UI; using S...
-
I just got a new machine, an Acer laptop with enough bells and whistles to make a developer happy (Core 2 Duo, 2GHZ, 2 GB RAM). As usual I ...
No comments:
Post a Comment