asp.net 2.0中删除数据的操作

一个很典型的场景,一个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";

        }

    }

展开阅读全文

没有更多推荐了,返回首页