asp.net 不用GridView內建刪除功能,刪除一行資料

來源:互聯網
上載者:User

前台代碼: 複製代碼 代碼如下:<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執行刪除就可以了 。

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.