C# 如何从动态添加的用户控件访问值?

C# 如何从动态添加的用户控件访问值?,c#,asp.net,user-controls,viewstate,C#,Asp.net,User Controls,Viewstate,我想从动态添加的用户控件中获取值 我正在尝试,但计数为0,因此条件失败 代码: .ascx.cs public string TextVisaNumber { get { return txtUser.Text; } set { txtUser.Text = value; } } public string VisaCountry { get { return dropCountry.SelectedVal

我想从动态添加的用户控件中获取值

我正在尝试,但计数为0,因此条件失败

代码:

.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


签证号码:
国家名称:
签证类型:
条目类型:
到期日
有什么想法吗?提前感谢

您需要在用户控件中创建。 并通过用户控件的id访问该属性。
请参阅和问题。

始终为要添加的控件分配id这是一个良好的做法

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

另外,由于要动态添加控件,您需要管理同一控件的视图状态,或者添加为您执行此操作的第三方控件。

@ShahroozJefriㇱ 离题:你多大年纪了?删除你的问题后,我的答案突然被2票否决?一个非常老,但投票最多,一个被接受?非常成熟的行为。干杯
    public string TextVisaNumber
    {
        get { return txtUser.Text; }
        set { txtUser.Text = value; }
    }

    public string VisaCountry
    {
        get { return dropCountry.SelectedValue; }
        set { dropCountry.SelectedValue = value; }
    }
<%@ 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>
Control ctrl = Page.LoadControl("~/UserControls/ReportControl.ascx");
ctrl.ID = "UniqueID";