Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/36.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 控件ascx不存在_C#_Asp.net - Fatal编程技术网

C# 控件ascx不存在

C# 控件ascx不存在,c#,asp.net,C#,Asp.net,我有一个Web应用程序项目,其结构如下 ProjectName -Controls --WebControls --MyControl.ascx -CustomerControls --CustDetails.ascx 在CustDetails.ascx控件中,我试图加载MyControl.ascx,因此我在页面上添加了一个引用(而不是codebehind),即 但是我收到了“文件'/MyControl.ascx'不存在。” 我试着改变道路,以 ~/Controls/WebControls/

我有一个Web应用程序项目,其结构如下

ProjectName
-Controls
--WebControls
--MyControl.ascx

-CustomerControls
--CustDetails.ascx
在CustDetails.ascx控件中,我试图加载MyControl.ascx,因此我在页面上添加了一个引用(而不是codebehind),即

但是我收到了“文件'/MyControl.ascx'不存在。”

我试着改变道路,以

~/Controls/WebControls/MyControl.ascx
但同样的错误。改成

../Controls/WebControls/MyControl.ascx
然后get“无法使用前导..退出顶部目录上方。”

我试过各种变体,搜索过谷歌,但我不明白哪里出了问题

编辑1


目录图片附件

请注意此处的目录结构:

项目名称

-控制

--网络控制

--MyControl.ascx

它说MyControl.ascx在Controls目录中,而不是WebControls

您有两种选择:

1) 将MyControl.ascx移动到WebControls目录,重新生成解决方案,这些代码将起作用:

<%@ Register Src="~/Controls/WebControls/MyControl.ascx" TagPrefix="ctrl" TagName="CustControl" %>
2) 将其保存在您列出的同一目录中(控制目录中的MyControl.ascx),并将上述代码中的路径更改为:

<%@ Register Src="~/Controls/MyControl.ascx" TagPrefix="ctrl" TagName="CustControl" %>
无需使用Server.MapPath

此外,如果在MyControl code behind类中定义了公共方法或属性,则需要将加载的控件强制转换为MyControl,以便像我在这里所做的那样访问这些方法和属性:

LoadControl = Page.LoadControl("...") as MyControl;
//or
LoadControl = (MyControl)Page.LoadControl("...");

网站或Web表单模板?它是一个Web应用程序(当我构建它时,它会在bin目录中创建一个dll)检查这个:@Efe,URL显示从根目录加载。我相信问题是,;如何从深层目录/Controls/WebControls/abc.ascx加载?@Anil谢谢你的观点,这是一个不理解的错误,我想路径是Controls/WebControls/MyControl。ascx@Anil是的,我想是的,我现在刚刚测试过它,“~/path/to/MyControl.ascx”工作得很好。@Computer,你能添加你尝试时遇到的错误吗“我尝试将路径更改为~/Controls/WebControls/MyControl.ascx,但出现了相同的错误“,根据Efe,这应该有效。谢谢各位,我会看看上面给出的建议。我已经附加了一个目录结构的图像,以避免任何错误confusion@Computer那么选项1肯定会完成这项工作。注册控制器时不要使用“..”。如果您想确定,只需从解决方案资源管理器中拖动MyControl.ascx并将其放入CustControl.ascx页面,您将看到“~”而不是“…”。
LoadControl = Page.LoadControl("~/Controls/WebControls/MyControl.ascx");
<%@ Register Src="~/Controls/MyControl.ascx" TagPrefix="ctrl" TagName="CustControl" %>
LoadControl = Page.LoadControl("~/Controls/MyControl.ascx");
LoadControl = Page.LoadControl("...") as MyControl;
//or
LoadControl = (MyControl)Page.LoadControl("...");