vendredi 18 septembre 2015

CheckChanged handler for CheckBox in ListView not reached

I have an .net 4.5 application with a ListView within an UpdatePanel. The ListView contains a bunch of checkboxes. Some of the checkboxes are assigned a ClickChanged even handler within the ListView's ItemDataBound handler.

The issue is that the checkboxes with an event handler assigned do cause an async postback when clicked (break point in Page_Load is triggered) but the ClickedChanged method is never called (break point in it is not triggered). I've create a minimalist sample that recreates the problem

page:

<%@ Page Title="" Language="C#" AutoEventWireup="true" CodeBehind="ListTest.aspx.cs" Inherits="Pgi.Hub.Testing.ListTest" %>

<!DOCTYPE html>

<html xmlns="http://ift.tt/lH0Osb">
<head id="Head1" runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>

        <asp:ScriptManager runat="server" ID="ScriptManager"></asp:ScriptManager>

        <asp:UpdatePanel runat="server" ID="upSample" ChildrenAsTriggers="true" UpdateMode="Always">
            <ContentTemplate>

                <asp:ListView ID="lvSample" runat="server" ItemPlaceholderID="itemPlaceHolder"
                        OnItemDataBound="lvSample_ItemDataBound" GroupPlaceholderID="groupPlaceHolder" 
                        GroupItemCount="1" ClientIDMode="AutoID" EnableModelValidation="True">
                    <LayoutTemplate>
                        <asp:PlaceHolder ID="groupPlaceHolder" runat="server"></asp:PlaceHolder>
                    </LayoutTemplate>
                    <GroupTemplate>
                        <asp:PlaceHolder ID="itemPlaceHolder" runat="server"></asp:PlaceHolder>
                    </GroupTemplate>
                    <ItemTemplate>
                        <div  runat="server" id="divSample">
                            <asp:CheckBox runat="server" ID="chkSample" />
                        </div>
                    </ItemTemplate>
                </asp:ListView>

            </ContentTemplate>
        </asp:UpdatePanel>

    </div>
    </form>
</body>
</html>

code behind:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Pgi.Hub.Testing
{
    public partial class ListTest : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                var list = new List<string> {"one", "two", "three"};
                lvSample.DataSource = list;
                lvSample.DataBind();
            }
        }

        protected void lvSample_ItemDataBound(object sender, ListViewItemEventArgs e)
        {
            if (!(e.Item is ListViewDataItem))
            {
                return;
            }

            var item = (string)e.Item.DataItem;

            var chkSelect = (CheckBox)e.Item.FindControl("chkSample");

            chkSelect.Text = item;

            if (item == "two")
            {
                chkSelect.CheckedChanged += chkSample_CheckedChanged;
                chkSelect.CausesValidation = false;
                chkSelect.AutoPostBack = true;
            }

        }

        public void chkSample_CheckedChanged(object sender, EventArgs e)
        {
            // Just looking to see if this code is reached
        }

    }
}

If I setup the checkbox's event handler on the page

<asp:CheckBox runat="server" ID="chkSample" OnCheckedChanged="chkSample_CheckedChanged" CausesValidation="false" AutoPostBack="true" />

then everything seems to work. But I'd rather not do this since that would cause an async postback on all the checkboxes, even the ones that don't need it, and I would need to determine that within the event handler method.

It almost seems like the CheckChanged handler isn't getting assigned correctly or is being lost at some point but I've looked at this for too long to be able to spot where the issue might be.




Aucun commentaire:

Enregistrer un commentaire