簡體   English   中英

無法識別ASP.net用戶控件

[英]ASP.net User control not recognized

我試圖將用戶控件動態添加到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>

我已經在.aspx文件中添加了注冊行:

<%@ Page Title="Home Page" Language="C#" MasterPageFile="~/Site.Master" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="PerCHW.Valkyrie.Server.WebApplication._Default" %>

<asp:Content ID="BodyContent" ContentPlaceHolderID="MainContent" runat="server">
    <%@ Reference Control="CustomParameterView.ascx" %>
...

問題是:

  var control = (CustomParameterView) LoadControl("CustomParameterView.ascx");

不編譯。

我還看到有人嘗試添加ASP. UC名稱之前,但效果不佳...

我究竟做錯了什么?

似乎您沒有將控件添加到PlaceHolder中。

在.aspx頁中添加PlaceHolder:

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

然后在后面的代碼中將控件添加到PlaceHolder:

var control = (CustomParameterView)LoadControl("~/CustomParameterView.ascx");
PlaceHolder1.Controls.Add(control);

確保每次加載頁面時都調用此代碼,因此請勿將其放在!IsPostBack檢查中,否則控件將在PostBack之后消失。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM