asp.net 不用GridView自帶刪除功能,刪除一行數(shù)據(jù)
更新時間:2009年11月30日 20:39:19 作者:
數(shù)據(jù)表一定要有個ID的主鍵值,你的gridview要設(shè)定一下DataKeyNames="ID"這個屬性值,接下的事件就好多了,寫個OnRowDeleting事件就可以了。
前臺代碼:
<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('您確認(rèn)刪
除?');" 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();
}
關(guān)鍵是設(shè)定好DataKeyNames后,可以靠 string strID = GridLog.DataKeys
[e.RowIndex].Value.ToString();獲得選擇列的ID值 然后用這個ID執(zhí)行刪除就可以了 。
復(fù)制代碼 代碼如下:
<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('您確認(rèn)刪
除?');" 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代碼
復(fù)制代碼 代碼如下:
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();
}
關(guān)鍵是設(shè)定好DataKeyNames后,可以靠 string strID = GridLog.DataKeys
[e.RowIndex].Value.ToString();獲得選擇列的ID值 然后用這個ID執(zhí)行刪除就可以了 。
您可能感興趣的文章:
- asp.net GridView控件中模板列CheckBox全選、反選、取消
- Asp.net GridView使用大全(分頁實(shí)現(xiàn))
- asp.net gridview代碼綁定
- asp.net GridView 刪除時彈出確認(rèn)對話框(包括內(nèi)容提示)
- asp.net Gridview里添加匯總行
- asp.net GridView排序簡單實(shí)現(xiàn)
- asp.net DataGridView導(dǎo)出到Excel的三個方法[親測]
- asp.net中讓Repeater和GridView支持DataPager分頁
- asp.net 設(shè)置GridView的選中行
- asp.net gridview的Rowcommand命令中獲取行索引的方法總結(jié)
- asp.net實(shí)現(xiàn)固定GridView標(biāo)題欄的方法(凍結(jié)列功能)
相關(guān)文章
Asp.net mvc 數(shù)據(jù)調(diào)用示例代碼
Asp.net mvc 數(shù)據(jù)調(diào)用示例代碼,學(xué)習(xí)asp.net mvc框架的朋友可以參考下。2010-10-10
win7系統(tǒng)下 vs2010 調(diào)式就關(guān)閉要重啟處理方法
最近經(jīng)常出現(xiàn)只要一使用vs2010進(jìn)行調(diào)試,就提示關(guān)閉并要重啟,好煩,度娘了半天,總結(jié)下來解決方法,親測可用哦。2014-08-08
C# 沒有動態(tài)的數(shù)組,可以用arraylist或list取代
C#里沒有動態(tài)的數(shù)組,只能用arraylist或list取代。2009-06-06
asp.net使用Repeater控件中的全選進(jìn)行批量操作實(shí)例
這篇文章主要介紹了asp.net使用Repeater控件中的全選進(jìn)行批量操作方法,實(shí)例分析了Repeater控件的使用技巧,需要的朋友可以參考下2015-01-01

