c# - 将用户控件添加到 asp.net 页面

标签 c# asp.net user-controls asp-usercontrols

我用这样的事件创建了一个用户控件:

public partial class Person : System.Web.UI.UserControl
{
    public delegate void EditPersonHandler(object sender, EditPersonEventArgs e);
    public event EditPersonHandler EditPerson;
    protected void Page_Load(object sender, EventArgs e)
    {

    }
}

这是我的 EditPersonEventArgs :

 public  class EditPersonEventArgs:EventArgs
{
    private int id;

    public int ID
    {
        get { return id; }
        set { id = value; }
    }
    public EditPersonEventArgs(int ID)
    {
        id = ID;
    }

}

现在我想将此用户控件添加到我的页面,我这样做:

<%@ Register TagPrefix="UControl" TagName="UControlPerson" Src="~/SimpleUC/Person.ascx" %>

<div>
    <UControl:UControlPerson ID="UControlPerson" runat="server"
                             OnEditPerson="UControlPerson_EditPerson" />
</div>

这是我处理 OnSavePerson 的函数:

protected void UControlPerson_EditPerson(object sender, EditPersonEventArgs e)
    {

    }

我构建了解决方案。 但是当我呈现页面时出现此错误:

Description: An error occurred during the compilation of a resource required to service this
request. Please review the following specific error details and modify your source code 
appropriately. 


Compiler Error Message: CS0426: The type name 'SimpleUC' does not exist in the type 
'System.Web.UI.UserControl'

这是我的命名空间: 命名空间 UserControl.SimpleUC

最佳答案

您有命名空间冲突,您的用户控件命名空间 UserControl.SimpleUCUserControl 部分被解析为命名空间 System.Web.UI。用户控件

解决此问题的最简单方法是为您的控件选择另一个命名空间。可能类似于 UserControls.SimpleUC

关于c# - 将用户控件添加到 asp.net 页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10863081/

相关文章:

c# - Uri.MakeRelativeUri 错误地处理 ../

c# - 在 C# 中获取网络接口(interface)名称

c# - 通过 ActionFilter 检查 session 是否存在

Javascript 无法在 IIS7.5 上的网站内运行 - Windows Server 2008 R2

wpf - 用户控件的用途是什么?

asp.net - IE 中托管的用户控件呈现为文本框

c# - WPF 绑定(bind) ObservableCollection<string> 到 ItemsControl 不起作用

c# - 在c#中动态地将参数注入(inject)到函数调用中

asp.net - 嵌套中继器错误: Object reference not set to an instance of an object

javascript - 查找元素的目标