title: How to access values from dynamically added user control?
body: <p>I want to get values from dynamically added user control</p>
  
  <p>I'm trying but count comes as 0 so condition  failed</p>
  
  <p>Code:</p>
  
  <pre><code>private const string VIEWSTATEKEY = "ContactCount";
      protected void Page_Load(object sender, EventArgs e)
      {
          if (!Page.IsPostBack)
          {
              //Set the number of default controls
              ViewState[VIEWSTATEKEY] = ViewState[VIEWSTATEKEY] == null ? 1 : ViewState[VIEWSTATEKEY];
  
              //Load the contact control based on Vewstate key
              LoadContactControls();
          }
      }
  
  
   private void LoadContactControls()
   {
      for (int i = 0; i &lt; int.Parse(ViewState[VIEWSTATEKEY].ToString()); i++)
      {
          rpt1.Controls.Add(LoadControl("AddVisaControl.ascx"));
      }
   }
  
   protected void addnewtext_Click(object sender, EventArgs e)
   {
       ViewState[VIEWSTATEKEY] = int.Parse(ViewState[VIEWSTATEKEY].ToString()) + 1;
       LoadContactControls();
   }
  </code></pre>
  
  <p><strong>EDIT</strong></p>
  
  <p>Here is the problem count takes as 0 so condition failed loop comes out</p>
  
  <pre><code>private void saveData()
  {
      foreach (var control in rpt1.Controls)
          {
              var usercontrol = control as VisaUserControl;
  
              string visaNumber = usercontrol.TextVisaNumber;
              string countryName = usercontrol.VisaCountry;
          }
      }
  }
  
  protected void Save_Click(object sender, EventArgs e)
  {
      saveData();
  }
  </code></pre>
  
  <p>.ascx.cs</p>
  
  <pre><code>    public string TextVisaNumber
      {
          get { return txtUser.Text; }
          set { txtUser.Text = value; }
      }
  
      public string VisaCountry
      {
          get { return dropCountry.SelectedValue; }
          set { dropCountry.SelectedValue = value; }
      }
  </code></pre>
  
  <p>.ascx</p>
  
  <pre><code>&lt;%@ Control Language="C#" AutoEventWireup="true" CodeBehind="AddVisaControl.ascx.cs" EnableViewState="false" Inherits="Pyramid.AddVisaControl" %&gt;
  &lt;%@ Register assembly="BasicFrame.WebControls.BasicDatePicker" namespace="BasicFrame.WebControls" tagprefix="BDP" %&gt;
  &lt;div id="divreg" runat="server"&gt;
  &lt;table id="tbl" runat="server"&gt;
      &lt;tr&gt;
      &lt;td&gt;
          &lt;asp:Label ID="lbl2" runat="server"&gt;&lt;/asp:Label&gt;
      &lt;/td&gt;
  &lt;/tr&gt;
  &lt;tr&gt;
  &lt;td&gt; Visa Number:&lt;/td&gt;
  &lt;td&gt;&lt;asp:TextBox ID="txtUser" Width="160px" runat="server"/&gt;&lt;/td&gt;
  &lt;td&gt; Country Name:&lt;/td&gt;
  &lt;td&gt;&lt;asp:DropDownList ID="dropCountry" Width="165px" runat="server"&gt;&lt;/asp:DropDownList&gt;&lt;/td&gt;
  &lt;/tr&gt;
  &lt;tr&gt;
  &lt;td&gt; Type of Visa:&lt;/td&gt;
  &lt;td&gt;&lt;asp:DropDownList ID="dropVisa" Width="165px" runat="server"&gt;&lt;/asp:DropDownList&gt;&lt;/td&gt;
  &lt;td&gt; Type of Entry:&lt;/td&gt;
  &lt;td&gt;&lt;asp:DropDownList ID="dropEntry" Width="165px" runat="server"&gt;&lt;/asp:DropDownList&gt;&lt;/td&gt;
  &lt;/tr&gt;
  &lt;tr&gt;
  &lt;td&gt; Expiry Date&lt;/td&gt;
      &lt;td&gt;&lt;BDP:BasicDatePicker ID="basicdate" runat="server"&gt;&lt;/BDP:BasicDatePicker&gt;&lt;/td&gt;
  &lt;td&gt;
  &lt;asp:Button ID="btnRemove" Text="Remove" runat="server" OnClick="btnRemove_Click" /&gt;
  &lt;/td&gt;
  &lt;/tr&gt;
  &lt;/table&gt;
  &lt;/div&gt;
  </code></pre>
  
  <p>Any ideas? Thanks in advance</p>