Getting error while trying to access a property in aspx page from a user control ascx
I am trying to access a variable from a aspx in user control as below
MyTestPage testPage = (MyTestPage)this.Parent;
testPage.ID // i am trying to accessing id which exists in the aspx page.
I am getting the following error while try to execute the above code
**Unable to cast object of type 'System.Web.UI.WebControls.ContentPlaceHolder' to type 'mynamespace.MyTestPage '.**
I am using master pages 开发者_开发百科in my project.
May i know where am i making the mistake. I am open to learn if you know any better technique to access a variable fro aspx page to ascx control
Have a look this very good article, hope it will help you
Mastering Page-UserControl Communication
and
Trigger Page Methods from a User Control
do this to access master page
// Cast the loosely-typed Page.Master
MyTestPage myMasterPage = Page.Master as MyTestPage;
also check this
Master Page To Content Page Interaction
精彩评论