null references in my user control!!

G

Guest

I've got a really annoying problem, and I feel like i've tried everything ....

I've got a really simple usercontrol, containing a single dropdown.

In the Page_Load of the usercontrol, i want to set the items of the drop
down, but the control reference in the code behind file is always undefined.

the id is correct and I've created lots of user control before and never had
this problem. what am i missing??

ascx
<%@ Control Language="c#" AutoEventWireup="false"
Codebehind="SelectorControl.ascx.cs" Inherits="MyNameSpace.SelectorControl"
TargetSchema="http://schemas.microsoft.com/intellisense/ie5"%>
<asp:DropDownList id="DropDownList1" runat="server"
Width="256px"></asp:DropDownList>


namespace MyNameSpace
{
using System;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

/// <summary>
/// Summary description for CurrencySelectorControl.
/// </summary>
public class CurrencySelectorControl : System.Web.UI.UserControl
{
protected System.Web.UI.WebControls.DropDownList DropDownList1;

private void Page_Load(object sender, System.EventArgs e)
{
// DropDownList1 == null
}

#region Web Form Designer generated code
}
}


please help?
 
O

OJ

Hi Sam,
does this work?

protected System.Web.UI.WebControls.DropDownList DropDownList1;

private void Page_Load(object sender, System.EventArgs e)
{
if(!Page.IsPostBack) DropDownList1 = this.yourdropdownlistname;
}


A better idea might be to derive your class from
System.Web.UI.WebControls.DropDownList

Hth,
O
 
K

Karl Seguin [MVP]

I coudln't help but notice that your user control ascx file inherits from
MyNameSpace.SelectorControl

but your user control class is actually

MyNameSpace.CurrencySelectorControl

my guess is that this inconsistency is the root of your problem..

Karl
 
G

Guest

yeah, sorry. changed for post purposes.

it is correct.

with regards to OJ's suggestion, "this.yourdropdownlistname" should be
this.DropDownList1, do you mean do FindControl()?

The problem is that the controls collection for the user control (this) is
empty. Calling EnsureChildControls doesn't help either.

In the final control there will be more controls than a single drop down, so
don't want to inherit from DropDownList

any other ideas ?


--
TIA
Sam Martin


Karl Seguin said:
I coudln't help but notice that your user control ascx file inherits from
MyNameSpace.SelectorControl

but your user control class is actually

MyNameSpace.CurrencySelectorControl

my guess is that this inconsistency is the root of your problem..

Karl
 
G

Guest

ok, sussed it.

thanks for that though. was a typo as it turned out. your suggestion made
me look twice at it.

cheers.



--
TIA
Sam Martin


Karl Seguin said:
I coudln't help but notice that your user control ascx file inherits from
MyNameSpace.SelectorControl

but your user control class is actually

MyNameSpace.CurrencySelectorControl

my guess is that this inconsistency is the root of your problem..

Karl
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,819
Latest member
masterdaster

Latest Threads

Top