views:

34

answers:

1

Hello!

I´ve read most posts here but i can´t figure out why the "CheckedChanged" Event is not firing. Here is my situation.

I´m using a Repeater to generate Items out of a Database. Each ReapeaterItem should include an UpdatePanel, because i have to Update the Controls inside the UpdatePanel and do not want to reload the complete page. Inside these dynamically generated UpdatePanels (each RepeaterItem has one) i´m adding up to three Checkboxes dynamically (based on the Database). These Checkboxes need to fire the "CheckedChanged" event, because on some conditions i want to enable/disable/check/uncheck Checkbox1, 2 or 3 based on business logic. ... Hope you got this so far. I´m adding all Controls and have the EventHandler Added. But the generated Code does not reflect the Event Handler. I tried OnItemDataBound, OnItemCreated, PreRender, ... Events to add the Eventhandler too, but i was not able to find the CheckBox-Control with the ID.

I´m totally lost with this and on the way to use Buttons instead of Checkboxes. From what i read so far is that with Buttons i can use the CommandName from the Button and the ItemCommand-Event from the Repeater to get a workaround, but then i need to reflect the "Check" on the Page in some way.

btw, every Repeater (8) sits inside an ajaxtoolkit-accordion control.

Here i give you some Code:

aspx-Page

<asp:Repeater ID="RepeaterAccordionPane2" runat="server">
     <ItemTemplate>
          HTML Stuff<%# DataBinder.Eval(Container.DataItem, "Header")%>HTML Stuff<%# DataBinder.Eval(Container.DataItem, "Beschreibung")%></td>
                <asp:UpdatePanel ID="UpdatePanel2" runat="server" UpdateMode=Conditional>
                       <ContentTemplate>
                       </ContentTemplate>
                 </asp:UpdatePanel>
                 HTML Stuff
     </ItemTemplate>
</asp:Repeater>

Here is the Page_Load Part

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            dvAlleArtikel = new System.Data.DataView(...Database...);

            [... some other code here ...]
            RepeaterAccordionPane2.DataSource = dvAlleArtikel;
            //RepeaterAccordionPane2.ItemCreated +=new RepeaterItemEventHandler(RepeaterAccordionPane2_ItemCreated);
            //RepeaterAccordionPane2.PreRender +=new EventHandler(RepeaterAccordionPane2_PreRender);
            RepeaterAccordionPane2.DataBind();

            int nUpdatePanelIndex = 0;
            foreach (Control crInRepeater in RepeaterAccordionPane2.Controls)
            {
                if (crInRepeater.GetType() == typeof(RepeaterItem))
                {
                    foreach (Control crInRepeaterItem in crInRepeater.Controls)
                    {
                        if (crInRepeaterItem.GetType() == typeof(UpdatePanel))
                        {
                            LiteralControl litTabelleBeginn = new LiteralControl("<table width=\"100%\"  border=\"0\" cellspacing=\"0\" cellpadding=\"2\">");
                            ((UpdatePanel)crInRepeaterItem).ContentTemplateContainer.Controls.Add(litTabelleBeginn);

                            if (dvAlleArtikel[nUpdatePanelIndex]["ArtNr1"].ToString() != "0")
                            {
                                CheckBox CheckBox1 = new CheckBox();
                                CheckBox1.ID = dvAlleArtikel[nUpdatePanelIndex]["ArtNr1"].ToString();
                                CheckBox1.Text = (dvAlleArtikel[nUpdatePanelIndex]["CheckBoxLbl1"].ToString() == "" ? "leer" : dvAlleArtikel[nUpdatePanelIndex]["CheckBoxLbl1"].ToString());
                                CheckBox1.AutoPostBack = true;
                                CheckBox1.CheckedChanged +=new EventHandler(CheckBox1_CheckedChanged);

                                LiteralControl litNeueTabellenZeileBeginn = new LiteralControl("<tr><td width=10><img src=\"images/helper/spacer.gif\" width=\"10\"></td><td height=\"20\">");
                                LiteralControl litNeueTabellenZeileEnde = new LiteralControl("</td><td width=\"100\" height=\"20\">" + dvAlleArtikel[nUpdatePanelIndex]["ArtPrice1"].ToString() + " &euro; </td></tr>");

                                ((UpdatePanel)crInRepeaterItem).ContentTemplateContainer.Controls.Add(litNeueTabellenZeileBeginn);
                                ((UpdatePanel)crInRepeaterItem).ContentTemplateContainer.Controls.Add(CheckBox1);
                                ((UpdatePanel)crInRepeaterItem).ContentTemplateContainer.Controls.Add(litNeueTabellenZeileEnde);
                            }

                            [... some other code here...]

                            LiteralControl litTabelleEnde = new LiteralControl("</table>");
                            ((UpdatePanel)crInRepeaterItem).ContentTemplateContainer.Controls.Add(litTabelleEnde);
                            nUpdatePanelIndex++;
                        }
                    }
                }
            }

This code is never reached:

    protected void CheckBox1_CheckedChanged(object sender, EventArgs e)
    {
        int foo = 0;
    }

This is the CheckBox-Code generated:

<input id="AccordionPane2_content_RepeaterAccordionPane2_ctl00_6200" type="checkbox" name="AccordionPane2_content$RepeaterAccordionPane2$ctl00$6200" onclick="javascript:setTimeout('__doPostBack(\'AccordionPane2_content$RepeaterAccordionPane2$ctl00$6200\',\'\')', 0)" />

The Event is generated, but when i click the Checkbox all Content in the UpdatePanel is gone and the CheckedChanged-EventHandler is not fired.

What am i doing wrong?

Thanks to all advice, i´m really stuck.

mk

+1  A: 

The first time the page loads you are adding all the checkboxes to the Controls collection, and they get rendered. When you do a postback (through the CheckBox's AutoPostBack) you have a check if(!IsPostBack) that doesn't allow the checkboxes to be added to the Controls collection on the postback. Because of that, you won't see the controls and the page, and when the page lifecycle tries to call the events (which occurs AFTER Page_Load), the controls that created the events are no longer there.

You will need to refactor your Page_Load method so it does two things - 1, regardless of the value of IsPostBack bind the repeaters and create the dynamic controls. 2, if IsPostBack==false, i.e., an initial load, then set the values of the dynamic controls. you don't want to set the values of the dynamic controls when IsPostBack==true because then you will lose the values the user entered.

also, just a note:

if (crInRepeater.GetType() == typeof(RepeaterItem))

can be rewritten as:

if (crInRepeater is RepeaterItem)
dave thieben
...and when the page lifecycle tries to call the events (which occurs AFTER Page_Load)... was the thing to do the job. I´m fairly new to more complex asp.net and ajax programming and didn´t knew that part of page-life cycle and how event-handling occurs with dynamic controls. Thanks for your help on this!
Markus Kitzig
please mark the answer as accepted if it helped.
dave thieben
Marked as you wished. :)
Markus Kitzig