Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/29.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访问aspx页中的属性时出错_C#_Asp.net_Master Pages - Fatal编程技术网

C# 尝试从用户控件ascx访问aspx页中的属性时出错

C# 尝试从用户控件ascx访问aspx页中的属性时出错,c#,asp.net,master-pages,C#,Asp.net,Master Pages,我试图从用户控件中的aspx访问一个变量,如下所示 MyTestPage testPage = (MyTestPage)this.Parent; testPage.ID // i am trying to accessing id which exists in the aspx page. 我在尝试执行上述代码时遇到以下错误 **Unable to cast object of type 'System.Web.UI.WebControls.ContentPlaceHolder' to ty

我试图从用户控件中的aspx访问一个变量,如下所示

MyTestPage testPage = (MyTestPage)this.Parent;
testPage.ID // i am trying to accessing id which exists in the aspx page.
我在尝试执行上述代码时遇到以下错误

**Unable to cast object of type 'System.Web.UI.WebControls.ContentPlaceHolder' to type 'mynamespace.MyTestPage '.**
我在我的项目中使用母版页


我可以知道我在哪里犯错吗。如果您知道从aspx页面到ascx控件访问变量的更好方法,我愿意学习

    // Cast the loosely-typed Page.Master 
 MyTestPage  myMasterPage = Page.Master as MyTestPage; 
也检查一下这个


看看这篇非常好的文章,希望能对你有所帮助


我认为您应该在页面上将其公开为公共属性。我有10个控件,所有这些控件都需要来自aspx页面的属性,我不想在所有用户控件上都设置一个setter。您想在aspx页面上设置一个变量还是控件?您可以通过以下方式获得它。Parent.FindControl()或者至少是这个。Page@i要访问父页面中的变量,即aspx文件检查此项检查此母版页到内容页的交互您将了解详细信息我收到的错误为母版页的强制转换错误而不是possible@kobe--在此检查此母版页到内容页的交互详细的想法