简体   繁体   中英

Select listview items using checkbox

I have a listview that has a checkbox inside. What i want to happen is when i click the process button the values of the checked items is pass to a label.

Ex. Output: 1,2,3,4,5,6,7,8,9

This is my HTML code.

<asp:ListView ID="ListView1" runat="server" DataKeyNames="CalendarDays" DataSourceID="sdsDays" GroupItemCount="7">


                <AlternatingItemTemplate>
                    <td>
                        <asp:CheckBox ID="CheckBox1" runat="server" />
                    </td>
                    <td runat="server" style=""><%--CalendarDays:--%>
                        <asp:Label ID="CalendarDaysLabel" runat="server" Text='<%# Eval("CalendarDays") %>' />
                        <br />
                    </td>
                </AlternatingItemTemplate>
                <EditItemTemplate>
                    <td>
                        <asp:CheckBox ID="CheckBox1" runat="server" />
                    </td>
                    <td runat="server" style=""><%--CalendarDays:--%>
                        <asp:Label ID="CalendarDaysLabel1" runat="server" Text='<%# Eval("CalendarDays") %>' />
                        <br />
                        <asp:Button ID="UpdateButton" runat="server" CommandName="Update" Text="Update" />
                        <br />
                        <asp:Button ID="CancelButton" runat="server" CommandName="Cancel" Text="Cancel" />
                        <br />
                    </td>
                </EditItemTemplate>
                <EmptyDataTemplate>
                    <table runat="server" style="">
                        <tr>
                            <td>No data was returned.</td>
                        </tr>
                    </table>
                </EmptyDataTemplate>
                <EmptyItemTemplate>
                    <td runat="server" />
                </EmptyItemTemplate>
                <GroupTemplate>
                    <tr id="itemPlaceholderContainer" runat="server">
                        <td id="itemPlaceholder" runat="server"></td>
                    </tr>
                </GroupTemplate>
                <InsertItemTemplate>
                    <td>
                        <asp:CheckBox ID="CheckBox1" runat="server" />
                    </td>
                    <td runat="server" style=""><%--CalendarDays:--%>
                        <asp:TextBox ID="CalendarDaysTextBox" runat="server" Text='<%# Bind("CalendarDays") %>' />
                        <br />
                        <asp:Button ID="InsertButton" runat="server" CommandName="Insert" Text="Insert" />
                        <br />
                        <asp:Button ID="CancelButton" runat="server" CommandName="Cancel" Text="Clear" />
                        <br />
                    </td>
                </InsertItemTemplate>
                <ItemTemplate>
                    <td>
                        <asp:CheckBox ID="CheckBox1" runat="server" />
                    </td>
                    <td runat="server" style=""><%--CalendarDays:--%>
                        <asp:Label ID="CalendarDaysLabel" runat="server" Text='<%# Eval("CalendarDays") %>' />
                        <br />
                    </td>
                </ItemTemplate>
                <LayoutTemplate>
                    <table runat="server">
                        <tr runat="server">
                            <td runat="server">
                                <table id="groupPlaceholderContainer" runat="server" border="0" style="">
                                    <tr id="groupPlaceholder" runat="server">
                                    </tr>
                                </table>
                            </td>
                        </tr>
                        <tr runat="server">
                            <td runat="server" style=""></td>
                        </tr>
                    </table>
                </LayoutTemplate>
                <SelectedItemTemplate>
                    <td>
                        <asp:CheckBox ID="CheckBox1" runat="server" />
                    </td>
                    <td runat="server" style=""><%--CalendarDays:--%>
                        <asp:Label ID="CalendarDaysLabel" runat="server" Text='<%# Eval("CalendarDays") %>' />
                        <br />
                    </td>
                </SelectedItemTemplate>


        </asp:ListView>

I tried it in a gridview

this is my code

         string valueListDate = string.Empty;
        int countDate = 0;


        foreach (GridViewRow row in grdDay.Rows)
        {
            if ((row.FindControl("chkDate") as CheckBox).Checked)
            {
                string Dates = row.Cells[1].Text;
          valueListDate += "" + Dates + ",";
                countDate++;
            }
        }


        if (countDate == 0)
        {
            System.Threading.Thread.Sleep(500);
        }

        else if (countDate == 1)
        {
            System.Threading.Thread.Sleep(500);
            valueListDate = valueListDate.Substring(0, valueListDate.Length - 1); 

            lbldates.Text = valueListDate;
        }

        else
        {

            valueListDate = valueListDate.Substring(0, valueListDate.Length - 1); 
            lbldates.Text = valueListDate;

        }

        txtDate.Text = ddlSelectMonth.SelectedValue + " " + lbldates.Text + " " + DateTime.Now.Year.ToString();
        ModalPopupExtenderAFindings.Show();

its working but i like to use a listview because i want the selecting to be like this LISTVIEW which the gridview can't do.

please help.

The following code snippet shall help you..

<asp:CheckBoxList ID="CheckBoxList1" runat="server" RepeatDirection="Horizontal" RepeatColumns="3" RepeatLayout="Table">
    </asp:CheckBoxList>

In the Code-behind you can do this

protected void btnProcess_click(object sender, EventArgs e)
        {
            foreach (ListItem item in CheckBoxList1.Items)
            {
                if (item.Selected)
                {
                    lblDisplay.Text += item.Text + ",";
                }
            }
        }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM