0

I am having a heck of time here. I have a class file and I need to reference a control from a web page in it. I've found some examples around the web, but something is not working correctly. Here's the relevant C#:

Page page = (Page)HttpContext.Current.Handler;
DropDownList ddl = (DropDownList)page.FindControl("ddlCrimeType");
ddl.DataSource = read; // read is a SqlDataReader.
ddl.DataBind();

What I'm trying to do is bind some data to this DropDownList in my class file. The problem is that at the third line there, I get the old:

Object reference not set to an instance of an object.

If I return the FieldCount of the SqlDataReader, I get the right number of fields, so I think it's got data; I'm just not finding the control, I guess. How can I find the control ddlCrimeType in my class file?

Let me know if there's anything else you need to know.

Update:

The control is in a content page. When I wrote similar code in my page's codebehind to do the same thing, I had to do this:

DropDownList ddl = (DropDownList)Master.FindControl("ContentPlaceHolder1").FindControl("ddlCrimeType");

I just need to figure out how to do that in my class file. Right now, I'm told:

The name 'Master' does not exist in the current context.
mrcoulson
  • 1,331
  • 6
  • 20
  • 35
  • 4
    It would seem to me that `ddl` is `null`. Have you stepped through your code with your debugger? – Brandon May 28 '14 at 23:04
  • 1
    Where is that code running, when is it running, and why are you doing it like that? – Andrew Barber May 28 '14 at 23:05
  • That wouldn't cause that error on that line, @Brandon I don't think. – Andrew Barber May 28 '14 at 23:06
  • Why, @SyedFarjadZiaZaidi? It's perfectly valid to set that property to null. – Andrew Barber May 28 '14 at 23:07
  • 1
    @Brandon that would mean ddl is null, wouldn't it, though? – Andrew Barber May 28 '14 at 23:08
  • did you set the id of your control correctly ? – Selman Genç May 28 '14 at 23:08
  • 2
    If you put a breakpoint on that line, is ddl null? – Mark Hall May 28 '14 at 23:09
  • @AndrewBarber - Actually, you are right. I had initially meant to type `ddl`, not `read`. Thanks for catching that. Removing the unneeded comments of mine. – Brandon May 28 '14 at 23:09
  • http://stackoverflow.com/a/15433600/2109960 – JustAndrei May 28 '14 at 23:13
  • Wow. Mega response. Let me step through with debugger and come back here. Some problem with Visual Studio was preventing debug from working earlier when I was posting this. – mrcoulson May 28 '14 at 23:14
  • Indeed, ddl is null. Let me try what's at @JustAndrei's link. – mrcoulson May 28 '14 at 23:17
  • @AndrewBarber In a class called Bind.cs, it runs on page load, and I'm doing it like that because it seemed like a good idea. I need to bind lots of dropdownlists, so I thought I'd like to write a class that binds any dropdownlist I specify to the result of any query I specify instead of writing a new method to bind each individual list. – mrcoulson May 28 '14 at 23:25
  • Got it! I had to handle the fact that I was using a master page in this project. Whew. Should I add the answer in here or answer my own question? – mrcoulson May 28 '14 at 23:56
  • 1
    Since the class is going to be dependent on the page and the controls anyway, you might as well pass the control references in to your Bind class directly via the page load method then. You can do pretty much what you want here, but with compile time checking for your control references. – Andrew Barber May 28 '14 at 23:57

1 Answers1

0

Okay, as it turns out, I needed to reference the control appropriately for a master page / content page relationship. ddl was remaining null in my original code because I wasn't finding a control from the base of the master page. This is the actual successful code:

var pageHandler = HttpContext.Current.CurrentHandler;
Control ctrl = ((System.Web.UI.Page)pageHandler).Master.FindControl("ContentPlaceHolder1").FindControl(strControlIDToBind);
DropDownList ddl = (DropDownList)((System.Web.UI.Page)pageHandler).Master.FindControl("ContentPlaceHolder1").FindControl(strControlIDToBind);
ddl.DataSource = read;
ddl.DataBind();

Also, I'm passing the ID of the control to the method instead of specifying it like in my previous code.

Thanks to all who helped! Have a great day!

mrcoulson
  • 1,331
  • 6
  • 20
  • 35