[英]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.