Click here to Skip to main content
15,886,110 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
<div style="height: 150px; width: 221px;">
                <input type="text" placeholder="Find" id="txtSearchCompany" style="width: 100%;" class="textboxWidth" />
                <div style="height: 150px; width: 221px; overflow: auto;" class="textboxWidth">
                    <asp:GridView ID="gvCompanyListing" runat="server" AutoGenerateColumns="False" ShowHeader="False" GridLines="None">
                        <Columns>
                            <asp:TemplateField>
                                <ItemTemplate>
                                    <asp:CheckBox ID="cbSelect" CssClass="gridCB" runat="server" ItemStyle-Width="10%"  OnCheckedChanged="Select_OnCheckedChanged" AutoPostBack="true" ></asp:CheckBox>
                                </ItemTemplate>
                            </asp:TemplateField>
                            <asp:BoundField DataField="CompanyInfo" SortExpression="CompanyInfo" ItemStyle-Width="92%" ItemStyle-Height="1px"></asp:BoundField>
                        </Columns>
                    </asp:GridView>
                </div>
            </div>
        </div>
        <asp:Label ID="lblmsg" runat="server" />
</div>

this is my aspx page.

And in code behind

protected void Select_OnCheckedChanged(object sender, EventArgs e)
       {
           string name = string.Empty;
           foreach (GridViewRow row in this.gvCompanyListing.Rows)
           {
               CheckBox chkSelect = row.FindControl("cbSelect") as CheckBox;
               if (chkSelect.Checked)
               {
                   name += row.Cells[1].Text + ",";
                   String[] tokens = name.Split(new[] { "," }, StringSplitOptions.RemoveEmptyEntries);
                   this.lblmsg.Text = string.Join("<br />", tokens);
               }
           }

       }

it displays the checked answers correctly,
but on unchecking it remove all the data from label expect the last unchecked option.
(On checking it give 'E,B,C,D' and on unchecking ,the last unchecked options will remain in label)How i will solve my issue.
Posted
Updated 18-Nov-15 23:36pm
v4

1 solution

Change Your function to
C#
protected void Select_OnCheckedChanged(object sender, EventArgs e)
       {
           string name = string.Empty;
           foreach (GridViewRow row in this.gvCompanyListing.Rows)
           {
               CheckBox chkSelect = row.FindControl("cbSelect") as CheckBox;
               if (chkSelect.Checked)
               {
                   name += row.Cells[1].Text + ",";                  
               }
           }
 String[] tokens = name.Split(new[] { "," }, StringSplitOptions.RemoveEmptyEntries);
                   this.lblmsg.Text = string.Join("<br />", tokens);
 
       }
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900