如何初始化另一个控件的属性 asp.net

How to Init properties of another control asp.net

我在 asp.net 中有 2 个网络用户控件: 控制1 控制2

control1 包含属性:

    <script runat="server">
        public string MacName = "UNKOWN";
    </script>

    <h1><%= MacName %></h1>

control2 包含后面的代码:

    protected void Page_Load(object sender, EventArgs e)
    {
        control1 newControl = ((control1)Page.LoadControl("~/controls/control1.ascx"));
        newControl .[some code here]

        somePanel.Controls.Add(newControl);
    }

“[这里有一些代码]”在哪里我想知道如何从 control1 和 MacName 初始化属性。

我怀疑这实际上并不是在创建 class 成员:

<script runat="server">
    public string MacName = "UNKOWN";
</script>

相反,将其作为 class 级别 属性:

放在代码隐藏中
class control1
{
    public string MacName { get; set; }

    // etc.
}

那么您应该能够在该类型的任何实例上设置它:

newControl.MacName = "some value";