0

I want to get values from dynamically added user control

I'm trying but count comes as 0 so condition failed

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 < 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();
 }

EDIT

Here is the problem count takes as 0 so condition failed loop comes out

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();
}

.ascx.cs

    public string TextVisaNumber
    {
        get { return txtUser.Text; }
        set { txtUser.Text = value; }
    }

    public string VisaCountry
    {
        get { return dropCountry.SelectedValue; }
        set { dropCountry.SelectedValue = value; }
    }

.ascx

<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="AddVisaControl.ascx.cs" EnableViewState="false" Inherits="Pyramid.AddVisaControl" %>
<%@ Register assembly="BasicFrame.WebControls.BasicDatePicker" namespace="BasicFrame.WebControls" tagprefix="BDP" %>
<div id="divreg" runat="server">
<table id="tbl" runat="server">
    <tr>
    <td>
        <asp:Label ID="lbl2" runat="server"></asp:Label>
    </td>
</tr>
<tr>
<td> Visa Number:</td>
<td><asp:TextBox ID="txtUser" Width="160px" runat="server"/></td>
<td> Country Name:</td>
<td><asp:DropDownList ID="dropCountry" Width="165px" runat="server"></asp:DropDownList></td>
</tr>
<tr>
<td> Type of Visa:</td>
<td><asp:DropDownList ID="dropVisa" Width="165px" runat="server"></asp:DropDownList></td>
<td> Type of Entry:</td>
<td><asp:DropDownList ID="dropEntry" Width="165px" runat="server"></asp:DropDownList></td>
</tr>
<tr>
<td> Expiry Date</td>
    <td><BDP:BasicDatePicker ID="basicdate" runat="server"></BDP:BasicDatePicker></td>
<td>
<asp:Button ID="btnRemove" Text="Remove" runat="server" OnClick="btnRemove_Click" />
</td>
</tr>
</table>
</div>

Any ideas? Thanks in advance

2 Answers 2

1

You need to create public property in your user control. And access that property through the id of the user control. See this and This question.

Sign up to request clarification or add additional context in comments.

1 Comment

@ShahroozJefriㇱ offtopic: how old are you that after deleting your question I've suddenly got 2 downvotes on my answers? One very old but most upvoted and one accepted? Very mature behavior. Cheers...
0

Always assign id to the controls you are adding its a good practice.

Control ctrl = Page.LoadControl("~/UserControls/ReportControl.ascx");
ctrl.ID = "UniqueID";

Also since you are adding controls dynamically you need to manage view state of the same, or add third party controls who do this part for you.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.