亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

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

 更新時間:2013年12月05日 15:41:56   作者:  
廢話就不多說了,都說.NET是托控件的,我就托給你們看,這個博文主要講 DataGridView 的數(shù)據(jù)綁定,和選中行刪除功能

首現(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="負責人" 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的光棒效果,就是每一行鼠標懸浮上去變動背景色啦。

復(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控件,這個控件是放了一個刪除按鈕的小圖標,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控件來提示用戶,刪除成功!

相關(guān)文章

最新評論