开发者

Getting error while trying to access a property in aspx page from a user control ascx

开发者 https://www.devze.com 2023-03-01 00:59 出处:网络
I am trying to access a variable from a aspx in user control as below MyTestPage testPage = (MyTestPage)this.Parent;

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

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号