开发者

Listview Control problem

I开发者_高级运维 have a Listview control "lstStudents" and i have added checkboxes inside the List viewControl.I need to add a Select All check box which results in checking all the checkboxes inside the ListView i use the following code but it doesn't work.

  private void chkAll_CheckedChanged(object sender, EventArgs e)
    {
        foreach (Control cont in lstStudents.Controls)
        {
            if (cont.GetType() == typeof(CheckBox))
            {
                (cont as CheckBox).Checked = true;
            }

        }
    }

I'm using c# windows Forms......


You are talking to the dataitem instead of the control itself

    private void chkAll_CheckedChanged(object sender, EventArgs e)
    {
        foreach (ListViewItem item in lstStudents.Items)
        {
            item.Checked = chkAll.Checked;
        }
    }
  • the Checked property is always accessible on a ListViewItem, visible or not.
  • lstStudents.Items returns only ListViewItem

so there is no need for an extra reference validation on these items


Try this:

private void chkAll_CheckedChanged(object sender, EventArgs e)
{
    foreach (ListViewDataItem item in lstStudents.Items)
    {
        CheckBox cbSelect = item.FindControl("cbSelect") as CheckBox;
        if (cbSelect != null)
        {
            cbSelect.Checked = true;
        } 
     }
 }

Assuming your listview definition goes something like this:

<asp:listview runat="server">
    <itemtemplate>
        <asp:checkbox id="cbSelect" runat="server" />
    </itemtemplate>
</asp:listview>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜