动态添加的复选框的CheckedChanged EventHandler不在Repeater的UpdatePanel内部触发 [英] CheckedChanged EventHandler of dynamically added Checkboxes not firing inside UpdatePanel of a Repeater

查看:95
本文介绍了动态添加的复选框的CheckedChanged EventHandler不在Repeater的UpdatePanel内部触发的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在这里阅读了大多数文章,但是我无法弄清楚为什么 CheckedChanged事件没有触发。这是我的情况。

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

我正在使用中继器从数据库中生成项目。每个ReapeaterItem应该包含一个UpdatePanel,因为我必须在UpdatePanel中更新控件,并且不想重新加载整个页面。在这些动态生成的UpdatePanels(每个RepeaterItem具有一个)中,我动态地添加了三个复选框(基于数据库)。这些复选框需要触发 CheckedChanged事件,因为在某些情况下,我想根据业务逻辑启用/禁用/选中/取消选中Checkbox1、2或3。希望您到目前为止。我正在添加所有控件,并添加了EventHandler。但是生成的代码不能反映事件处理程序。我也尝试了OnItemDataBound,OnItemCreated,PreRender,...事件来添加Eventhandler,但是我找不到ID为CheckBox的控件。

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´我完全迷失了这一点,并在使用按钮而不是复选框的途中迷失了方向。到目前为止,我了解到的是,通过Button,我可以使用Button中的CommandName和Repeater中的ItemCommand-Event来解决,但随后我需要以某种方式在页面上反映检查。

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,每个中继器(8)都位于ajaxtoolkit手风琴控件中。

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

在这里,我给您一些代码:

Here i give you some Code:

aspx页

<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>

这里是Page_Load部分

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++;
                        }
                    }
                }
            }

此永远不会到达代码:

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

这是生成的CheckBox代码:

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)" />

事件已生成,但是当我单击Checkbox时,UpdatePanel中的所有内容都消失了,而CheckedChanged -EventHandler未解雇。

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

我在做什么错了?

感谢所有建议,我

mk

推荐答案

页面第一次加载您将所有复选框添加到Controls集合中,它们就会被渲染。当您执行回发时(通过CheckBox的AutoPostBack)时,您会得到一个 if(!IsPostBack)支票,该支票不允许将复选框添加到回发控件的集合中。因此,您将看不到控件和页面,并且当页面生命周期尝试调用事件时(发生在Page_Load之后),创建事件的控件将不再存在。

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.

您将需要重构Page_Load方法,以便它做两件事-1,而不管 IsPostBack 的值如何绑定转发器并创建动态控件。如图2所示,如果 IsPostBack == false (即初始加载),则设置动态控件的值。您不想在 IsPostBack == true 时设置动态控件的值,因为那样您会丢失用户输入的值。

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.

也请注意:

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

可以重写为:

if (crInRepeater is RepeaterItem)

这篇关于动态添加的复选框的CheckedChanged EventHandler不在Repeater的UpdatePanel内部触发的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆