0

This is in my user control

<div class=" row-fluid">
<div class="span4" style="overflow: hidden;">
    <asp:DropDownList ID="ddlAccountName" runat="server" DataSourceID="ChartOfAccountsforVouchers"
        DataTextField="AccountName" DataValueField="ChartOfAccID">
    </asp:DropDownList>
   <%-- <asp:EntityDataSource ID="ChartOfAccountsforVouchers" runat="server" ConnectionString="name=CnFV1MainModelEntities"
        DefaultContainerName="CnFV1MainModelEntities" EnableFlattening="False" EntitySetName="ChartOfAccounts"
        Select="it.[ChartOfAccID], it.[AccountName]">
    </asp:EntityDataSource>--%>
</div>
<div class="span4">
    <asp:TextBox ID="txtValue" MaxLength="10" CssClass="numeric input-block-level" runat="server"></asp:TextBox>
</div>
<div class="span4">
    <asp:TextBox ID="txtValueB" MaxLength="10" CssClass="numeric input-block-level" runat="server"></asp:TextBox>
</div>

And this is how i am adding this control to a placeholder

int count = Convert.ToInt32(Session["AddedRows"]);
        ++count;
        for (int i = 0; i < count; i++)
        {
            // Add the usercontrol dynamically
            Control webUserControl = (Control)Page.LoadControl("ctrlEntryRows.ascx");
            placeRows.Controls.Add(webUserControl);
        }

        Session["AddedRows"] = count.ToString();

And this is how on a button click i am trying to get the controls

if (placeRows.Controls.Count > 0)
            {
                foreach (Control ctrl in placeRows.Controls)
                {
                    TextBox txt = (TextBox)placeRows.FindControl("txtValue");
                    TextBox txtB = (TextBox)placeRows.FindControl("txtValueB");
                    string message = txt.Text + txtB.Text;
                }
            }

But the code shows 0 count.IT doesn't even get in the loop. How do i get the values of my dynamically added user controls?

Thank you

Monzir
  • 621
  • 4
  • 9
  • 29

1 Answers1

1

try this

if (placeRows.Controls.Count > 0)
            {
                foreach (Control ctrl in placeRows.Controls)
                {
                    TextBox txt = (TextBox)ctrl.FindControl("txtValue");
                    TextBox txtB = (TextBox)ctrl.FindControl("txtValueB");
                    string message = txt.Text + txtB.Text;
                }
            }

UPDATE:

You should read some articles about dynamic controls & state management like these

  1. http://www.codeproject.com/Articles/3684/Retaining-State-for-Dynamically-Created-Controls-i
  2. https://web.archive.org/web/20210330142645/http://www.4guysfromrolla.com/articles/092904-1.aspx
  3. Maintain the state of dynamically added user control on postback?
Community
  • 1
  • 1
yogi
  • 19,175
  • 13
  • 62
  • 92