Programatically Highlighting Current Step In ASP.NET Wizard

This was frustrating, so gonna save some folks the time that are using the ASP.NET wizard in a SharePoint WebPart.

I found a lot of examples of how to highlight the current step in the ASP.NET wizard control via WebForm markup, but there was a severe lack of examples that show how to do it in straight code behind. In essence you are relegated to implement various sets of ITemplates for the ASP.NET wizard corresponding DataList, notably instantiating against the SelectedItemTemplate and ItemTemplate properties. ITemplates defines the behavior for populating a templated ASP.NET server control with child controls. The child controls represent the inline templates defined on the page, or in this case, within a composite control. The important part to remember when creating the various templates is the SideBarList control must contain an IButtonControl with ID SideBarButton in every item template, this may include ItemTemplate, EditItemTemplate, SelectedItemTemplate or AlternatingItemTemplate if they exist. As stated before, we just need to focus on two properties.

So, firstly for your strongly typed Wizard object in CreateChildControls or wherever set the SideBarTemplate

  1. [SomeAspNetWizardObject].SideBarTemplate = new SideBarHighlighter();

Now onto ITemplate fiesta:

  1. public class SelectedItemTemplateMerged :ITemplate
  2.     {
  3.         public void InstantiateIn(Control container)
  4.         {
  5.             LinkButton activeSideBarButton = new LinkButton();
  6.             activeSideBarButton.ID = "ActiveSideBarButton";
  7.             LinkButton sideBarButton = new LinkButton();
  8.             sideBarButton.ID = "SideBarButton";
  9.             sideBarButton.Visible = false;
  10.             container.Controls.Add(activeSideBarButton);
  11.             container.Controls.Add(sideBarButton);
  12.         }
  13.     }
  14.  
  15.     public class SideBarItem :ITemplate
  16.     {
  17.         public void InstantiateIn(Control container)
  18.         {
  19.             LinkButton sideBarButton = new LinkButton();
  20.             sideBarButton.ID = "SideBarButton";
  21.             container.Controls.Add(sideBarButton);
  22.         }
  23.     }
  24.  
  25.     public class SideBarHighlighter : ITemplate
  26.     {
  27.         public void InstantiateIn(Control container)
  28.         {
  29.             DataList sideBarList = new DataList();
  30.             sideBarList.ItemDataBound += SideBarList_ItemDataBound;
  31.             sideBarList.SelectedItemTemplate = new SelectedItemTemplateMerged();
  32.             sideBarList.ItemTemplate = new SideBarItem();
  33.             sideBarList.ID = "SideBarList";
  34.             sideBarList.SelectedItemStyle.Font.Bold = true;
  35.             sideBarList.RepeatLayout = RepeatLayout.Flow;
  36.             container.Controls.Add(sideBarList);
  37.         }
  38.  
  39.         private void SideBarList_ItemDataBound(object sender, DataListItemEventArgs e)
  40.         {
  41.             WizardStep stepCurrent = (WizardStep) e.Item.DataItem;
  42.             if (stepCurrent.Wizard.ActiveStepIndex == e.Item.ItemIndex)
  43.             {
  44.                 LinkButton activeSideBarButton = (LinkButton) e.Item.FindControl("ActiveSideBarButton");
  45.                 activeSideBarButton.BorderStyle = BorderStyle.Solid;
  46.                 activeSideBarButton.BorderColor = Color.White;
  47.                 activeSideBarButton.BackColor = Color.White;
  48.                 activeSideBarButton.Font.Bold = true;
  49.                 activeSideBarButton.Style.Add(HtmlTextWriterStyle.Color, "#000000");
  50.                 activeSideBarButton.Text = string.Format(">> {0}", stepCurrent.Title);           
  51.             }
  52.         }
  53.     }

 The obvious important part is the ItemDataBound event, since that’s where the text is mutated.

share save 171 16 Programatically Highlighting Current Step In ASP.NET Wizard

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>