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

  • Question

  • User-2074302700 posted

    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

     

    Dannster

     

     


     

    Wednesday, February 27, 2008 1:29 PM

Answers

  • User660069657 posted

     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. 

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, February 28, 2008 6:46 AM

All replies

  • User660069657 posted

    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;
    else
      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# 

    Thursday, February 28, 2008 3:28 AM
  • User-2074302700 posted

    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.

     
    Cheers

    Mart
     

     


     


     

    Thursday, February 28, 2008 6:38 AM
  • User660069657 posted

     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. 

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, February 28, 2008 6:46 AM
  • User-2074302700 posted

    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!

     Cheers

     

    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 

     

    Martin
     

    Thursday, February 28, 2008 7:32 AM
  • User-1206211797 posted

    May be you need to twik the page like this http://www.dotnetfunda.com/articles/article97.aspx (Pass value from user control to page)

    Tuesday, July 15, 2008 4:46 PM
  • User522004677 posted

    This is what I was lookin for:

    http://aspalliance.com/67_Accessing_Page_Properties


    Thanks , 

    Monday, January 25, 2010 10:49 PM