title: C# - Find LoginView Control within MultiView
body: <p>How to access <code>LoginView1</code> control inside <code>Multiview1</code>, <code>View2</code>?</p>
  
  <p>I have a some textbox and dropdownlist in loggedIn template of View 2 within Multiview, I need to get the control and bind with text from database.</p>
  
  <p>I had try:</p>
  
  <pre><code>((TextBox)this.LoginView1.FindControl("custEmail")).Text = dr.GetValue(1).ToString();
  ((TextBox)LoginView1.FindControl("custEmail")).Text = dr.GetValue(1).ToString();
  ((TextBox)LoginView1.FindControl("custEmail")).Text = "xxxx";
  </code></pre>
  
  <p>return "Object reference not set to an instance of an object."</p>
  
  <p>Markup Code</p>
  
  <pre><code>&lt;asp:View ID="View2" runat="server" OnActivate="login_Click"&gt;
                  &lt;asp:Label ID="tprice" runat="server" /&gt;&lt;br /&gt;
                  &lt;asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Conditional"&gt;
                      &lt;ContentTemplate&gt;
                          &lt;asp:LoginView ID="LoginView1" runat="server" ViewStateMode="Disabled"&gt;
                              &lt;AnonymousTemplate&gt;
                                  Please login to continue.&lt;br /&gt;
                                  &lt;asp:Login ID="Login1" runat="server" ViewStateMode="Disabled" RenderOuterTable="false"&gt;
                                      &lt;LayoutTemplate&gt;
                                          &lt;fieldset&gt;
                                              &lt;legend&gt;Log in Form&lt;/legend&gt;
                                              &lt;ul&gt;
                                                  &lt;li&gt;
                                                      &lt;asp:Label ID="Label2" runat="server" AssociatedControlID="UserName"&gt;User name&lt;/asp:Label&gt;
                                                      &lt;asp:TextBox runat="server" ID="UserName" /&gt;
                                                      &lt;asp:RequiredFieldValidator runat="server" ControlToValidate="UserName"
                                                          CssClass="field-validation-error" ErrorMessage="The user name field is required." /&gt;
                                                  &lt;/li&gt;
                                                  &lt;li&gt;
                                                      &lt;asp:Label ID="Label4" runat="server" AssociatedControlID="Password"&gt;Password&lt;/asp:Label&gt;
                                                      &lt;asp:TextBox runat="server" ID="Password" TextMode="Password" /&gt;
                                                      &lt;asp:RequiredFieldValidator runat="server" ControlToValidate="Password"
                                                          CssClass="field-validation-error" ErrorMessage="The password field is required." /&gt;
                                                  &lt;/li&gt;
                                              &lt;/ul&gt;
                                              &lt;asp:Button ID="login" runat="server" CommandName="Login" OnCommand="login_Click"
                                                  Text="Log in"
                                                  CssClass="btn btn-info" /&gt;
                                          &lt;/fieldset&gt;
                                          &lt;asp:Label runat="server" ID="FailureText" class="validation-summary-errors" /&gt;
                                      &lt;/LayoutTemplate&gt;
                                  &lt;/asp:Login&gt;
                                  or
                          &lt;a id="registerLink" runat="server" href="~/Account/Register"&gt;Register&lt;/a&gt;&amp;nbsp;
                          to continue
                              &lt;/AnonymousTemplate&gt;
                              &lt;LoggedInTemplate&gt;
                                  &lt;table class="custable"&gt;
                                      &lt;tr&gt;
                                          &lt;td&gt;
                                              &lt;asp:Label ID="Label5" AssociatedControlID="title" runat="server" Text="Title : " /&gt;
                                          &lt;/td&gt;
                                          &lt;td&gt;
                                              &lt;asp:DropDownList ID="title" runat="server"&gt;
                                                  &lt;asp:ListItem Value="0"&gt;Select One...&lt;/asp:ListItem&gt;
                                                  &lt;asp:ListItem Value="Mr"&gt;Mr&lt;/asp:ListItem&gt;
                                                  &lt;asp:ListItem Value="Mrs"&gt;Mrs&lt;/asp:ListItem&gt;
                                                  &lt;asp:ListItem Value="Miss"&gt;Miss&lt;/asp:ListItem&gt;
                                                  &lt;asp:ListItem Value="Ms"&gt;Ms&lt;/asp:ListItem&gt;
                                                  &lt;asp:ListItem Value="Doctor"&gt;Doctor&lt;/asp:ListItem&gt;
                                              &lt;/asp:DropDownList&gt;
                                          &lt;/td&gt;
                                          &lt;td&gt;
                                              &lt;asp:CompareValidator ID="CompareValidator1" runat="server"
                                                  ControlToValidate="title" ValueToCompare="0" Operator="NotEqual"
                                                  Text="Please select the title." CssClass="field-validation-error"
                                                  SetFocusOnError="true" ValidationGroup="vg1" /&gt;
                                          &lt;/td&gt;
                                      &lt;/tr&gt;
                                      &lt;tr&gt;
                                          &lt;td&gt;
                                              &lt;asp:Label ID="Label6" AssociatedControlID="name" runat="server" Text="Full Name As Per IC : " /&gt;
                                          &lt;/td&gt;
                                          &lt;td&gt;
                                              &lt;asp:TextBox ID="name" runat="server" /&gt;
                                          &lt;/td&gt;
                                          &lt;td&gt;
                                              &lt;asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ControlToValidate="name"
                                                  CssClass="field-validation-error" Text="*Required"
                                                  SetFocusOnError="true" ValidationGroup="vg1" /&gt;
                                          &lt;/td&gt;
                                      &lt;/tr&gt;
                                      &lt;tr&gt;
                                          &lt;td&gt;
                                              &lt;asp:Label ID="Label7" AssociatedControlID="custEmail" runat="server" Text="Email : " /&gt;
                                          &lt;/td&gt;
                                          &lt;td&gt;
                                              &lt;asp:TextBox ID="custEmail" runat="server" TextMode="Email" /&gt;
                                          &lt;/td&gt;
                                          &lt;td&gt;
                                              &lt;asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server" ControlToValidate="custEmail"
                                                  CssClass="field-validation-error" Text="*"
                                                  SetFocusOnError="true" ValidationGroup="vg1" /&gt;
                                          &lt;/td&gt;
                                      &lt;/tr&gt;
                                      &lt;tr&gt;
                                          &lt;td&gt;
                                              &lt;asp:Label ID="Label8" AssociatedControlID="email2" runat="server" Text="Confirm Email : " /&gt;
                                          &lt;/td&gt;
                                          &lt;td&gt;
                                              &lt;asp:TextBox ID="email2" runat="server" TextMode="Email" /&gt;
                                          &lt;/td&gt;
                                          &lt;td&gt;
                                              &lt;asp:CompareValidator ID="CompareValidator2" ControlToCompare="email2" ControlToValidate="custEmail"
                                                  runat="server"
                                                  Operator="Equal" Type="String" Text="Please enter same Email." CssClass="field-validation-error"
                                                  ValidationGroup="vg1" /&gt;
                                          &lt;/td&gt;
                                      &lt;/tr&gt;
                                      &lt;tr&gt;
                                          &lt;td&gt;
                                              &lt;asp:Label ID="Label9" AssociatedControlID="icPassport" runat="server" Text="IC / Passport : " /&gt;
                                          &lt;/td&gt;
                                          &lt;td&gt;
                                              &lt;asp:TextBox ID="icPassport" runat="server" /&gt;
                                          &lt;/td&gt;
                                          &lt;td&gt;
                                              &lt;asp:RequiredFieldValidator ID="RequiredFieldValidator3" runat="server" ControlToValidate="icPassport"
                                                  CssClass="field-validation-error" Text="*"
                                                  SetFocusOnError="true" ValidationGroup="vg1" /&gt;
                                          &lt;/td&gt;
                                      &lt;/tr&gt;
                                      &lt;tr&gt;
                                          &lt;td&gt;
                                              &lt;asp:Label ID="Label10" AssociatedControlID="mobile" runat="server" Text="Mobile : " /&gt;
                                          &lt;/td&gt;
                                          &lt;td&gt;
                                              &lt;asp:TextBox ID="mobile" runat="server" placeholder="60123456789" /&gt;
                                          &lt;/td&gt;
                                          &lt;td&gt;
                                              &lt;asp:RequiredFieldValidator ID="RequiredFieldValidator4" runat="server" ControlToValidate="mobile"
                                                  CssClass="field-validation-error" Text="*"
                                                  SetFocusOnError="true" ValidationGroup="vg1" /&gt;&lt;br /&gt;
                                              &lt;asp:CompareValidator ID="CompareValidator3" runat="server" ControlToValidate="mobile"
                                                  Operator="DataTypeCheck" Type="Double" ErrorMessage="Mobile number must be integer."
                                                  CssClass="field-validation-error" ValidationGroup="vg1" Display="None" /&gt;&lt;br /&gt;
                                              &lt;asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server" ControlToValidate="mobile"
                                                  ErrorMessage="Mobile Number must within 10 to 18 digits." ValidationExpression="^\d{10,18}$"
                                                  CssClass="field-validation-error" ValidationGroup="vg1" Display="None" /&gt;
                                          &lt;/td&gt;
                                      &lt;/tr&gt;
                                      &lt;tr&gt;
                                          &lt;td&gt;
                                              &lt;asp:Label ID="Label11" AssociatedControlID="street" runat="server" Text="Street : " /&gt;
                                          &lt;/td&gt;
                                          &lt;td&gt;
                                              &lt;asp:TextBox ID="street" runat="server" /&gt;
                                          &lt;/td&gt;
                                          &lt;td&gt;
                                              &lt;asp:RequiredFieldValidator ID="RequiredFieldValidator5" runat="server" ControlToValidate="street"
                                                  CssClass="field-validation-error" Text="*"
                                                  SetFocusOnError="true" ValidationGroup="vg1" /&gt;
                                          &lt;/td&gt;
                                      &lt;/tr&gt;
                                      &lt;tr&gt;
                                          &lt;td&gt;
                                              &lt;asp:Label ID="Label12" AssociatedControlID="townstate" runat="server" Text="Town / State : " /&gt;
                                          &lt;/td&gt;
                                          &lt;td&gt;
                                              &lt;asp:TextBox ID="townstate" runat="server" /&gt;
                                          &lt;/td&gt;
                                          &lt;td&gt;
                                              &lt;asp:RequiredFieldValidator ID="RequiredFieldValidator6" runat="server" ControlToValidate="townstate"
                                                  CssClass="field-validation-error" Text="*"
                                                  SetFocusOnError="true" ValidationGroup="vg1" /&gt;
                                          &lt;/td&gt;
                                      &lt;/tr&gt;
                                      &lt;tr&gt;
                                          &lt;td&gt;
                                              &lt;asp:Label ID="Label13" AssociatedControlID="country" runat="server" Text="Country : " /&gt;
                                          &lt;/td&gt;
                                          &lt;td&gt;
                                              &lt;asp:DropDownList ID="country" runat="server" /&gt;
                                          &lt;/td&gt;
                                          &lt;td&gt;
                                              &lt;asp:CompareValidator ID="CompareValidator4" runat="server"
                                                  ControlToValidate="country" ValueToCompare="Select One..." Type="String" Operator="NotEqual"
                                                  Text="*" CssClass="field-validation-error"
                                                  SetFocusOnError="true" ValidationGroup="vg1" /&gt;
                                          &lt;/td&gt;
                                      &lt;/tr&gt;
                                      &lt;tr&gt;
                                          &lt;td&gt;
                                              &lt;asp:Label ID="Label14" AssociatedControlID="postalcode" runat="server" Text="Postal Code : " /&gt;
                                          &lt;/td&gt;
                                          &lt;td&gt;
                                              &lt;asp:TextBox ID="postalcode" runat="server" /&gt;
                                          &lt;/td&gt;
                                          &lt;td&gt;
                                              &lt;asp:RequiredFieldValidator ID="RequiredFieldValidator7" runat="server" ControlToValidate="postalcode"
                                                  CssClass="field-validation-error" Text="*"
                                                  SetFocusOnError="true" ValidationGroup="vg1" /&gt;&lt;br /&gt;
                                              &lt;asp:CompareValidator ID="CompareValidator5" runat="server" ControlToValidate="postalcode"
                                                  Type="Integer" Operator="DataTypeCheck" ErrorMessage="Postal Code must be integer."
                                                  CssClass="field-validation-error" ValidationGroup="vg1" Display="none" /&gt;&lt;br /&gt;
                                              &lt;asp:RegularExpressionValidator ID="RegularExpressionValidator2" runat="server" ControlToValidate="postalcode"
                                                  ErrorMessage="Postal Code, 5 or 9 Digits." ValidationExpression="\d{5}?(\d{4})?$"
                                                  CssClass="field-validation-error" ValidationGroup="vg1" Display="None" /&gt;
                                          &lt;/td&gt;
                                      &lt;/tr&gt;
                                      &lt;tr&gt;
                                          &lt;td&gt;
                                              &lt;asp:Label ID="Label15" AssociatedControlID="yearOfBirth" runat="server" Text="Year Of Birth : " /&gt;
                                          &lt;/td&gt;
                                          &lt;td&gt;
                                              &lt;asp:DropDownList ID="yearOfBirth" runat="server"&gt;
                                                  &lt;asp:ListItem Value="0"&gt;Year&lt;/asp:ListItem&gt;
                                              &lt;/asp:DropDownList&gt;
                                          &lt;/td&gt;
                                          &lt;td&gt;
                                              &lt;asp:CompareValidator ID="CompareValidator6" runat="server"
                                                  ControlToValidate="yearOfBirth" ValueToCompare="0" Operator="NotEqual"
                                                  Text="*" CssClass="field-validation-error"
                                                  SetFocusOnError="true" ValidationGroup="vg1" /&gt;
                                          &lt;/td&gt;
                                      &lt;/tr&gt;
                                  &lt;/table&gt;
                              &lt;/LoggedInTemplate&gt;
                          &lt;/asp:LoginView&gt;
                      &lt;/ContentTemplate&gt;
                  &lt;/asp:UpdatePanel&gt;
                  &lt;asp:Button ID="Button1" Text="Next" CommandName="NextView" runat="server" /&gt;
              &lt;/asp:View&gt;
  </code></pre>
  
  <p>C# Code Behind</p>
  
  <pre><code>protected void login_Click(object sender, EventArgs e)
      {
          MultiView1.SetActiveView(View2);
  
          if (User.Identity.IsAuthenticated)
          {
              string userId = (Membership.GetUser().ProviderUserKey).ToString();
              SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString);
              SqlCommand cmd = new SqlCommand();
              string selectcmd = "select Email from Memberships where userID='" + userId + "'";
              string selectcmd2 = "select custEmail, title, name, icPassport, mobile, street, townstate, postalcode, townstate, yearOfBirth from custInfo where userID='" + userId + "'";
  
              SqlDataReader dr;
              cmd = new SqlCommand(selectcmd2, con);
              con.Open();
              dr = cmd.ExecuteReader();
  
              if (dr.HasRows)
              {
  
                  while (dr.Read())
                  {
                      ((TextBox)this.LoginView1.FindControl("custEmail")).Text = dr.GetValue(1).ToString();
                      ((DropDownList)LoginView1.FindControl("title")).SelectedValue = dr.GetValue(2).ToString();
                      ((TextBox)LoginView1.FindControl("name")).Text = dr.GetValue(3).ToString();
                      ((TextBox)LoginView1.FindControl("icPassport")).Text = dr.GetValue(4).ToString();
                      ((TextBox)LoginView1.FindControl("mobile")).Text = dr.GetValue(5).ToString();
                      ((TextBox)LoginView1.FindControl("street")).Text = dr.GetValue(6).ToString();
                      ((DropDownList)LoginView1.FindControl("townstate")).SelectedValue = dr.GetValue(7).ToString();
                      ((TextBox)LoginView1.FindControl("postalcode")).Text = dr.GetValue(8).ToString().Trim();
                      ((TextBox)LoginView1.FindControl("townstate")).Text = dr.GetValue(9).ToString();
                      ((DropDownList)LoginView1.FindControl("yearOfBirth")).SelectedValue = dr.GetValue(10).ToString();
                  }
                  dr.Close();
                  con.Close();
              }
  
              else
              {
                  dr.Close();
                  cmd = new SqlCommand(selectcmd, con);
                  dr = cmd.ExecuteReader();
                  while (dr.Read())
                  {
                      (this.LoginView1.FindControl("custEmail") as TextBox).Text = dr.GetValue(0).ToString();
                  }
                  dr.Close();
                  con.Close();
              }
          }
      }
  </code></pre>