|
復制代碼 代碼如下:
<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;
//執(zhí)行刪除
ClientScript.RegisterStartupScript(GetType(), "Message", "<SCRIPT
LANGUAGE='Javascript'>alert('刪除成功!');</script>");
GridBind();
}
關鍵是設定好DataKeyNames后,可以靠 string strID = GridLog.DataKeys
[e.RowIndex].Value.ToString();獲得選擇列的ID值 然后用這個ID執(zhí)行刪除就可以了 。
AspNet技術:asp.net 不用GridView自帶刪除功能,刪除一行數據,轉載需保留來源!
鄭重聲明:本文版權歸原作者所有,轉載文章僅為傳播更多信息之目的,如作者信息標記有誤,請第一時間聯系我們修改或刪除,多謝。