Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Access control in main page from user control 1

Status
Not open for further replies.

Peppi

Programmer
Apr 9, 2001
205
CA
Hi,

Just wondering if and how I can access a checkbox on my main page from my user control?

Thx.
 
I'd create a Public Property on the page that exposes whatever you need to do to the CheckBox.


____________________________________________________________

Need help finding an answer?

Try the Search Facility or read FAQ222-2244 on how to get better results.

 
ca8msm's was is definately the proper way.
You can howerver do this from your user control:
Code:
 cb = Me.Page.FindControl("Checkbox1")
This is not reccomended because it defeats the reusability of the control.
 
OK, once I have the property how do I reference it in the user control?
 
I had tried that but it doesn't work.

In my aspx page:

Public ReadOnly Property ShowDeclinedRequests() As Boolean
Get
Return chkDeclined.Checked
End Get
End Property

In my user control:

If Me.Page.ShowDeclinedRequests Then

It tells me that it's not a member of System.Web.UI.Page.
 
Try converting the type to your page first. e.g.
Code:
Ctype(Me.Page, myPage).myProperty


____________________________________________________________

Need help finding an answer?

Try the Search Facility or read FAQ222-2244 on how to get better results.

 
Don't forget that your 'Page' is essentially just a class inheriting from System.Web.UI.Page, of which 'ShowDeclinedRequests' is not a property. I believe, (without testing) you'd need to cast the Parent.Page to the 'type' of your Page Class. I.E., If your Page class was defined as, (c#);
Code:
public partial class MyPage: System.Web.UI.Page
you would have to cast the Parent.Page to the 'MyPage' type which of course has the 'ShowDeclinedRequests' property. This however takes you back to the issue of defeating usability.

If you define a base Page class with this property for all of your pages to inherit from you could then cast the Parent Page to the base Page class, knowing that all of your pages would then encapsulate this property, but personally I think it's a bit circular having a user control reference a custom property of a container page as you're seriously restricting the use of said control.

Rhys
The use of COBOL cripples the mind; its teaching should, therefore, be regarded as a criminal offense Edsgar Dijkstra

Church of the Flying Spaghetti Monster
 
I think my problem stems from the fact that my page class is not being recognized as a valid type in my user control. So I can't cast it to the page class type. How do I get it to be recognized?
 
I can't help but ask before you go any further, but I'm assuming what you're trying to achieve is have a User Control to display 'declined requests', or not, and a checkbox on the page to flag whether declined requests should be displayed.

If this is the case why not encapsulate the checkbox in the User Control and make in entirely re-usable?

Rhys
The use of COBOL cripples the mind; its teaching should, therefore, be regarded as a criminal offense Edsgar Dijkstra

Church of the Flying Spaghetti Monster
 
The 'declined requests' checkbox needs to be at the page level so that the user can select it once and it will apply for all grids. I have six grids so we don't want to have to force the user to select six checkboxes. The grids will show all active requests regardless, but if the checkbox is selected, then I need to show declined requests as well as active one's.

So what I'm doing in my user control is trying the check the state of the checkbox in my DataSource_Selecting event so that I can set the SelectMethod of the DataSource to the appropriate method. Hence my dilemma with accessing the checkbox.
 
Does this mean you've got six user controls containing grids that will, (as per the checkbox), show declined requests? Can you confirm the basic architecture you've designed?

This depends on your design, but essentially what I'm thinking is if you make the ShowDeclinedRequests property a property of the user control, you could then control this property in all of the instances of the same type of User Control on the page via a single checkbox, and its OnCheckChanged event handler.

Basically, instead of trying to read a property of a Page, (or of another control contained within the page), from a User Control, use a Page level control to get/set a property in a User Control.

Make sense?

Rhys
The use of COBOL cripples the mind; its teaching should, therefore, be regarded as a criminal offense Edsgar Dijkstra

Church of the Flying Spaghetti Monster
 
Yes, that totally works for me!
Thanks so much.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top