[英]how can i add items which are checked to arraylist in Gridview c# asp.net
我想保留一些在阵列列表中检查过的数据。 我添加了复选框到gridview,当我检查项目之一。 复选框的单击事件全部运行,因此,例如,当我单击它运行的任何复选框4次时,我有两个数据,因此它对每个复选框都有效,但是,我只希望添加一个复选框中选中或未选中的项目列出。 有解决方案还是其他解决方案
// checkbox click event
protected void SelectedFriends_Click(object sender, EventArgs e)
{
bool isflag=false;
foreach (GridViewRow row in GridView1.Rows)
{
// Access the CheckBox
CheckBox cb = (CheckBox)row.FindControl("FriendSelector");
if (cb != null && cb.Checked)
{
string friendname = GridView1.Rows[row.RowIndex].Cells[1].Text.ToString();
for (int i = 0; i < list.Count; i++)
{
if (friendname.Equals(list[i].ToString()))
{
isflag = true;
}
}
// if it is added previously don't add to list
if(!isflag)
{
list.Add(friendname);
}
}
else
{
string friendname = GridView1.Rows[row.RowIndex].Cells[1].Text.ToString();
for (int i = 0; i < list.Count; i++)
{
if (friendname.Equals(list[i].ToString()))
{
isflag = true;
}
}
// if it is not checked and it is in list delete it from list
if(isflag)
{
list.Remove(friendname);
}
}
}
}
<asp:GridView ID="GridView1" runat="server" AllowPaging="True"
AutoGenerateColumns="False" DataSourceID="SqlDataSource1" Font-Bold="True"
Width="157px">
<Columns>
<asp:TemplateField HeaderText="Select">
<ItemTemplate>
<asp:CheckBox runat="server" id="FriendSelector"
oncheckedchanged="SelectedFriends_Click" AutoPostBack="True">
</asp:CheckBox>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="F_Name" HeaderText="Friend Name"
SortExpression="F_Name" />
</Columns>
</asp:GridView>
你可以试试这个吗?
protected void SelectedFriends_Click(object sender, EventArgs e)
{
list.Clear();
foreach (GridViewRow row in GridView1.Rows)
{
// Access the CheckBox
CheckBox cb = (CheckBox)row.FindControl("FriendSelector");
if (cb != null && cb.Checked)
{
string friendname = GridView1.Rows[row.RowIndex].Cells[1].Text.ToString();
list.Add(friendname);
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.