(一)編輯樣板
(二)選取ItemTemplate模式並加入checkBox元件
(三)此時html原碼變會類似下面格式
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
Width="80%" onrowdeleting="GridView1_RowDeleting" DataKeyNames="pID">
<Columns>
<asp:TemplateField HeaderText="選取">
<ItemTemplate>
<asp:CheckBox ID="chkSelect" runat="server" AutoPostBack="false"/>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="pID" HeaderText="編號" Visible="False" />
<asp:HyperLinkField DataTextField="bookName" HeaderText="書名"
DataNavigateUrlFields="pID"
DataNavigateUrlFormatString="publishModify.aspx?id={0}" />
<asp:HyperLinkField DataTextField="price" HeaderText="價格"
DataNavigateUrlFields="pID"
DataNavigateUrlFormatString="publishModify.aspx?id={0}" />
<asp:HyperLinkField DataTextField="bookDate" HeaderText="刊登日期"
DataNavigateUrlFields="pID"
DataNavigateUrlFormatString="publishModify.aspx?id={0}" />
</Columns>
</asp:GridView>
(四)對其進行尋訪
for (int iRow = 0; iRow < GridView1.Rows.Count; iRow++)
{
CheckBox chkSelect = (CheckBox)GridView1.Rows[iRow].FindControl("chkSelect");
if (chkSelect.Checked == true)
{
string pID = GridView1.DataKeys[iRow].Value.ToString();
DeleteSql(String.Format("delete from Publish where pID = '{0}'", pID));
}
}
留言
張貼留言