event handler with dynamically created control?

Author Topic
danny parks
Member

Joined: 26 Sep 2006
Country:
Posts: 1

Posted: 26 Sep 2006 01:46:42

When I use the Toolbox to create an <aspropDownList> on a .aspx page I can easily get the control to be associated with an event handler:

<aspropDownList ID="DropDownList1" runat="server" AutoPostBack="true"
OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged">

When I try to create a similar control dynamically on an .aspx.cs page, I can get the control created and visible, bind it to a data source, etc., but when I try to associate it with an event handler the build fails with an error of:

'System.Web.UI.WebControls.ListControl.OnSelectedIndexChanged(System.EventArgs)'
is inaccessible due to its protection level

Part of the code to create the control dynamically:

DropDownList ddlDropDown2 = new DropDownList();
ddlDropDown2.AutoPostBack = true;
ddlDropDown2.OnSelectedIndexChanged = "ddlDropDown2_SelectedIndexChanged";

I also tried using an OnSelectedIndexChanged of:

ddlDropDown2.OnSelectedIndexChanged += new EventHandler(this.ddlDropDown2_SelectedIndexChanged);

but it doesn't matter. Is it something with my use of 'new' that's creating a control that is protected rather than public? If so, how do I change this?

When I use the Toolbox to create an <aspropDownList> on a .aspx page I can easily get the control to be associated with an event handler:

<aspropDownList ID="DropDownList1" runat="server" AutoPostBack="true"
OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged">

When I try to create a similar control dynamically on an .aspx.cs page, I can get the control created and visible, bind it to a data source, etc., but when I try to associate it with an event handler the build fails with an error of:

'System.Web.UI.WebControls.ListControl.OnSelectedIndexChanged(System.EventArgs)'
is inaccessible due to its protection level

Part of the code to create the control dynamically:

DropDownList ddlDropDown2 = new DropDownList();
ddlDropDown2.AutoPostBack = true;
ddlDropDown2.OnSelectedIndexChanged = "ddlDropDown2_SelectedIndexChanged";

I also tried using an OnSelectedIndexChanged of:

ddlDropDown2.OnSelectedIndexChanged += new EventHandler(this.ddlDropDown2_SelectedIndexChanged);

but it doesn't matter. Is it something with my use of 'new' that's creating a control that is protected rather than public? If so, how do I change this?
Jeevan Seba
Member

Joined: 08 Dec 2009
Country: IN
Posts: 1

Posted: 08 Dec 2009 15:55:26

Acessing Dynamically added HTML Element on server side.



htmlerror-info.blogspot.com/2009/12/accessing-controls-created-dynamically.html

Acessing Dynamically added HTML Element on server side.



htmlerror-info.blogspot.com/2009/12/accessing-controls-created-dynamically.html

Follow us on twitter Subscribe to our RSS feed
Activate your free membership today | Login | Currency