取消选中复选框时,asp:checkbox 的 OnCheckedChanged 事件处理程序不会触发

发布于 2024-08-03 22:20:44 字数 2086 浏览 3 评论 0原文

我有一个中继器,在中继器的每个 ItemTemplate 中都是一个带有 OnCheckedChanged 事件处理程序集的 asp:checkbox。这些复选框的 AutoPostBack 属性设置为 true。当选中任何复选框时,将触发事件处理程序。当取消选中任何一个时,事件处理程序不会触发。

知道为什么该事件没有触发,以及我如何让它触发吗?谢谢。

简化的转发器代码:

<asp:Repeater ID="rptLinkedItems" runat="server">            
    <ItemTemplate>      
    <asp:CheckBox ID="chkLinked" runat="server" 
     Checked="false" OnCheckedChanged="chkLinked_CheckedChanged" />
    </ItemTemplate>    
</asp:Repeater>

集合绑定到转发器,如下所示:

protected override void OnPreRenderComplete(EventArgs e)
{
    if (!Page.IsPostBack)
    {
        m_linkedItems = GetLinkedItems();
        rptLinkedItems.DataSource = GetLinkableItems();
        rptLinkedItems.ItemDataBound += new RepeaterItemEventHandler
               (rptLinkedItems_ItemDataBound);
        rptLinkedItems.DataBind();
    }

    base.OnPreRenderComplete(e);
}

OnItemDataBound 事件处理程序如下:

private void rptLinkedItems_ItemDataBound(Object sender, RepeaterItemEventArgs args)
{
    if (args.Item.ItemType == ListItemType.Item || args.Item.ItemType == ListItemType.AlternatingItem)
    {
        CategoryItem item = args.Item.DataItem as CategoryItem;

        Literal litItemName = args.Item.FindControl("litItemName") as Literal;
        CheckBox chkLinked = args.Item.FindControl("chkLinked") as CheckBox;

        litItemName.Text = item.Text;

        chkLinked.Checked = IsItemLinked(item);
        chkLinked.AutoPostBack = true;
        chkLinked.InputAttributes.Add("Value", item.Id.ToString());
    }
}

OnCheckedChanged 事件处理程序如下:

protected void chkLinked_CheckedChanged(Object sender, EventArgs args)
{
    CheckBox linkedItem = sender as CheckBox;
    Boolean itemState = linkedItem.Checked;
    Int32 itemId = Int32.Parse(linkedItem.InputAttributes["Value"].ToString());
    DataAccessLayer.UpdateLinkedItem(m_linkingItem, Utilities.GetCategoryItemFromId(itemId), itemState);
}

PS 如果有人也可以告诉我为什么 markdown 对我来说不能正常工作......

I have a repeater, in each ItemTemplate of the repeater is an asp:checkbox with an OnCheckedChanged event handler set. The checkboxes have the AutoPostBack property set to true. When any of the checkboxes is checked, the event handler fires. When any is unchecked, the event handler does not fire.

Any idea why the event does not fire, and how I mgiht make it fire? Thanks.

Simplified repeater code:

<asp:Repeater ID="rptLinkedItems" runat="server">            
    <ItemTemplate>      
    <asp:CheckBox ID="chkLinked" runat="server" 
     Checked="false" OnCheckedChanged="chkLinked_CheckedChanged" />
    </ItemTemplate>    
</asp:Repeater>

The collection is bound to the repeater as follows:

protected override void OnPreRenderComplete(EventArgs e)
{
    if (!Page.IsPostBack)
    {
        m_linkedItems = GetLinkedItems();
        rptLinkedItems.DataSource = GetLinkableItems();
        rptLinkedItems.ItemDataBound += new RepeaterItemEventHandler
               (rptLinkedItems_ItemDataBound);
        rptLinkedItems.DataBind();
    }

    base.OnPreRenderComplete(e);
}

The OnItemDataBound event handler is as follows:

private void rptLinkedItems_ItemDataBound(Object sender, RepeaterItemEventArgs args)
{
    if (args.Item.ItemType == ListItemType.Item || args.Item.ItemType == ListItemType.AlternatingItem)
    {
        CategoryItem item = args.Item.DataItem as CategoryItem;

        Literal litItemName = args.Item.FindControl("litItemName") as Literal;
        CheckBox chkLinked = args.Item.FindControl("chkLinked") as CheckBox;

        litItemName.Text = item.Text;

        chkLinked.Checked = IsItemLinked(item);
        chkLinked.AutoPostBack = true;
        chkLinked.InputAttributes.Add("Value", item.Id.ToString());
    }
}

The OnCheckedChanged event handler is as follows:

protected void chkLinked_CheckedChanged(Object sender, EventArgs args)
{
    CheckBox linkedItem = sender as CheckBox;
    Boolean itemState = linkedItem.Checked;
    Int32 itemId = Int32.Parse(linkedItem.InputAttributes["Value"].ToString());
    DataAccessLayer.UpdateLinkedItem(m_linkingItem, Utilities.GetCategoryItemFromId(itemId), itemState);
}

P.S. If someone can also tell me why markdown doesn't work correctly for me...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

死开点丶别碍眼 2024-08-10 22:20:44

尝试使用AutoPostBack="true",如下所示:

<asp:CheckBox ID="chkLinked" runat="server" Checked="false"
    OnCheckedChanged="chkLinked_CheckedChanged" AutoPostBack="true"/>

Try usingAutoPostBack="true" like this:

<asp:CheckBox ID="chkLinked" runat="server" Checked="false"
    OnCheckedChanged="chkLinked_CheckedChanged" AutoPostBack="true"/>
昔日梦未散 2024-08-10 22:20:44

这是因为当 ASP.NET 执行 ASP.NET 页面生命周期的 Control events 部分时,控件层次结构(特别是复选框)不存在,就像您在稍后的 PreRender 阶段。请参阅 ASP.NET 页面生命周期概述,了解更详细的概述事件顺序。

对于 @bleeeah 的建议,我会谨慎行事,因为您正在为 rptLinkedItems_ItemDataBound 内的 CheckBox.Checked 分配一个值,这也会导致该事件要执行的处理程序:


chkLinked.Checked = IsItemLinked(item);

相反,将:


if (!Page.IsPostBack)
   {
      m_linkedItems = GetLinkedItems();
      rptLinkedItems.DataSource = GetLinkableItems();
      rptLinkedItems.ItemDataBound += new RepeaterItemEventHandler
          (rptLinkedItems_ItemDataBound);
      rptLinkedItems.DataBind();
   }

移至 Page.Load 事件处理程序中。

This is because the control hierarchy (and the check boxes in particular) don't exist when ASP.NET executes the Control events portion of the ASP.NET page life cycle, as you had created them in the later PreRender stages. Please see ASP.NET Page Life Cycle Overview for more detailed overview of the event sequence.

I would err on the side of caution for @bleeeah's advice, for you're assigning a value to CheckBox.Checked inside rptLinkedItems_ItemDataBound, which would also cause the event handler to execute:


chkLinked.Checked = IsItemLinked(item);

Instead, move:


if (!Page.IsPostBack)
   {
      m_linkedItems = GetLinkedItems();
      rptLinkedItems.DataSource = GetLinkableItems();
      rptLinkedItems.ItemDataBound += new RepeaterItemEventHandler
          (rptLinkedItems_ItemDataBound);
      rptLinkedItems.DataBind();
   }

Into the Page.Load event handler.

來不及說愛妳 2024-08-10 22:20:44

尝试重新订阅 OnItemDataBound 事件中的 CheckChanged 事件,

chkLinked.CheckedChanged += new EventHandler(chkLinked_CheckedChanged);

Try re-subscribing to the CheckChanged event in your OnItemDataBound event ,

chkLinked.CheckedChanged += new EventHandler(chkLinked_CheckedChanged);
转角预定愛 2024-08-10 22:20:44

像这样使用 AutoPostBack="true"

<asp:CheckBox ID="chkLinked" runat="server" AutoPostBack="true"
    Checked="false" OnCheckedChanged="chkLinked_CheckedChanged" />

Use AutoPostBack="true" like this:

<asp:CheckBox ID="chkLinked" runat="server" AutoPostBack="true"
    Checked="false" OnCheckedChanged="chkLinked_CheckedChanged" />
み零 2024-08-10 22:20:44

订阅 Page_Init 中的 CheckChanged 事件。

Subscribe to the CheckChanged event in your Page_Init.

仅冇旳回忆 2024-08-10 22:20:44

您必须在中继器项目命令之外为清单定义事件处理程序,然后在中继器项目命令内,遍历清单项目并获取已检查的项目。

在 .aspx 页面中,您可以使用 Ajax 和 updatepanel 来触发事件处理程序,但请记住,您必须在中继器之外定义 scriptmanage。

// checklisk checkchange 事件处理程序

protected void chkLinked_CheckedChanged(Object sender, EventArgs args)
        {
        }

和项目重复器命令 item:
// 迭代清单项并检测已检查

    protected void Repeater1_ItemCommand(object sender, RepeaterCommandEventArgs e)
    {
        CheckBoxList cbl = (CheckBoxList)e.Item.FindControl("CheckBoxList1");
        cbl.SelectedIndexChanged += new EventHandler(chkLinked_CheckedChanged);

        string name = "";
        for (int i = 0; i < cbl.Items.Count; i++)
        {
            if (cbl.Items[i].Selected)
            {
                name += cbl.Items[i].Text.Split(',')[0] + ",";
            }
        }
    }

You have to define eventhandler for checklist out of repeater item command, then inside the repeater item command, go through checklist items and get checked items.

In the .aspx page you can use Ajax and updatepanel to fire eventhandler, but keep in mind you have to define scriptmanage outside of repeater.

// checklisk checkedchange eventhandler

protected void chkLinked_CheckedChanged(Object sender, EventArgs args)
        {
        }

and item repeater command item:
// iterate checklist items and detect checked

    protected void Repeater1_ItemCommand(object sender, RepeaterCommandEventArgs e)
    {
        CheckBoxList cbl = (CheckBoxList)e.Item.FindControl("CheckBoxList1");
        cbl.SelectedIndexChanged += new EventHandler(chkLinked_CheckedChanged);

        string name = "";
        for (int i = 0; i < cbl.Items.Count; i++)
        {
            if (cbl.Items[i].Selected)
            {
                name += cbl.Items[i].Text.Split(',')[0] + ",";
            }
        }
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文