ASP.NET中的DataGridView綁定數(shù)據(jù)和選中行刪除功能具體實例
首現(xiàn)我們拖入一個DataGridView控件到.aspx頁面中,然后綁定你需要顯示的列,具體代碼如下。
<asp:GridView ID="gvDepartList" runat="server" AutoGenerateColumns="False"
Height="108px" Width="600px" OnRowDeleting="gvDepartList_RowDeleting" RowDataBound="gvDepartList_RowDataRound">
<Columns>
<asp:TemplateField HeaderText="部門名稱" >
<ItemTemplate>
<asp:Label runat="server" style="text-align:center" Text='<%# Eval("DepartName") %>' />
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField HeaderText="機構(gòu)" DataField="BranchId" />
<asp:BoundField HeaderText="負責人" DataField="PrincipalUser" />
<asp:BoundField HeaderText="聯(lián)系電話" DataField="ConnectTelNo" />
<asp:BoundField HeaderText="移動電話" DataField="ConnectMobileTelNo"/>
<asp:BoundField HeaderText="傳真" DataField="Faxes" />
<asp:TemplateField HeaderText="修改">
<ItemTemplate>
<asp:ImageButton ID="ImageButton1" ImageUrl="../images/edit.gif" CommandArgument='<%#Eval("DepartId") %>' CommandName="delete" runat="server" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="刪除">
<ItemTemplate>
<asp:ImageButton ImageUrl="../images/delete.gif" CommandArgument='<%#Eval("DepartId") %>' CommandName="delete" runat="server" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
二:在這個.aspx頁面后臺的Page_load事件中綁定數(shù)據(jù)。
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
gvDepartList.DataSource= new DepartInfoManager().GetDepartInfos(-1);
gvDepartList.DataBind();
}
}
如果我們想添加一個DataGridView的光棒效果,就是每一行鼠標懸浮上去變動背景色啦。
/// <summary>
/// 動態(tài)注冊腳本(在GridView控件呈現(xiàn)之前) 光棒效果
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void gvUsers_RowDataBound(object sender, GridViewRowEventArgs e)
{
//此處判斷只有在數(shù)據(jù)行在進行腳本注冊
if (e.Row.RowType == DataControlRowType.DataRow)
{
//光棒效果
e.Row.Attributes.Add("onmouseover","currentcolor=this.style.backgroundColor;this.style.backgroundColor='#6699ff'");
e.Row.Attributes.Add("onmouseout ", "this.style.backgroundColor=currentcolor");
LinkButton lnkbtnDel = e.Row.FindControl("lnkbtnDel") as LinkButton;
lnkbtnDel.Attributes.Add("onclick", "return confirm('確定刪除嗎?')");
}
}
現(xiàn)在重點來了,怎么一行的數(shù)據(jù)呢?既然是刪除,我們肯定是要根據(jù)一條數(shù)據(jù)的ID來刪除了,那么我們在Page_load方法中加入一段代碼:
gvDepartList.DataKeyNames = new string[] { "id"};//這個代碼是什么意思呢,就是每一行設(shè)置一個鍵,這個鍵就是用來操作數(shù)據(jù)的。
現(xiàn)在我們用另外一種方法刪除,看到頁面中的倒數(shù)第二列,沒錯,是一個ImageButtom控件,這個控件是放了一個刪除按鈕的小圖標,CommandArgument是干什么的呢?CommandName又是干什么的呢?CommandArgument就是指定我們要操作的參數(shù),CommandName就是指令這個按鈕是要干什么?這里用到的是刪除,我們寫上Delete。
<asp:TemplateField HeaderText="刪除">
<ItemTemplate>
<asp:ImageButton ImageUrl="../images/delete.gif" CommandArgument='<%#Eval("DepartId") %>' CommandName="delete" runat="server" />
</ItemTemplate>
</asp:TemplateField>
接下來就是后臺操作代碼了,可以看到這個DataGridView綁定了一個OnRowDeleting事件,這個事件就是用來刪除的。
然后我們在這個事件寫上這樣的代碼。
/// <summary>
/// 刪除選中的行
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void gvDepartList_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
ImageButton buttom = gvDepartList.Rows[e.RowIndex].FindControl("btnDelete") as ImageButton;
string departId = buttom.CommandArgument.ToString();
if (manage.DeleteDepart(departId))
{
Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "alert", "<script>alert('刪除成功!');</script>");
BindDepartInfos();//重新綁定數(shù)據(jù)
}
else
{
Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "alert", "<script>alert('刪除失敗!');</script>");
}
}
為了更好的用戶體驗,我們可以不使用這個Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "alert", "<script>alert('刪除成功!');</script>");
可以選擇在頁面中顯眼的地方放一個label控件,設(shè)計Visible=false;隱藏它,然后刪除成功后,利用這個Label控件來提示用戶,刪除成功!
相關(guān)文章
asp.net DataFormatString格式化GridView
在GridView里面顯示數(shù)據(jù),要顯示的數(shù)據(jù)有好多位小數(shù),就想讓它只顯示兩位小數(shù),在delphi里,直接用DisplayFormat就行了,2009-08-08ASP.NET Global.asax應(yīng)用程序文件簡介
Global.asax 文件,有時候叫做 ASP.NET 應(yīng)用程序文件,提供了一種在一個中心位置響應(yīng)應(yīng)用程序級或模塊級事件的方法。2009-03-03ASP.NET Core文件壓縮常見使用誤區(qū)(最佳實踐)
本文給大家分享ASP.NET Core文件壓縮常見的三種誤區(qū),就每種誤區(qū)給大家講解的非常詳細,是項目實踐的最佳紀錄,對ASP.NET Core文件壓縮相關(guān)知識感興趣的朋友一起看看吧2021-05-05詳解ASP.NET Core 處理 404 Not Found
這篇文章主要介紹了詳解ASP.NET Core 處理 404 Not Found,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-10-10詳解.NET Core 使用HttpClient SSL請求出錯的解決辦法
這篇文章主要介紹了.NET Core 使用HttpClient SSL請求出錯的解決辦法,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2019-03-03創(chuàng)建一個ASP.NET MVC5項目的實現(xiàn)方法(圖文)
這篇文章主要介紹了創(chuàng)建一個ASP.NET MVC 5項目,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2020-09-09