Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3872082
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T21:53:12+00:00 2026-05-19T21:53:12+00:00

I have a user control I’m building (ASP.NET 3.5, using C#). This control is

  • 0

I have a user control I’m building (ASP.NET 3.5, using C#).

This control is largely based on another, similar control (cut & paste inheritance, unfortunately, but there’s no easy way to abstract this). The lines that are giving me trouble are directly copied from the other control, as is the majority of the display.

The relevant lines of code are as follows:

Panel pnlForm = (Panel)e.Item.FindControl("pnlForm");
Literal ltrAvailableCount = (Literal)e.Item.FindControl("ltrAvailableCount");

DropDownList drpLanguage = (DropDownList)pnlForm.FindControl("drpLanguage");
DropDownList drpShipTo = (DropDownList)pnlForm.FindControl("drpShipTo");
HiddenField hdnAvailableProductId = (HiddenField)pnlForm.FindControl("hdnAvailableProductId");

DropDownList drpQuantity = (DropDownList)pnlForm.FindControl("drpQuantity");
HiddenField hdnSelectedStyle = (HiddenField)e.Item.FindControl("hdnSelectedStyle");
Label lblStyleName = (Label)e.Item.FindControl("lblSelectedStyle");

pnlForm in the above is found correctly, as is ltrAvailableCount.

If I skip down to drpQuantity, it and the following lines work fine. However, when I run the lines for drpLanguage, drpShipTo, or hdnAvailableProductId FindControl throws a NullReferenceException.

I do not mean it returns ‘null’ and I try to access a property of the returned object, the method FindControl throws the exception. According to the MSDN library, this isn’t possible – FindControl doesn’t list anything as a potential thrown error, it just says that if it can’t find the Control, it returns null.

What can cause FindControl to NullReferenceException?

—————–Edit—————

I should also mention that if I run any of the three problematic lines in the immediate window, I get the correct results. I can also see the controls in the ControlCollection of pnlForm when I inspect it while debugging.

—————–2nd Edit——————-

Just to confirm, I added another line:
DropDownList notThere = (DropDownList)pnlForm.FindControl(“notHere”);
The control notHere is not anywhere on the page. The above line compiles (of course) and when I run the debugger, it runs fine. The cast goes fine. The variable notThere simply is null.

The underlying cause of this issue is NOT FindControl failing to find the controls and trying to cast null to a DropDownList or other Control

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-19T21:53:13+00:00Added an answer on May 19, 2026 at 9:53 pm

    It is NOT FindControl that’s causing the problem.

    The way you’ve got it coded it’s performing two operations on one line – FindControl() and then the cast to the control datatype. It’s the cast that’s bombing, because the results of the FindControl call is a null value, and you’re attempting to cast null to a control.

    This one bit me many times, so I learned to code it as

    object oDropDown1 = pnlForm.FindControl("DropDown1");
    
    // then check if oDrowpDown 1 is null and cast if it's safe.
    
    if(oDropDown1 != null)
    {
       // here it's safe to cast.
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have user control on a ASP.NET web page, which contains a GridView and
Say I have a User Control in ASP.NET that contains a button: public class
I have user control named DateTimeUC which has two textboxes on its markup: <asp:TextBox
I have a user control that I'm building. It's purpose is to display the
I have user control in which there are text boxes no i am using
I have user control that i wrote. this user control is appear on some
This seemed easier in Web Forms; I'd have a user control, with the logic
I have a user control in a repeater that I need to pass data
I have a user control that is pretty basic. It contains several TextBox controls,
I have a user control ( gallery.ascx ) and I want to use the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.