前台代碼:
複製代碼 代碼如下:<asp:GridView ID="GridLog" runat="server"
AutoGenerateColumns="False" CellPadding="4" DataKeyNames="ID"
BorderColor="#333" BorderStyle="solid" BorderWidth="1"
OnRowDeleting="PublicGridRowDeleting"
GridLines="None" Width="98%" ForeColor="#333333">
<FooterStyle BackColor="#507CD1" ForeColor="White" Font-Bold="True" />
<Columns>
<asp:BoundField DataField="ID" HeaderText="ID" InsertVisible="false"
ReadOnly="True"
SortExpression="ID" >
<ItemStyle HorizontalAlign="Center" Width="20px" />
</asp:BoundField>
<asp:TemplateField HeaderText="刪除" ShowHeader="False">
<ItemStyle HorizontalAlign="Center" Width="40px" />
<ItemTemplate>
<asp:LinkButton ID="LinkButton1" runat="server"
CausesValidation="False" CommandName="Delete" OnClientClick="return confirm('您確認刪
除?');" Text="刪除"></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
</Columns>
<RowStyle BackColor="#EFF3FB" />
<SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333"
/>
<PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center"
/>
<HeaderStyle BackColor="#5A799C" ForeColor="White" Height="22px" />
<AlternatingRowStyle BackColor="White" />
<EditRowStyle BackColor="#2461BF" />
<EmptyDataTemplate>
日誌庫暫時為空白!
</EmptyDataTemplate>
</asp:GridView>
CS代碼 複製代碼 代碼如下:protected void PublicGridRowDeleting(object sender, GridViewDeleteEventArgs e)
{
string strID = GridLog.DataKeys[e.RowIndex].Value.ToString();//strID就是該行的ID
string strSQL = "Delete from table " +
" WHERE id = " + strID;
//執行刪除
ClientScript.RegisterStartupScript(GetType(), "Message", "<SCRIPT
LANGUAGE='javascript'>alert('刪除成功!');</script>");
GridBind();
}
關鍵是設定好DataKeyNames後,可以靠 string strID = GridLog.DataKeys
[e.RowIndex].Value.ToString();獲得選擇列的ID值 然後用這個ID執行刪除就可以了 。