1

我有一个嵌套的数据列表结构,并希望在每个类别上都选择所有复选框。

顶部复选框工作正常,但不知道如何为每个主要类别执行此操作

在此处输入图像描述

这工作正常:

 protected void cbTamaminiSec_CheckedChanged(object sender, EventArgs e)
{
    CheckBox cbTemp;

    foreach (DataListItem ItemP in parentDataList.Items)
    {
        cbTemp = (CheckBox)parentDataList.Items[ItemP.ItemIndex].FindControl("cbTumunuSec");
        cbTemp.Checked = cbTamaminiSec.Checked;

        DataList nestedDataList = (DataList)parentDataList.Items[ItemP.ItemIndex].FindControl("nestedDataList");

        foreach (DataListItem Item in nestedDataList.Items)
        {
            cbTemp = (CheckBox)nestedDataList.Items[Item.ItemIndex].FindControl("cbTamam");

            cbTemp.Checked = cbTamaminiSec.Checked;
        }
    }
}

但不知道部分选择(在父数据列表中的 chechboxes 代码下面)我将选择的 id 设置为“0”以强调问题

  protected void cbTumunuSec_CheckedChanged(Object sender, EventArgs e)
    {
        int selected = 0;//= (int)parentDataList.SelectedItem.ToString(); // problem is here..

        DataList nestedDataList = (DataList)parentDataList.Items[selected].FindControl("nestedDataList");

        foreach (DataListItem Item in nestedDataList.Items)
        {
            CheckBox cbTemp = (CheckBox)nestedDataList.Items[Item.ItemIndex].FindControl("cbTamam");

            cbTemp.Checked = true;

        }
    }

解决办法是什么

4

2 回答 2

1

您可以尝试使用此代码 - 基于OfType LINQ OperatorControls propery

var allControls = nestedDataList.Items[index].Controls.OfType<CheckBox>();

....//Filter with `Where Operator`

链接: http: //msdn.microsoft.com/fr-fr/library/system.web.ui.control.controls (v=vs.80).aspx

于 2012-09-20T13:36:31.523 回答
0

问题解决了!ASP代码:

<asp:HiddenField ID="hiddenBaslikId" runat="server" Value='<%# Eval("baslikId") %>' />

后面的代码:

protected void cbTumunuSec_CheckedChanged(Object sender, EventArgs e)
    {
    CheckBox checkAll = (CheckBox)sender;
    DataListItem item = (DataListItem)checkAll.NamingContainer;
    HiddenField HiddenID = (HiddenField)item.FindControl("hiddenBaslikId");

    int selected = Convert.ToInt32(HiddenID.Value)-1;

        DataList nestedDataList = (DataList)parentDataList.Items[selected].FindControl("nestedDataList");

        foreach (DataListItem Item in nestedDataList.Items)
        {
            CheckBox cbTemp = (CheckBox)nestedDataList.Items[Item.ItemIndex].FindControl("cbTamam");

            cbTemp.Checked = true;

        }
    }
于 2012-09-21T07:14:18.163 回答