Findcontrol property not working in createUserWiza

2019-07-18 02:55发布

问题:

I'm using ASP's createUserWizard control to create my users, and I'd like to add some extra fields (with extra info on the user) which I'm saving in my own table.

I try to access these custom textboxes in code behind with the findContol property (since they're inside the createUserWizard)

The problem is that the textbox I declare and initialize as the control, is null. Here's how I do it:

TextBox t_desc = (TextBox)(CreateUserWizard1.FindControl("txt_desc")); 
o.organisation_description = t_desc.Text;

And this is how the control is nested:

<asp:CreateUserWizard ID="CreateUserWizard1" runat="server"     
      oncreateduser="CreateUserWizard1_CreatedUser">
   <WizardSteps>
       <asp:CreateUserWizardStep ID="CreateUserWizardStep1" runat="server" 
                    Title="Registreer uw organisatie">
         <ContentTemplate>
            <div class="row">
               <div class="half">
                  <table>
                     <tr>
                        <td align="right">
                           <asp:Label ID="lbl_organisation_description" runat="server" AssociatedControlID="txt_desc">Beschrijf uw organisatie:</asp:Label>
                        </td>
                        <td>
                           <asp:TextBox ID="txt_desc" runat="server" ValidationGroup="CreateUserWizard1"></asp:TextBox>
                        </td>
                      </tr>
                    </table>
                  </div>
                </div>
              </ContentTemplate>
              </asp:CreateUserWizardStep>
          </WizardSteps>
</asp:CreateUserWizard>

回答1:

 CreateUserWizardStep step =   (CreateUserWizardStep)  CreateUserWizard1.FindControl("CreateUserWizardStep1"); 
 if (step!=null)
 {
     TextBox txt =  (TextBox)step.ContentTemplateContainer.FindControl("txt_desc");
 }

or if you can see your step control from server-code

TextBox txt =  (TextBox)CreateUserWizardStep1.ContentTemplateContainer.FindControl("txt_desc");


回答2:

FindControl only checks the direct child controls, which in your CreateWizard1 is CreateUserWizardStep (uses runat=server). An easy fix would be using a recursive algorithm:

public static class ControlExtensions
{
    public static Control FindControlRecursive(this Control root, string id)
    {
        if (root.ID == id)
        {
            return root;
        }

        foreach (Control c in root.Controls)
        {
            Control t = c.FindControlRecursive(id);
            if (t != null)
            {
                return t;
            }
        }

        return null;
    }
}

Adding this class to your solution will allow you to use control.FindControlRecursive("id").

Remember that if you're using big pages, this can mean quite some overhead!



回答3:

I think WizardStep is a naming container, so you will need to run the findcontrol against that.

Something like

var step = CreateUserWizard1.CreateUserStep;
TextBox t_desc = (TextBox)(step.FindControl("txt_desc"));

This is completely untested code so it might not work, but i'm pretty sure that the root of the problem is that you are running FindControl on the wrong parent control.



回答4:

Try this to find the Controls inside the CreateUserWizard

TextBox txtCaptcha = (TextBox)CreateUserWizardStep1.CreateUserStep.ContentTemplateContainer.FindControl("txtCaptcha");



回答5:

The EnableViewState property of your CreateUserWizard must be set to True for your DropDownList to maintain state.