0

i'm trying check if control(means user custom controls *ascx ) placed on correct page and i getting error - how to solve it

Control Page_Load:

    protected void Page_Load(object sender, EventArgs e)
    {
        ddl.enable = (Page as Online.MyPage);
    }

Error:

    Object reference not set to an instance of an object.

But if we look on page life cycle it's possible:

  Page: PreInit
  Control: Init
  Page: Init
  Page: InitComplete
  Page: PreLoad
  Page: Load
  Control: Load

ThanX

UDP:

I think it can be some like this but i think it's a wrong way.

var v = Page as Online.MyPage;
if (v != null)
   ddl.enable = v;

UPD:

ddl.enable - means dropdownlist

 DropDownList ddList = new DropDownList();
 ddList.Enabled = y;

I dont want to delegate parent page right to operate control by property, just want to solve this across control.

AleksP
  • 1,244
  • 2
  • 12
  • 18
  • 1
    There is not enough information. Based on your code, only reason you are seeing NullReferenceException is `Online is null`. – Win Oct 21 '14 at 15:30
  • Ok but how to check it null or not if it's child control - i have two different page with this control and i should show only on one of this page – AleksP Oct 21 '14 at 15:47
  • 2
    A control shouldn't care on what page it's placed; use properties to control its behavior. You need to show the implementation of `ddl` and `ddl.enable` in order to fix this. – CodeCaster Oct 22 '14 at 10:10

1 Answers1

0

Page as Online.MyPage will return null if Page cannot be casted to Online.MyPage. See here for an explanation on why.

What you want to be doing instead is to see if the type of the Page object inherits your custom Online.MyPage class. You can do this as such:

ddl.Enabled = Page.GetType().IsSubclassOf(typeof(Online.MyPage))

Update:

The given code example in the question is showing that ddl.enable is not a bool but some type of object instead. My assumption is that the intention is to enable or disable something based on the value of ddl.enable therefore I have assumed that ddl.enable is actually meant to be treated as a bool not an object. If ddl.enable is of type object, then the code will still compile since bool is also an object. Whether the code will execute without exception will entirely depend on what is done with ddl.enable.

Either case Page.GetType().IsSubclassOf(typeof(Online.MyPage)) is probably the best way to detect if the control is on a particular type of page.

Update 2:

Now that ddl has been clarified as a DropDownList control, I have updated my code snippet so that it enables or disables the DropDownList control based on the Page type.

Community
  • 1
  • 1
Adrian Sanguineti
  • 2,455
  • 1
  • 27
  • 29
  • 1
    If the result of `Page as Online.MyPage` is `null` and `ddl.enable = null` throws an NRE, then the setter of `ddl.enable` contains code that accesses the reference. It's not a `bool`. – CodeCaster Oct 22 '14 at 10:23
  • 1
    Yep, did see that but I have interpreted that question as the trying to enable/disable a control based on whether the `Page` inherits a particular class which doesn't necessarily make my answer wrong and is dependent on the @AleskP on showing the implementation of `ddl` and `ddl.enable` as you have asked. – Adrian Sanguineti Oct 22 '14 at 10:30