How Can I Reference Custom Controls in the CreateUserWizard?

E

EagleRed

I am creating a customized CreateUserWizard that has two steps. In the first
step I add controls to collect the user's name and address. In the second
step I collect information about the user's company, including the company
address. I include a checkbox to allow use of the user's address as the
company address.

Both steps contain a dropdownlist for selecting the state. In the
SelectedIndexChanged event handler on the first page I'd like to determine
the state of the checkbox on the second step and also reference the
dropdownlist on the second step and set the selection there. I would also
like to copy the contents of the user address textboxes to the company
textboxes in the "Next" button event handler.

My problem is that I don't know how to reference these additional controls.
I can see the standard controls included in the base template but not the
controls I add.

Any ideas or guidance would be greatly appreciated.
 
D

Dominick Baier [DevelopMentor]

Hi,

yeah - controls inside of a template are not placed as a page field.

This can be tricky - i am not sure how the CreateUserWizard handles this
(have to look) - but the general way is:

What i usually do is, enable page tracing in the page directive - and use
the FindControl method of the parent control.
 
E

EagleRed

Thanks. I have been doing some additional searching. One post has a
suggestion like this:

DropDownList listVariable = (DropDownList)
CreateUserWizard1.CreateUserStep.ContentTemplateContainer.FindControl("MyControlID");

I tried this but the listVariable was null. It seems the custom controls
that I add disappear into the mist.
 
E

EagleRed

I set tracing on the page and I was able to see one of the controls of
interest as follows:

ctl00$ContentPlaceHolder1$CreateUserWizard1
$CreateUserStepContainer$cboCountry

I cannot access the CreateUserStepContainer in my code because of its
protection level.
 
E

EagleRed

I was able to work around the issue by writing two page methods:

protected Control FindWizardControl(string id)
This method takes the ID of the target control as a parameter and determines
the number of wizard steps via the CreateUserWizard1.WizardSteps.Count value.
Then I iterate over those steps using the
CreateUserWizard1.WizardSteps[stepmumber].Controls.Count. For each control
in the step I call the second method,

protected Control FindWizardControl(Control srcCtrl, string id)
The parameters are a source or root Control and the ID of the target
control. This method is called recursively until the control is found or
everything is exhaused indicating the target control ID is not present. The
approach is to iterate over the child controls of the srcCtrl recursively and
to break from the iterations when the target control is found.

The is as follows:

/// <summary>
/// Find Wizard Control - Find a control in a wizard
/// </summary>
/// <param name="id">ID of target control</param>
/// <returns>A control reference, if found, null, if not</returns>
protected Control FindWizardControl(string id)
{
Control ctrlRtn = null;

for (int i = 0; i < CreateUserWizard1.WizardSteps.Count; i++)
{
for (int j = 0; j <
CreateUserWizard1.WizardSteps.Controls.Count; j++)
{
ctrlRtn =
FindWizardControl(

(Control)CreateUserWizard1.WizardSteps.Controls[j], id);
if (ctrlRtn != null) break;
}
if (ctrlRtn != null) break;
}

return ctrlRtn;

} // end protected Control FindWizardControl(string id)

/// <summary>
/// Find Wizard Control - Find a control in a wizard, is recursive
/// </summary>
/// <param name="srcCtrl">Source/Root Control</param>
/// <param name="id">ID of target control</param>
/// <returns>A Control, if found; null, if not</returns>
protected Control FindWizardControl(Control srcCtrl, string id)
{
Control ctrlRtn = srcCtrl.FindControl(id);

if (ctrlRtn == null)
{
if (srcCtrl.HasControls())
{
int nbrCtrls = srcCtrl.Controls.Count;
for (int i=0; i < nbrCtrls; i++)
{
// Check all child controls of srcCtrl
ctrlRtn = FindWizardControl(srcCtrl.Controls, id);
if (ctrlRtn != null) break;
} // endfor (int i=0; i < nbrCtrls; i++)
} // endif (HasControls)

} // endif (ctrlRtn == null)

return ctrlRtn;
} // end protected Control FindWizardControl(Control srcCtrl, string id)

Thus far it seems to work. Does anyone have a better approach?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,020
Latest member
GenesisGai

Latest Threads

Top