OT: Can't find a control in a Wizard!

OT: Can't find a control in a Wizard!

Old forum URL: forums.lhotka.net/forums/t/948.aspx


david.wendelken posted on Friday, August 18, 2006

No matter what I've tried, (and I've tried all the things mentioned in every article I can find!), I cannot get the FindControl to work.

<asp:Wizard ID="Wizard1" runat="server" ActiveStepIndex="0"
    OnActiveStepChanged="Wizard1_ActiveStepChanged" >
  <WizardSteps>
    <asp:WizardStep ID="WizardStep1" runat="server" Title="Start" ></asp:WizardStep>
  
<asp:WizardStep ID="WizardStep2" runat="server" Title="Select Edit Option" ></asp:WizardStep>
</WizardSteps>
<HeaderTemplate>

    <asp:Label ID="lblNote" runat="server" Text="Wizard Step Title Goes here" ></asp:Label>

</HeaderTemplate>

protected void Wizard1_ActiveStepChanged(object sender, EventArgs e)
{
   Label lblNote = (Label)Wizard1.FindControl("lblNote");

  // No matter how I've tried the above line, lblNote is always null.

   if
(lblNote != null)
  {
      lblNote.Text = Wizard1.WizardSteps[Wizard1.ActiveStepIndex].Title;
  }

}

david.wendelken replied on Friday, August 18, 2006

I moved the label to one of the steps, and the same FindControl code could find the label.

I've tried Wizard1.FindControl, WizardStep1.FindControl, Wizard1.Page.FindControl, and half-a-dozen other ways.

Any ideas?

david.wendelken replied on Friday, August 18, 2006

Found this routine at http://www.asptoday.com/Content.aspx?id=2359 that does the trick very nicely:

 

private Control FindControl(Control parent, string id)

{

if (parent.ID == id)

{ return parent; }

foreach (Control child in parent.Controls)

{

Control recurse = FindControl(child, id);

if (recurse != null)

{ return recurse; }

}

return null;

}

Copyright (c) Marimer LLC