asp.net 2.0中刪除資料的操作

來源:互聯網
上載者:User
一個很典型的情境,一個GRIDVIEW中的每個資料行,有兩個圖片按鈕 “操作(修改)”,“刪除”,
先來看前端的代碼

<asp:ScriptManager ID="sm" runat="server" ></asp:ScriptManager>
    <table class="Table" border="0" cellpadding="5" cellspacing="0">
  <tr>
   <td colspan="2">
    <asp:UpdatePanel runat="server" ID="up">
     <ContentTemplate>
      <asp:GridView ID="gvCategory" runat="server" Width="100%" AutoGenerateColumns="False" SkinID="gvSkin" OnRowCommand="gvCategory_RowCommand" OnRowDataBound="gvCategory_RowDataBound" AllowPaging="True" OnPageIndexChanging="gvCategory_PageIndexChanging">
       <Columns>
        <asp:TemplateField HeaderText="相簿分類">
         <ItemTemplate>
          <a href='Category.aspx?CategoryID=<%# Eval("ID") %>'><%# Eval("Name") %>(<%# Eval("PhotoCount") %>)</a>
         </ItemTemplate>
         <ItemStyle HorizontalAlign="Left" Width="70%" />
         <HeaderStyle HorizontalAlign="Left" />
        </asp:TemplateField>
        <asp:TemplateField HeaderText="分類狀態">
         <ItemTemplate>
          <%# (byte)Eval("Status") == 0 ? "公開" : "私人" %>          
         </ItemTemplate>
         <ItemStyle HorizontalAlign="Center" Width="18%" />
         <HeaderStyle HorizontalAlign="Center" />
        </asp:TemplateField>
        <asp:TemplateField HeaderText="操作">
         <ItemTemplate>
          <asp:ImageButton ID="imgUpdate" runat="server" CommandArgument='<%# Eval("ID") %>' ImageUrl="~/App_Themes/ASPNETAjaxWeb/Images/edit.PNG" CommandName="update" />&nbsp;
          <asp:ImageButton ID="imgDelete" runat="server" Visible='<%# (int)Eval("PhotoCount") > 0 ? false : true %>' CommandArgument='<%# Eval("ID") %>' ImageUrl="~/App_Themes/ASPNETAjaxWeb/Images/delete.PNG" CommandName="del" />        
         </ItemTemplate>
         <ItemStyle HorizontalAlign="Center" Width="12%" />
         <HeaderStyle HorizontalAlign="Center" />
        </asp:TemplateField>
       </Columns>
       <PagerSettings Mode="NextPreviousFirstLast" />
      </asp:GridView>
     </ContentTemplate>
    </asp:UpdatePanel>    

要點是,修改和刪除的圖片按鈕的commandname要設定好,要注意commandargument的設定,因為這裡代表裡當前行的ID,刪除的時候要用
然後要設計rowcommand事件,在使用者單擊控制項每行中的按鈕時觸發
protected void gvCategory_RowCommand(object sender,GridViewCommandEventArgs e)
 {
  if(e.CommandName.ToLower() == "update")
  {   ///重新導向到修改分類頁面
   Response.Redirect("~/UpdateCategory.aspx?CategoryID=" + e.CommandArgument.ToString());
   return;
  }
  if(e.CommandName.ToLower() == "del")
  {   ///刪除選擇的相簿分類
   Album album = new Album();
   if(album.DeleteCategory(Int32.Parse(e.CommandArgument.ToString())) > 0)
   {
    BindPageData();
   }
   return;
  }

而要為刪除按鈕加提示資訊,則需要在rowdatabound事件中說明,在每一行資料繫結後觸發,為每個刪除的按鈕增加用戶端的確認
protected void gvCategory_RowDataBound(object sender,GridViewRowEventArgs e)
 {   ///添加刪除確認的對話方塊
  ImageButton imgDelete = (ImageButton)e.Row.FindControl("imgDelete");
  if(imgDelete != null)
  {
   imgDelete.Attributes.Add("onclick","return confirm(\"您確認要刪除當前行的相簿分類嗎?\");");
  }
 }

當然,分頁部分也寫上吧
protected void gvCategory_PageIndexChanging(object sender,GridViewPageEventArgs e)
 {   ///設定新的頁碼,並重新顯示資料
  gvCategory.PageIndex = e.NewPageIndex;
  BindPageData();
 }
還有rowdatabound事件也經常用,比如資料庫取出來的1,0欄位,要變為“男”,“女”顯示,則這樣
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)

{

//判斷當前行是否是資料行

        if (e.Row.RowType == DataControlRowType.DataRow)

        {  //用FindControl方法找到模板中的Label控制項

Label lb1= (Label)e.Row.FindControl("Label1");

//因為RowDataBound是發生在資料繫結之後,所以我們可以

//判斷Label繫結資料,如果是True,就更改其text屬性為男

                if (lb1.Text== "True")

                      lb1.Text = "男";

                else

                      lb1.Text = "female";

        }

    }

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.