後端寫法效能較差
<asp:Button ID="Button1" runat="server" Text="取得打勾項目" OnClick="Btn_Click" />
<div>
</div>
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataSourceID="SqlDataSource1" DataKeyNames="ProductID" >
<Columns>
<asp:TemplateField>
<HeaderTemplate>
<asp:CheckBox ID="ck_head" runat="server" OnCheckedChanged="Ck_CheckedChanged" AutoPostBack="true" />
</HeaderTemplate>
<ItemTemplate>
<asp:CheckBox ID="ck" runat="server" />
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="ProductID" HeaderText="ProductID" SortExpression="ProductID" InsertVisible="False" ReadOnly="True" />
<asp:BoundField DataField="UnitPrice" HeaderText="UnitPrice" SortExpression="UnitPrice" />
<asp:BoundField DataField="UnitsInStock" HeaderText="UnitsInStock" SortExpression="UnitsInStock" />
<asp:BoundField DataField="ProductName" HeaderText="ProductName" SortExpression="ProductName" />
</Columns>
<EmptyDataRowStyle ForeColor="Red" />
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:NORTHWNDConnectionString %>" SelectCommand="SELECT [ProductID], [UnitPrice], [UnitsInStock], [ProductName] FROM [Products]"></asp:SqlDataSource>
protected void Ck_CheckedChanged(object sender, EventArgs e)
{
foreach (GridViewRow row in GridView1.Rows)
{
((CheckBox)row.FindControl("ck")).Checked = ((CheckBox)sender).Checked;
}
}
protected void Btn_Click(object sender, EventArgs e)
{
List<string> list = new List<string>();
for (int i = 0; i < GridView1.Rows.Count; i++)
{
if(((CheckBox)GridView1.Rows[i].FindControl("ck")).Checked == true)
{
list.Add(GridView1.Rows[i].Cells[1].Text);
}
}
}