以编程方式添加控件-并访问其参数

本文关键字:访问 参数 控件 编程 方式 添加 | 更新日期: 2023-09-27 18:02:53

我有以下控制:

public partial class Controls_ProductPanel : System.Web.UI.UserControl
{
    private int _ProductID;
    public int ProductID
    {
        get
        {
            return _ProductID;
        }
        set
        {
            _ProductID = value;
        }
    }
    protected void Page_Load(object sender, EventArgs e)
    {
    }
}

我希望他能像这样把它添加到一个页面:

Controls_ProductPanel panel = new Controls_ProductPanel();
panel.ProductID = 2;
Page.Controls.Add(panel);

但是Visual Studio似乎无法识别Controls_ProductPanel

:

 The type or namespace name 'Controls_ProductPanel' could not be found. (Are you missing a using directive or an assembly reference?)

以编程方式添加控件-并访问其参数

为了解决这个问题,我不得不将注册标签放在.aspx页面上。这允许我访问它的类型。

 <%@ Register TagName="ProductPanel" TagPrefix="uc" Src="~/Controls/ProductPanel.ascx" %>

看看这个线程:

找不到类型或命名空间名称

希望有所帮助