[英]ASP.net User control not recognized
I am trying to add a user control dynamically to an asp.net web page. 我试图将用户控件动态添加到asp.net网页。 user control code:
用户控制代码:
<%@ 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>
I have added the register line in the .aspx file: 我已经在.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" %>
...
The problem is that this: 问题是:
var control = (CustomParameterView) LoadControl("CustomParameterView.ascx");
does not compile. 不编译。
I also saw people trying ti add the ASP.
我还看到有人尝试添加
ASP.
before the UC name but that does not work as well... UC名称之前,但效果不佳...
What am I doing wrong? 我究竟做错了什么?
It looks like you are not adding the control to a PlaceHolder. 似乎您没有将控件添加到PlaceHolder中。
In the .aspx page add a PlaceHolder: 在.aspx页中添加PlaceHolder:
<asp:PlaceHolder ID="PlaceHolder1" runat="server"></asp:PlaceHolder>
And then add the Control to the PlaceHolder in code behind: 然后在后面的代码中将控件添加到PlaceHolder:
var control = (CustomParameterView)LoadControl("~/CustomParameterView.ascx");
PlaceHolder1.Controls.Add(control);
Make sure this code is called every time the page is loaded, so don't put it inside a !IsPostBack
check or the Control will be gone after PostBack. 确保每次加载页面时都调用此代码,因此请勿将其放在
!IsPostBack
检查中,否则控件将在PostBack之后消失。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.