1

I create a number of CheckboxList Controls in my code-behind (in Page_Load), which are put into an UpdatePanel. The UpdatePanel is set up for UpdateMode="always", so if I understood correctly, every Postback from within this UpdatePanel should trigger a Postback on the whole Updatepanel (which is what I want).

Simplified aspx-markup:

<asp:UpdatePanel ID="updatepanel_profiles_manualdata" runat="server">
<ContentTemplate>
<div id="cbl_container" runat="server">
</div>
</ContentTemplate>
</asp:UpdatePanel>    

Simplified code-behind:

protected void create_cbl(){
// this is called in Page_Load
CheckBoxList mycbl = new CheckBoxList();
mycbl.SelectedIndexChanged += new EventHandler(cbl_manual_clickEvent);

DataTable dt3 = get_cbl_data(someparameter);
mycbl.DataSource = dt3;
mycbl.DataTextField = "Title";
mycbl.DataValueField = "ID";
mycbl.AutoPostBack = true;
mycbl.DataBind();

//add the checkboxlist to the container-div in the aspx-markup
cbl_container.Controls.Add(mycbl);
}

protected void cbl_manual_clickEvent(object sender, EventArgs e){
// do something with this click...
}

Struggles:

  1. I tried setting a breakpoint into my cbl_manual_clickEvent - it is never reached, but the page compiles without an error.
  2. I have another CheckBoxList in the same UpdatePanel, initialized in the markup and therefore the OnSelectedIndex event is bound within the markup. Postback works just fine here. However I need to create the second CheckBoxList from code-behind, as the amount depends on some data within a database.
  3. I tried setting ChildrenAsTriggers="true" in the UpdatePanel, as well as changing the UpdateMode and defining triggers, without any luck.

What am I missing? Thanks in advance for all hints or suggestions...!

konrad_pe
  • 1,189
  • 11
  • 26
  • post how did you call this function in Page_Load – Feras Salim Jun 30 '15 at 16:23
  • try to call this function under the condition !IsPostBack – Feras Salim Jun 30 '15 at 16:25
  • Yeah I call this function the first time in !Page.IsPostBack besides some other controls which are created there. – konrad_pe Jun 30 '15 at 17:49
  • try to assign the event handler out side the condition !Page.IsPostBack – Feras Salim Jun 30 '15 at 18:19
  • My controls are only created within a function which is called upon the first execution of the page (therefore in the !Page.IsPostBack). You propose that AFTER that function I try to find those created controls and assign them the OnSelectedIndexChanged event? (Just asking to get you right!) – konrad_pe Jun 30 '15 at 18:31

2 Answers2

1

try this aspx file:

<asp:UpdatePanel ID="updatepanel_profiles_manualdata" runat="server">
<ContentTemplate>
<div id="cbl_container" runat="server">
<asp:CheckBoxList ID="CheckBoxList1" AutoPostBack="true" runat="server"></asp:CheckBoxList>
</div>
</ContentTemplate>
</asp:UpdatePanel>   

code behind

protected void Page_Load(object sender, EventArgs e)
{
CheckBoxList1.SelectedIndexChanged += new EventHandler(cbl_manual_clickEvent);
DataTable dt3 = get_cbl_data(someparameter);
CheckBoxList1.DataSource = dt3;
CheckBoxList1.DataTextField = "Title";
CheckBoxList1.DataValueField = "ID";
CheckBoxList1.AutoPostBack = true;
CheckBoxList1.DataBind();

}

#update1 see this answer

Community
  • 1
  • 1
Feras Salim
  • 438
  • 7
  • 33
  • (I already commented on this, was this deleted? However..) this won't work for me. There is no way around for me creating the CheckBoxList controls in code-behind. Building one CheckBoxList in the markup and hooking it up is not the problem. Creating them dynamically for an UpdatePanel and then hooking them up with their events is the catch. – konrad_pe Jun 30 '15 at 19:08
  • That does not really help me. Did a workaround, but thanks for your effort @Feras. – konrad_pe Jul 02 '15 at 07:13
-1

Do you tried doing AutoPostBack=true of CheckBoxList?

Jamil
  • 330
  • 1
  • 6
  • 25