C# 无法识别ASP.net用户控件

C# 无法识别ASP.net用户控件,c#,asp.net,user-controls,C#,Asp.net,User Controls,我正在尝试将用户控件动态添加到asp.net网页。 用户控制代码: <%@ Control ClassName="CustomParameterView" %> <script runat="server"> public string Value { get; set; } public string Description { get; set; } public string Name { get; set; } private

我正在尝试将用户控件动态添加到asp.net网页。 用户控制代码:

<%@ Control ClassName="CustomParameterView" %>

<script runat="server">
    public string Value { get; set; }
    public string Description { get; set; }
    public string Name { get; set; }

    private void Input_OnTextChanged(object sender, EventArgs e)
    {
        Value = Input.Text;
    }

</script>
<div class="form-horizontal">
    <div class="form-group">
        <label class="col-sm-2 control-label" id="DisplayName"></label>
        <div class="col-sm-3">
            <asp:TextBox ID="Input" runat="server" CssClass="form-control" OnTextChanged="Input_OnTextChanged" />
        </div>
    </div>
</div>
不编译

我还看到有人试图在UC名称前添加
ASP.
,但效果不太好


我做错了什么?

看起来您没有将控件添加到占位符中

在.aspx页面中添加占位符:

<asp:PlaceHolder ID="PlaceHolder1" runat="server"></asp:PlaceHolder>

确保每次加载页面时都调用此代码,因此不要将其放入
中!IsPostBack
检查,否则控件将在回发后消失。

VDWWD post强调了占位符。此外,还需要在页面_Init上添加该用户控件(每次在回发时)。这里是一个链接,指向一篇博客文章,其中包含一个完整的示例站点,可以动态添加用户控件。
  var control = (CustomParameterView) LoadControl("CustomParameterView.ascx");
<asp:PlaceHolder ID="PlaceHolder1" runat="server"></asp:PlaceHolder>
var control = (CustomParameterView)LoadControl("~/CustomParameterView.ascx");
PlaceHolder1.Controls.Add(control);