Wrox Programmer Forums
| Search | Today's Posts | Mark Forums Read
ASP.NET 1.0 and 1.1 Professional For advanced ASP.NET 1.x coders. Beginning-level questions will be redirected to other forums. NOT for "classic" ASP 3 or the newer ASP.NET 2.0 and 3.5
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ASP.NET 1.0 and 1.1 Professional section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
  #1 (permalink)  
Old May 21st, 2005, 02:50 PM
Authorized User
 
Join Date: May 2005
Location: , , United Kingdom.
Posts: 13
Thanks: 0
Thanked 0 Times in 0 Posts
Default Accessing Properties in User Controls

(I belive I may have originally posted this in the wrong forum - appologies for that)

How do I programatically access a property in a parent webform from a User Control?

e.g. if we had a web form that had the following property:

public string myProperty
{
  get{ return _myProperty;}
  set{ _myProperty = value;}
}

How can I access this property from a user control that in inside this web form page?
Many thanks. Andy

__________________
Andy has a keen interest in home security and home alarm systems. Improve your home security now by going to the The Best Home Security Guide.
  #2 (permalink)  
Old May 22nd, 2005, 07:09 AM
planoie's Avatar
Friend of Wrox
Points: 16,481, Level: 55
Points: 16,481, Level: 55 Points: 16,481, Level: 55 Points: 16,481, Level: 55
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Aug 2003
Location: Clifton Park, New York, USA.
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
Default

Andy,

Doing this breaks the model of the user control. The user control should be an autonomous unit that is not expected to live on a particular webform (which is what you are asking by expecting a particular property to be on the control's parent).

What are you trying to achieve? If your goal is to get some value back up to the web form, then I'd suggest one of two options:

1. Provide a public property on the user control so the web form can access it. This is better because the webform knows what controls will be on it, versus the user control NOT knowing on what web form it will be used.

2. Provide a public event on the user control so you can "tell" the webform about something that happened. And example would be a "SomethingChanged" event. Then raise that event on the user control and handle that event on the web form.

-Peter
  #3 (permalink)  
Old May 22nd, 2005, 05:54 PM
Authorized User
 
Join Date: May 2005
Location: , , United Kingdom.
Posts: 13
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks for the reply Peter. This is what I was trying to do......
There is a button on the user control and when the user clicks it, and its event fires, then (amongst other things) retrieve a value from the parent web form page and fire off a stored procedure. I know this "ties" this user control to only work in this web form only - I understand that. I wouldnt normally do this sort of thing with a user control. The reason I am doing this is that I want the flexibility of loading another user control in the original ones place, that is a slight variation of the first user control.
Anyway I have found out how to reference the parent's public property with the code as follows:

Int32 _totalRecords = ((aqreview.MPRSearch.MPRList)this.Parent.Page).Tot alRecords;

where aqreview.MPRSearch.MPRList is the reference to the parent web form and TotalRecords is the public property on that web form.

Thanks for your help.


  #4 (permalink)  
Old May 23rd, 2005, 07:50 AM
planoie's Avatar
Friend of Wrox
Points: 16,481, Level: 55
Points: 16,481, Level: 55 Points: 16,481, Level: 55 Points: 16,481, Level: 55
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Aug 2003
Location: Clifton Park, New York, USA.
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
Default

You can certainly do what you are doing but it's not a good habit to get into. May I suggest the following alternative:

Provide a public property on the user control for the required value. Let the page that includes the control set that value when it changes (either on the initial page request or if it changes based on something that happens on the page). This way you don't break the user control model, but you get the desired behavior.

-Peter
  #5 (permalink)  
Old May 25th, 2005, 06:40 PM
Authorized User
 
Join Date: May 2005
Location: , , United Kingdom.
Posts: 13
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Yes I'm thinking of re-coding what I've done actually. I was going to have an event which would be raised off the button in the user control. Then have a corresponding event handler in the webform which would fire a method to set the public value in the user control.
Thanks again for your help. Andy
  #6 (permalink)  
Old June 29th, 2005, 12:07 PM
Registered User
 
Join Date: Jun 2005
Location: , , .
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Based on the above I have a question... How do you use the public property from the user control in the parent webform?

In the user control I have:

Private Viewmodevalue As String = ""
Public Property Viewmode() As String
  Get
    Return Viewmodevalue
  End Get
  Set(ByVal Value As String)
    Viewmodevalue = Value
  End Set
End Property

I can set the value in the parent webform using:

Public Class Home
    Public Top

    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs)
        Top.Viewmode = "2"
    End Sub

End Class

First, is this the good method to set a public propery?

Second, how do I get/retrieve the public property value in the parent webform (if I want to use it on another place, or if I have set the property in the webcontrol, but want to retrieve it in the parent webform)?

Third, can I also set and get the public property from the user conrol in another user control?

Thanks in advance! Martin

  #7 (permalink)  
Old June 29th, 2005, 09:26 PM
planoie's Avatar
Friend of Wrox
Points: 16,481, Level: 55
Points: 16,481, Level: 55 Points: 16,481, Level: 55 Points: 16,481, Level: 55
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Aug 2003
Location: Clifton Park, New York, USA.
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
Default

1) This is a good method.

2) You get/retrieve the value from the public property just like you are assigning it your Page_Load code. I think what you might be asking however is "how do I get the value if the user control code changes it?"

This is where events come into play. You create a public event on your user control and the page that consumes the control handles that event.

3) This is done by either having the page wire 1 user control's event to another's public handler for it, or by handling the event on the page and calling a method or by setting a property on the other control.

-Peter
  #8 (permalink)  
Old June 30th, 2005, 04:23 AM
Registered User
 
Join Date: Jun 2005
Location: , , .
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Peter, thanks a lot for helping out.

Basically what I'm trying to achieve is this:
I have a set of pages and they all use the same user controls named top, menu and bottom. Menu is only showed in the pages if a user has logged in. The user control top is the one that matters, there I like to set a lot of variables/properties for the pages to use. For example a userID, so I don't have to retrieve it in every page trough queries and all, but can ask it from the user control.

A lot is basically interaction from user control to page, like userID, viewmode, usergroup, etc. I set these values in the user control, I use them both in the usercontrol and in the page. Some of the values I also want to user in the menu user control. From the page I only set one value in the user control being systempageID (which tells the user control which page it's handling, I guess that's bad, but I need it for knowing some variables for display).

At this moment I think the public properties are an easy way to handle such stuff, but I'm not sure. Consering the second answer you gave, I guess I'm already doing that:

Public Class Home
  Public Top

  Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs)
    Top.Systempage = "2"
    Top.Top_Load()
  End Sub

End Class

Top_Load in the Top user control does:

Public Sub Top_Load()
  '## Maintenance check
  Onderhoud()

  '## No cache
  Response.Cache.SetCacheability(HttpCacheability.No Cache)

  '## Retrieve systemdata
  Dbconnect.Connection.Open()
  Dim SQL = "SELECT Systemtitle, Nformat1, Nformat2 FROM System"
  Dim Sqlcommand As New OdbcCommand(SQL, Dbconnect.Connection)
  Dim Sqlreader As OdbcDataReader = Sqlcommand.ExecuteReader()
  Sqlreader.Read()
  Systemtitle = Sqlreader.Item(0) 'Public propery
  Nformat1 = Sqlreader.Item(1) 'Public propery
  Nformat2 = Sqlreader.Item(2) 'Public propery
  Dbconnect.Connection.Close()

  '## Set userinfo
  If (Not ((Systempage = "1") Or (Systempage = "0"))) Then
    Userinfo()
  End If
End Sub

Based on this, is this the correct way to do what I want, or am I doing it completely wrong here?

About the third answer, so it's not possible to retrieve the value from one user control to another user control without involving the page?

Thanks, Martin

  #9 (permalink)  
Old June 30th, 2005, 07:51 AM
planoie's Avatar
Friend of Wrox
Points: 16,481, Level: 55
Points: 16,481, Level: 55 Points: 16,481, Level: 55 Points: 16,481, Level: 55
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Aug 2003
Location: Clifton Park, New York, USA.
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
Default

There are no "wrong" ways of doing things. There are just "better" ways.

It sounds like much of what you are doing with one of those user controls is something that should be moved to page functionality. You should concider superclassing your page.

Create a class ("BasePage" for example) that performs all the tasks you need to perform on each page. This is a better approach because it follows a more logical object design structure. The tasks you are performing are really page tasks so they should live in a page class. Then all your pages can derive from that page class. All derived pages will get all the common page tasks with a single line of code "Inherits BasePage".

This technique also solves the problem of getting user controls to talk to each other because the common tasks and data associated with them live in the base page class not in a user control.

Some years ago, I developed a technique to provide commonly used data to all pages and user controls in an application. Here's the article I wrote explaining it:

http://www.geekdork.com/WebSkeleton/...aspx?section=5

This technique may greatly simplify what you are trying to do.

-Peter
  #10 (permalink)  
Old July 1st, 2005, 09:08 AM
Registered User
 
Join Date: Jun 2005
Location: , , .
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks for the great solution you provided Peter!

I'm using the basepage structure now, really helpfull, and it works great In the user controls I just use something like CType(Me.Page, Basepage).Systempage instead of a "inherits basepagecontrol" as well, because I do not use it there a lot anyway, and also I can have some VS.NET designer use. To bad the VS.NET designer doesn't work anymore for the page classes, but by changing it to "inherits page" before loading the designer (and then in the code behind back to "inherits basepage") it works anyway, so no big problems.

At first I also considered session for this task, but this is a much better solution...

Thanks again, solved my problems here!






Similar Threads
Thread Thread Starter Forum Replies Last Post
Properties In User Controls - A Tip And Trick Muhammad Zeeshan ASP.NET 2.0 Basics 0 November 23rd, 2007 03:51 AM
Properties In User Controls - A Tip And Trick Muhammad Zeeshan ASP.NET 2.0 Professional 0 September 20th, 2007 10:49 PM
Accessing the Controls of a User Control? Aaron Edwards ASP.NET 2.0 Basics 6 June 16th, 2006 07:22 PM
Accessing Properties in User Controls andyj00 Classic ASP Professional 1 May 21st, 2005 02:52 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.