There are a few questions on here that are similar, but they didn't work for me.
I have a custom drop down list on a page, the page name is WebForm1
. On WebForm1
I have an assembly reference to it: <%@ Register Assembly="GroupDropDownList" Namespace="GroupDropDownList" TagPrefix="cc1" %>
I then have a static method like this:
public static void Populate_Country_DDL(System.Web.UI.Page page)
{
GroupDropDownList
.GroupDropDownList ddlCountry =
((GroupDropDownList.GroupDropDownList)page.FindControl("ddlCountry"));
using (DataContext db = new DataContext())
{
var country = db.Countries.OrderBy(x => x.Text);
ddlCountry.DataSource = country; //Error here
On WebForm1
i'm trying to access the static method like this:
Utility.Populate_Country_DDL(this.Page);
The problem is that I'm getting an "Object reference not set to an instance of an object" error. I put a using statement in my utility class for GroupDropDownList
GroupDropDownList
simply allows me to put option groups inside a drop down list.
Update: To fix the problem I modified my utility method to take a ContentPlaceHolder
instead of Page
class, and then in my calling method I found the ContentPlaceHolder within the current pages MasterPage like this:
Utility.Populate_Country_DDL(
(ContentPlaceHolder)this.Master
.FindControl("ContentPlaceHolder1"));