User control can't access the parent page's public properties

Hi Guys

 I have built a page that is contained within a master page and it holds 2 user controls

Control A contains a DataList

Control B contains a FormView

 I want to pass the selected index from the DataList on Control A to the FormView on Control B so it acts like a master / detail scenario

 I created a public property on the page that contains both controls called DataListSelectedIndex

 A also created a public property for DataListSelectedIndex on the Control

 I populate the DataListSelectedIndex property of the Control when the DataList_SelectedIndexChanged event is fired

 And I populate the DataListSelectedIndex property of the page on the OnLoad event of the page.

My problem is that on Control B - (or Control A for that matter) I can't access the public property DataListSelectedIndex


The method I am trying to access this is through  - this.Page on the OnLoad even of the Control

Can anyone help? Thanks in Advance







2/27/2008 6:29:04 PM 3498 articles. 0 followers. Follow

5 Replies

Similar Articles

[PageSpeed] 19
Get it on Google Play
Get it on Apple App Store

The issue is that when you access this.Page through the control you are getting an instance of the base Page object, which does not have a DataListSelectedIndex property. You first need to cast the page object to the actual page you are on. Take a look in your pages codebehind, the class name is the type of page you want. You can then access the property from your page by doing:

((CODEBEHINDCLASSNAME)this.Page).DataListSelectedIndex = VALUE;

  This is bad practice though as it ties your control to a specific page. You can get round this in two ways. Firstly you could write a simple interface:

public interface IDataListClientPage
  int DataListSelectedIndex { get; set; }

 And implement it in your page using " : IDataListClientPage" after your class definition. You then change the code in the control to:

if (this.Page is IDataListClientPage)
  ((IDataListClientPage)this.Page).DataListSelectedIndex = VALUE;
  throw new Exception("Page must implement IDataListClientPage.");

  The other option is to use an event handler, so you put a SelectedIndexChanged event on the datalist and handle it on the page, this would fully decouple the controls for maximum flexibility:

Events in C# 

Please mark posts as the answer if I was helpful. Thanks!

Visit my blog
2/28/2008 8:28:27 AM

Hi Mate

Thanks for your reply, I'm nearly there now - just a couple of issues.

  • I cant cast to the class name of the codebehind page (it wont show up in intellisense)
  • How can I implement the IDataListClientPage interface if the page class is already Inheriting from  : System.Web.UI.Page?

I tried the event way also - thanks for that - its very complicated for my brain tho at the minute

I have an Issue with not being able to access an instance of ControlA from ControlB

I have created a sample project containing a page which contains 2 user controls

Control A (label and a button)

Control B (label and a button)

I created a delegate in the codebehind of ControlA but placed it outside the class for ControlA

I created a separate class for the Event args with one string property

I created an event inside the ControlA class 

I then created a method in ControlA codebehind which raises the event

then I got all confused and didn't know a) how to handle the event in Control B code behind b) Where to subscribe the event

Help much appreciated. - If I can crack this I reckon I'll be sorted.







2/28/2008 11:38:21 AM

 To use the interface just change the inhertience to : System.Web.UI.Page, IDataListClientPage

Remember that you can inherit from just one base type, but you can implement as many interfaces as you like.


With the events route - you would generally handle the event in your page, and then set the value in control B from the page level. That way B doesn't know where its data comes from. 

Please mark posts as the answer if I was helpful. Thanks!

Visit my blog
2/28/2008 11:46:52 AM

Sorted, I've sussed it now - thanks for the help.


I was well baffled for ages because I couldn't reference my controls from my page but there was something goin on in Visual Studio so I restarted it and that did the trick!



I still couldn't get this.Page to cast to (_Default) or even cast to the Interface so not sure what i did wrong there, but I don't need that now coz this will do nicely thanks 




2/28/2008 12:32:24 PM

May be you need to twik the page like this (Pass value from user control to page)

Thanks with regards,
Sheo Narayan

Please "Mark as Answer", if this helped.
7/15/2008 8:46:51 PM

Similar Artilces:

Unable to access user control's user control's function\property from another user control
Hi, I used to call an user control's user control function as stated below from my user control in ASP.Net 1.1 wucCompany.wucEmployee.GetEmployeeSomething() After migrating to ASP.Net 2.0, I am unable to use any properties/functions(even the public ones) of the user control's User control's from another user control.  The way I have to do is create property\function in wucCompany which calls the wucEmployee's property\function and call the wucDepartment.GetEmployeeSomething().Since I need to do this change in too many places, I can...

Label control's text doesn't appear to be set (in the page which has a user control )when trying to update the text property in the FormView's ItemUpdated event
Hi There,I have a user control which hosts a formview control, gridview control and a label control. The grid view is usedto show rows in the DB and when clicked to edit each row, a form view opens up in edit mode with that particular row info. So I have nothing in the item template.My first question it mandatory to have smth in the itemtemplate of the form view.I have controls in edititemtemplate and insertitemtemplate.When a particular row is edited, I would like to show a status msg showing that the row has been updated.For this, I use the ItemUpdated event of the formview and try ...

Can I hide a user control's control from the hosting page's client script?
Okay, my previous question was not unswered and I guess you can't hide a control's panel with server code then show it with client code. Now this is what I try to do: on the hosting page's onload event, call a function to hide the user control's penal. Here is the deal: the user control was looped as many times as data required. So the penals will be name mypanel1, my panel2, etc... that is not even a problem if I know if the following is true: * Do all server codes finish running before the page get sent back to the client? Say the database gave me 5 penals, I can then determine how...

Access parent page's controls from a user control
I have a .ascx user control that contains a button. I place this user control on a .aspx web page. The web page contains a textbox. When I click on the button in the user control I want the data in the textbox (in the web page) to be saved to the database. I want to write this code in the click event of the user control's button. Is there any way I can access the parent web page's controls from the user control? Add event to user control which fires on page when button inside user control is clicked. view post 779045Jigar Desai-----------------------Do not forget to "Mark as Answer...

User Control Within UserControl
If I have a user control A and then inside it a user control B embedded/referenced within it.  User Control A has a panel defined (cpeNewUser) that I want to manipulate from the code behind of User Control B based on some event that happens.So putting this in my code behind of User Control B won't work as it has no idea of cpeNewUser:cpeNewUser.Collapsed = true;So is it because only at runtime they are married up and because of this you cannot access each other's objects at compile time?  How would I get this to work becuase I'm using all user controls inside a AJAX Too...

How can I access Master page properties in a user control's code-behind?
I have a  page that uses a master page, and also has a user control on it, I would like to acces the master page class properties from code-behind in user control. Ho could I do this? Note I am NOT asking how to access Master page properties in page's code behind but a user control's code behind.sun21170 UserControl has the Page property. In Page property, you can to access the MasterPage. Is it posible you coul dgive a code example .... could use this functionality myself ... many thanks I just found the answer to my question. The answer is as below and it's in ...

When outside the Server Control, ObjectServerControl can't access components within Server Control.
Hello all,I've written a server control and the code goes above. This is a ripped version of my code with all functionalities but without redundancies. I just put one template to ilustrate the problem. My server control has more than one template.using System;using System.ComponentModel;using System.ComponentModel.Design;using System.Security.Permissions;using System.Drawing;using System.Web.UI;using System.Web.UI.Design;using System.Web.UI.Design.WebControls;using System.Web.UI.WebControls;using System.Collections;using System.Text;using System.Web;namespace FormRows{    ...

Can a User Control's Page_Load retrieve a value set in the parent's Page_Load?
I want to use a control to perform a specific task, but I want it to vary based on the page it is on. I was hoping to set a value in the Page_Load event of the Parent, and have that value retrieved by the Page_Load event of the user control. I am not sure what order these events fire in, Can I be certain that the parent event will fire first?...

Accessing a control's properties in another user control?
I have two user controls on the page, control A and control B. Control B has a bunch of panels in it that I need to access from Control A. Control A needs to lookup a panel in Control B and turn its visibility on or off based on some criteria. Using the FindControl() method how can I accomplish this? I have no problem accessing a control from the Page or from within the same control, but I haven't figured out how to access the properties of a different control than the one thats requesting the info. Thanks. You *could* do a FindControl on the Parent of UserControl A, to get a reference t...

Accessing Parent's control form User Control?
Hi, I have a Webform and a User Control in it. The form has a panel and textbox in it. The User control has a button in it. Using that Button, I want to show/hide the panel. But, my issue is how to get access to Parent's controls (in this case, the parent control is Panel). Please advice. Thanks in advance. Easy... just make the controls you want to access in the Page public and then cast the Page in the control to the appropriate type and access the now public controls.... uhh??? got that ;-) Example: Page: Name MyPage Controls in MyPage: public System.Web.UI.WebCo...

Can't access controls from codebehind in a user control
I'm attempting to make the transition from 1.1 to 2.0 and I'm trying to understand the "codebeside" page model.  Here's my problem: I have a user control.  It inherits from a base class which in turn inherits from system.web.ui.usercontrol.  So it looks something like this: public partial class MyControl : MyBaseUserControlClass{ }public class MyBaseUserControlClass : System.Web.UI.UserControl{ } If I add a control, such as a button to the user control, I am not able to access it in codebehind.  I get the error 'MyControl does not contain a definiti...

Can I have a web user control call a method on the page it's parent page?
I'm building an application where I'm using serveral different controls on a page and as one of them causes a postback I may want the page to make changes to other controls.  Is there a way to either fire and event off from the control that the page could then capture or simply have the control call a method of it's parent page? Obviously the Page object is there to work with, but I can't seem to figure out how to specifically cast it as a specific page.  For real man, In your user control you need to create a public event which is raised by the event of the...

How can I access the property a control on a master page from the master page's code behind.
On my master page I have a label. I want to change this label's text property based on an element of the querystring. (label.text = "NewValue")All child pages using this master will display the same thing so I was trying to put the code in the master page so as not to have to duplicate it for the child pages or put it in a base class and use FindControl.Is it possible to access from a master page's codebehind the property of a server control that is placed on a master page's aspx? If so how?(I saw similar questions, but they all came from the view of a child accessing the property.)Thank you...

accessing a dynamically loaded child's page user controls(ascx) from a parent page(aspx)
hi all,i've search the forums and i still can't get it to work.ok here goes...i've created a dropdownlist in the parent(aspx) page, this dropdown will post-back on and load a different child(ascx) page onto a Placeholder each time the user chooses a different selection in the the child(ascx) page, say i have a textbox. in the parent(aspx) page, i have a submit button.the placeholder loads accordingly to the dropdownlist's selection, so that is now the problem is, how do i capture the text in the textbox inside the child(ascx) page from the parent(aspx) p...

Web resources about - User control can't access the parent page's public properties -

Resources last updated: 12/11/2015 2:34:35 AM