Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
780 views
in Technique[技术] by (71.8m points)

c# - OnCheckedChanged event handler of asp:checkbox does not fire when checkbox is unchecked

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Try usingAutoPostBack="true" like this:

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...