国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > .NET > 正文

ASP.NET中的DataGridView綁定數(shù)據(jù)和選中行刪除功能具體實例

2020-01-17 23:39:12
字體:
供稿:網(wǎng)友

首現(xiàn)我們拖入一個DataGridView控件到.aspx頁面中,然后綁定你需要顯示的列,具體代碼如下。

復(fù)制代碼 代碼如下:

 <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="負(fù)責(zé)人" 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ù)。

復(fù)制代碼 代碼如下:

protected void Page_Load(object sender, EventArgs e)
       {
           if (!IsPostBack)
           {
              gvDepartList.DataSource= new DepartInfoManager().GetDepartInfos(-1);
              gvDepartList.DataBind();
           }
       }

如果我們想添加一個DataGridView的光棒效果,就是每一行鼠標(biāo)懸浮上去變動背景色啦。

復(fù)制代碼 代碼如下:

/// <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控件,這個控件是放了一個刪除按鈕的小圖標(biāo),CommandArgument是干什么的呢?CommandName又是干什么的呢?CommandArgument就是指定我們要操作的參數(shù),CommandName就是指令這個按鈕是要干什么?這里用到的是刪除,我們寫上Delete。

復(fù)制代碼 代碼如下:

<asp:TemplateField HeaderText="刪除">
                <ItemTemplate>
                     <asp:ImageButton ImageUrl="../images/delete.gif" CommandArgument='<%#Eval("DepartId") %>' CommandName="delete" runat="server" />
                </ItemTemplate>
             </asp:TemplateField>

接下來就是后臺操作代碼了,可以看到這個DataGridView綁定了一個OnRowDeleting事件,這個事件就是用來刪除的。
然后我們在這個事件寫上這樣的代碼。

復(fù)制代碼 代碼如下:

/// <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控件來提示用戶,刪除成功!

發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 武冈市| 白山市| 锡林浩特市| 尼木县| 新和县| 灌云县| 谢通门县| 楚雄市| 铜鼓县| 蒙城县| 额尔古纳市| 乐安县| 内黄县| 昌平区| 新密市| 湾仔区| 三原县| 同仁县| 宣武区| 积石山| 独山县| 获嘉县| 剑川县| 黄大仙区| 大理市| 天柱县| 施甸县| 华阴市| 明水县| 扶风县| 云霄县| 宁陕县| 大同市| 板桥市| 汪清县| 杨浦区| 定结县| 贵定县| 扬州市| 喀什市| 绥中县|