gpt4 book ai didi

c# - 在嵌套的 GridView 中使用复选框进行操作

转载 作者:太空宇宙 更新时间:2023-11-03 12:26:04 27 4
gpt4 key购买 nike

这是 GridView 的设计。

<asp:GridView ID="gmainrole" runat="server" OnRowDataBound="gmainrole_RowDataBound">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<input type="checkbox" id="chkavail" runat="server" checked='<%#Eval("checkstatus") %>' />
&nbsp;
<asp:Literal ID="litstate" runat="server" Text='<%#Eval("areaname") %>'></asp:Literal>
<asp:Literal ID="lituserrole" runat="server" Text='<%#Eval("nid") %>' Visible="false"></asp:Literal>
<asp:GridView ID="subrole" runat="server" AutoGenerateColumns="false" OnRowDataBound="subrole_RowDataBound">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:CheckBox ID="chk1vail" runat="server" Checked='<%#Eval("checkstatus") %>' />
<asp:Literal ID="litstate" runat="server" Text='<%#Eval("areaname") %>'></asp:Literal>
<asp:Literal ID="lituserrole" runat="server" Text='<%#Eval("nid") %>' Visible="false"></asp:Literal>
<asp:DataList ID="glastrole" runat="server" GridLines="None" AutoGenerateColumns="false" OnItemDataBound="glastrole_ItemDataBound">
<ItemTemplate>
<div>
<asp:CheckBoxList runat="server" ID="chklastrole">
</asp:CheckBoxList>
&nbsp;
<asp:Literal ID="Literal1" runat="server" Text='<%#Eval("areaname") %>'></asp:Literal>
<asp:Literal ID="litlast" runat="server" Text='<%#Eval("nid") %>' Visible="false"></asp:Literal>
<asp:DataList ID="ecounter" runat="server" GridLines="None" AutoGenerateColumns="false">
<ItemTemplate>
<asp:CheckBoxList runat="server" ID="chklastrole">
</asp:CheckBoxList>

&nbsp;
<asp:Literal ID="Literal11" runat="server" Text='<%#Eval("areaname") %>'></asp:Literal>
<asp:Literal ID="litlast1" runat="server" Text='<%#Eval("nid") %>' Visible="false"></asp:Literal>
</ItemTemplate>
</asp:DataList>
</div>
</ItemTemplate>
</asp:DataList>
</table>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</td>
</tr>
</table>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>

这个 GridView 是一个四层嵌套的 GridView,下面两层是 DataList,上面两层是 GridView。我希望当用户在名为 subrole(级别 2)的第二个 GridView 中检查复选框时,DataList glastrole(级别 3)和 DataList 中的所有后续复选框>ecounter(第 4 级)让他们的 CheckBoxes 检查特定的第 2 级复选框。我会给你我到目前为止尝试过的代码,但事实是我什至不知道如何解决这个问题。请帮忙!

最佳答案

试试这个可以解决你的问题:

添加 OnCheckedChanged 属性 <asp:CheckBox ID="chk1vail" OnCheckedChanged="Checked_Changed" runat="server" Checked='<%#Eval("checkstatus") %>' />

找到控件的隐藏代码:

    protected void Checked_Changed(object sender, EventArgs e)
{
// gets a row from GridView
GridViewRow subrole_row = ((CheckBox)sender).Parent as GridViewRow;

// get GridView
GridView subrole = subrole_row.Parent as GridView;

// find the datalist in GridView
DataList glastrole = subrole.Rows[subrole_row.RowIndex].FindControl("glastrole") as DataList;

glastrole.DataSource = glastrole_dt; // set your data to datalist
glastrole.DataBind(); // bind datalist

subrole.DataSource = subrole_dt; // set your data to gridview
subrole.DataBind(); // bind gridview
}

protected void glastrole_ItemDataBound(object sender, DataListItemEventArgs e)
{
if ((e.Item.ItemType == ListItemType.Item) || (e.Item.ItemType == ListItemType.AlternatingItem))
{
GridViewRow subrole_row = ((DataList)sender).Parent as GridViewRow;

CheckBox chk1vail = subrole_row.FindControl("chk1vail") as CheckBox;

// find CheckBoxList in glastrole
CheckBoxList chklastrole = e.Item.FindControl("chklastrole") as CheckBoxList;

// find if checkbox is checked
if (chk1vail.Checked)
{
// here you can check all the CheckBoxList items
foreach (ListItem item in chklastrole.Items)
{
item.Selected = true;
}
}


// find DataList in glastrole
DataList ecounter = e.Item.FindControl("ecounter") as DataList;

ecounter.DataSource = ecounter_dt; // set your data to datalist
ecounter.DataBind(); // bind datalist
}
}

protected void ecounter_ItemDataBound(object sender, DataListItemEventArgs e)
{
if ((e.Item.ItemType == ListItemType.Item) || (e.Item.ItemType == ListItemType.AlternatingItem))
{
DataList glastrole = ((DataList)sender).Parent as DataList;
DataList ecounter = glastrole.Parent as DataList;

GridViewRow subrole_row = ecounter.Parent as GridViewRow;

CheckBox chk1vail = subrole_row.FindControl("chk1vail") as CheckBox;

// find CheckBoxList in glastrole
CheckBoxList chklastrole = e.Item.FindControl("chklastrole") as CheckBoxList;

// find if checkbox is checked
if (chk1vail.Checked)
{
// here you can check all the CheckBoxList items
foreach (ListItem item in chklastrole.Items)
{
item.Selected = true;
}
}
}
}

关于c# - 在嵌套的 GridView 中使用复选框进行操作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44942896/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com