1

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.

2
  • Please make it easier for others to support you by specifying more details, e.g. which framework in ASP.NET do you use? WebForms or MVC (add the appropriate tag). How does the HTML look like? Where do you want the processing to occur? On the server or on the client in JavaScript? What have you tried so far? Add the relevant parts of code. Commented Feb 12, 2016 at 8:36
  • Hey you can use Checkbox list where you can specify the repeat direction as horizontal Commented Feb 12, 2016 at 9:14

1 Answer 1

1

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 + ",";
                }
            }
        }
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.