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

Last post 06-24-2008 3:54 AM by RutuPrash. 9 replies.

Sort Posts:

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

    06-21-2008, 2:53 AM
    • Participant
      835 point Participant
    • RutuPrash
    • Member since 01-04-2008, 12:36 AM
    • India
    • Posts 282

    Hi Friends,

    My problem scenario:
    I have a masterpage. In that I have 5 <a ...>Show Text</a> 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.

  • Re: How to access Master page file contents in user control page?

    06-21-2008, 3:08 AM
    Answer
    • All-Star
      28,218 point All-Star
    • johram
    • Member since 06-13-2006, 6:36 AM
    • Sweden
    • Posts 3,543
    • Moderator

    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!
  • Re: How to access Master page file contents in user control page?

    06-21-2008, 3:32 AM
    • Participant
      835 point Participant
    • RutuPrash
    • Member since 01-04-2008, 12:36 AM
    • India
    • Posts 282

    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 

  • Re: How to access Master page file contents in user control page?

    06-21-2008, 4:51 AM
    • Participant
      1,667 point Participant
    • tasnim5
    • Member since 06-08-2006, 4:24 AM
    • Mumbai, India
    • Posts 276
    Regards,
    Ahmed

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

    Thank you.
    www.ahmadtasnim.com
  • Re: How to access Master page file contents in user control page?

    06-21-2008, 5:04 AM
    • Participant
      835 point Participant
    • RutuPrash
    • Member since 01-04-2008, 12:36 AM
    • India
    • Posts 282

    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 

  • Re: How to access Master page file contents in user control page?

    06-21-2008, 6:12 AM
    Answer
    • Member
      22 point Member
    • samiradddd
    • Member since 05-19-2007, 6:58 AM
    • Posts 11
    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
  • Re: How to access Master page file contents in user control page?

    06-21-2008, 6:57 AM
    Answer
    • Contributor
      4,221 point Contributor
    • Coool
    • Member since 04-11-2007, 5:24 AM
    • Ahmedabad, India
    • Posts 652

    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, <MasterPageClassName>).<PublicFunction>

    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
  • Re: How to access Master page file contents in user control page?

    06-21-2008, 8:51 AM
    • All-Star
      28,218 point All-Star
    • johram
    • Member since 06-13-2006, 6:36 AM
    • Sweden
    • Posts 3,543
    • Moderator

    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!
  • Re: How to access Master page file contents in user control page?

    06-22-2008, 11:04 PM
    • Contributor
      4,221 point Contributor
    • Coool
    • Member since 04-11-2007, 5:24 AM
    • Ahmedabad, India
    • Posts 652

    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
  • Re: How to access Master page file contents in user control page?

    06-24-2008, 3:54 AM
    • Participant
      835 point Participant
    • RutuPrash
    • Member since 01-04-2008, 12:36 AM
    • India
    • Posts 282

    Thank you johram,cool and samir.

    I will try this solutions....

    Prashant V 

Page 1 of 1 (10 items)