How to access Master page file contents in user control page?

Hi Friends,

My problem scenario:
I have a masterpage. In that I have 5 Show Text parts ok.
I have created a default.aspx. I referenced the masterpage in it.
I have now products.ascx user control file.
This will be shown in Default's contentpage holder right.

Now i want to access masterpage file's anchors along with its text in products.ascx . How can i achieve it? or is there any workaorund for it?
pl fell free if my problem dowsn't elaborate itself.

Thanks

Prashant V.

0
RutuPrash
6/21/2008 6:53:26 AM
📁 asp.net.web-forms
📃 93655 articles.
⭐ 1 followers.

💬 9 Replies
👁️‍🗨️ 234 Views

Your user control shouldn't access the Master page directly, as it will lead to a direct dependency between the two. Instead, use the page as a "broker" between them, so that the page forwards the value from the master page by setting the value in a property on the user control. Let me know if you need code example.


If this post was useful to you, please mark it as answer. Thank you!
0
johram
6/21/2008 7:08:29 AM

Yes Mr.johram,

I didn't get what u stated. pl elaborate it.

a code sample will also do either.

Thank you for quick reply,

Prashant 

0
RutuPrash
6/21/2008 7:32:22 AM

Hi mate,

Try to visit :

http://blogs.ittoolbox.com/c/coding/archives/events-in-master-page-content-pages-16262

http://msdn.microsoft.com/en-us/library/system.web.ui.masterpage(VS.80).aspx


Regards,
Ahmed

***************************************************
Please don't forget to mark the post as Answer, which helps you.

Thank you.
www.ahmadtasnim.com
0
tasnim5
6/21/2008 8:51:48 AM

Hi tasnim,

BTW my problem is how to access master file contents from a user control page that is contained within a default.aspx page which inherits masterpage.

pl feel free to reply if my problem is not self describing.

Thanks

Prashant V 

0
RutuPrash
6/21/2008 9:04:32 AM
Subject
First make the properties in the user control of the value which u want from master page like
 

Property cProductCode() As String

Get

Return lblcProductCode.Text

End Get

Set(ByVal value As String)

lblcProductCode.Text = value

End Set

End Property

then i s urs Default page first fetch the values from master page

like

usercontrolId.cProductCode=Ctype(Master.FindControl("productcode"),TextBox).Text

and the call urs function like

usercontrolId.Initiate_Control()

 

Please mark the answer if u find this post usefull


Always Remember to mark the answer

Samira Mirza
Sr. Software Engineer
Visionaire Technologies
Chandigarh
0
samiradddd
6/21/2008 10:12:28 AM

Hi RutuPrash,

One quick question. do u want to get the text of ur anchor tag at server side?

if yes then u can refer page of UserCntrol using Me.Parent from ascx page.

now u want to fine the Master Page of the aspx page which u can find using

Me.Parent.Master and then u can use findControl Method.

 if u want to access method of Master page which return the list of Text then u can create one public method in Master page and then call it like

DirectCast(Me.Parent.Master, ).

Think and enjoy the power of OOP. It will lead to almost all solutions in ASP.NET which u generally not find in any other web language.

Cheers

 


Please mark as Answer if it helps u. Thanks!

Parth Patel
Techsture Technologies
Software Developer
Ahmedabad
0
Coool
6/21/2008 10:57:22 AM

I second samiradddd. Although the example is written in VB.NET. That's the approach I also suggested.

As for Coool's suggestion, it is technically possible although you should be aware that this leads to tightly coupled code - something which may not be a problem if the controls are very tightly related - but something you may want to avoid in most scenarios. Especially when working with user controls, since the OOP concept of reuse is one of the main reasons for building user controls.


If this post was useful to you, please mark it as answer. Thank you!
0
johram
6/21/2008 12:51:44 PM

Sorry, but in my previous post i made a mistake.

U can access Parent page using Me.Parent.Page instead of Me.Parent.


Please mark as Answer if it helps u. Thanks!

Parth Patel
Techsture Technologies
Software Developer
Ahmedabad
0
Coool
6/23/2008 3:04:40 AM

Thank you johram,cool and samir.

I will try this solutions....

Prashant V 

0
RutuPrash
6/24/2008 7:54:00 AM