DropDownList event delegates

  • Thread starter studio60podcast
  • Start date
S

studio60podcast

I'm not sure I can do this, but here is what I'm trying to
accomplish...

I have a WebUserControl that has a single control - a DropDownList -
called BoundDL. In that control, I have methods that will bind that
control to a DataTable based on other properties that are set.

I have another page, test.aspx, that registers this UserControl and
sets it's properties. The trick is, I want to do AutoPostback on the
DDL in the user control and provide information from that event back
to the test.aspx page. I thought the best way to accomplish this
would be through a public delegate and event, but it's not working.
It says that the object reference was not set to an instance of an
object. Here is some code:

BoundDL.ascx:
<asp:DropDownList ID="BoundDL" runat="server" AutoPostBack="true"
OnSelectedIndexChanged="BoundDL_SelectedIndexChanged" />

BoundDL.ascx.cs:
public delegate void IndexChangedEventHandler(int value);
public event IndexChangedEventHandler IndexChanged;
protected void BoundDL_SelectedIndexChanged(object sender, EventArgs
e)
{
IndexChanged(Int32.Parse(BoundDL.SelectedValue));
}

test.aspx:
<%@ register tagprefix="admin" tagname="BoundDL" src="BoundDL.ascx" %>
<asp:Label ID="Label1" runat="server" Text="Event" />
<admin:BoundDL ID="dlEvent" runat="server" />

test.aspx.cs:
protected void Page_Load(object sender, EventArgs e)
{
dlEvent.IndexChanged += new
BoundDL.IndexChangedEventHandler(dlEvent_IndexChanged);
}

protected void dlEvent_IndexChanged(int value)
{
Label1.Text += value.ToString();
}

It's failing on
IndexChanged(Int32.Parse(BoundDL.SelectedValue));

Have I missed something? This is the first time I've used delegates
in ASP.NET. I just followed an example I had working in Windows
Forms.

Thanks for the help!
Jason
 
M

Masudur

Hi,

First check that selected value do exists...by
checking your ddl binding.... whither you actually assigned the
datavaluefield in the dropdown list....

Because every thing on code looks okay except you subscribed the event
in load instead on oninit method....

Thanks
Masudur
http://www.kaz.com.bd
http://munnacs.blogspot.com
 
S

studio60podcast

Masudur, thanks for the reply!

Simply moving
dlEvent.IndexChanged += new
BoundDL.IndexChangedEventHandler(dlEvent_IndexChanged);
to OnInit fixed the problem!

Thanks again.
 

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,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top