mercredi 20 avril 2016

Checking if a list-view generated checkbox is checked or not without knowing the id

Yesterday, I submitted a question here about retrieving the selected checkboxes from a ValidationGroup. That question got sorted out, so I thought it would be simple to figure out the next problem, running a delete statement if the checkbox is checked.

The problem is, even when I check boxes and click enter, breakpoints in my code are showing that the value of .Checked is always "false". This is strange because values like .Text, or .ID are contextually correct for the item being looked at.

Here is the code:

ControlFinder<CheckBox> finder = new ControlFinder<CheckBox>();
    finder.FindChildControlsRecursive(lvUsers);

    foreach (CheckBox chkUser in finder.FoundControls)
    {
        if (chkUser.Checked == true && chkUser.ValidationGroup == "userCheck")
        {
            //Delete statement goes in here.
        } 

Again, the values for chkUser.ValidationGroup and chkUser.Text are correct, but not chkUser.Checked.

Here is the code for the ControlFinder function, mentioned in the previous post and ultimately used from Jimmy in this post:

private class ControlFinder<T> where T : Control
{
    private readonly List<T> _foundControls = new List<T>();
    public IEnumerable<T> FoundControls
    {
        get { return _foundControls; }
    }

    public void FindChildControlsRecursive(Control control)
    {
        foreach (Control childControl in control.Controls)
        {
            if (childControl.GetType() == typeof(T))
            {
                _foundControls.Add((T)childControl);
            }
            else
            {
                FindChildControlsRecursive(childControl);
            }
        }
    }
}

And here is the ASP .NET markup for that section of the page:

        <asp:ListView ID="lvUsers" runat="server">
            <ItemTemplate>
                    <asp:CheckBox ID="chkUser" runat="server" Text='<%# Eval("userName") %>' ValidationGroup="userCheck" /><br />
            </ItemTemplate>
        </asp:ListView>

Any help would be appreciated, this has been a shockingly irritating problem to solve.




Aucun commentaire:

Enregistrer un commentaire