如何在ASP.Net中的Label中显示GridView的Checked(Selected)CheckBox行(数据) [英] How to display Checked (Selected) CheckBox rows (data) of GridView in Label in ASP.Net

查看:80
本文介绍了如何在ASP.Net中的Label中显示GridView的Checked(Selected)CheckBox行(数据)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

<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>



这是我的aspx页面。



并且代码落后




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);
               }
           }

       }



它正确显示选中的答案,

但是在取消选中它会删除标签中的所有数据,期望最后一个未选中的选项。

(在检查它时给出'E,B,C,D'并取消选中,最后未选中的选项将保留在标签中)我将如何解决我的问题。


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.

推荐答案

将您的功能更改为

Change Your function to
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);
 
       }


这篇关于如何在ASP.Net中的Label中显示GridView的Checked(Selected)CheckBox行(数据)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆