2016-08-24 89 views
0

我有一個動態綁定網格視圖的內容頁面。我還添加了編輯和刪除按鈕。但點擊編輯按鈕時,如何生成更新和取消鏈接按鈕。在點擊asp.net中的編輯按鈕時生成更新和取消按鈕

我知道它必須在RowEditing Event中處理。請詳細說明如何獲取這些按鈕。 這裏是ap.net網頁

<%@ Page Title="" Language="C#" MasterPageFile="~/ManageSMS.Master" AutoEventWireup="true" CodeBehind="WebForm10.aspx.cs" Inherits="SMS_Mod2.WebForm10" %> 
<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="server"> 
</asp:Content> 
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="server"> 
    <asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="true" OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged"></asp:DropDownList> 
    <asp:Panel ID="Panel1" Visible="false" runat="server"><asp:DropDownList ID="DropDownList2" AutoPostBack="true" OnSelectedIndexChanged="DropDownList2_SelectedIndexChanged" runat="server"></asp:DropDownList></asp:Panel> 
    <asp:GridView ID="GridView1" runat="server"> 
     <Columns> 
      <asp:CommandField ShowDeleteButton ="true" /> 
      <asp:CommandField ShowEditButton="true" /> 
     </Columns> 
    </asp:GridView> 
</asp:Content> 

這裏是代碼隱藏文件。

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Web; 
using System.Web.UI; 
using System.Web.UI.WebControls; 
using BOL; 
using DAL; 
namespace SMS_Mod2 
{ 
    public partial class WebForm10 : System.Web.UI.Page 
    { 
     StationaryManagement stMgmt = new StationaryManagement(); 
     protected void Page_Load(object sender, EventArgs e) 
     { 
      if (!IsPostBack) 
      { 
       DropDownList1.Items.Add(new ListItem("All", "0")); 
       DropDownList1.Items.Add(new ListItem("Branch", "1")); 
       //DropDownList1.DataBind(); 

      } 
     } 

     protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e) 
     { 
      if(DropDownList1.SelectedIndex != 0) 
      { 
       Panel1.Visible = true; 
       DropDownList2.DataSource = stMgmt.ViewBranches(); 
       DropDownList2.DataTextField = "BranchName"; 
       DropDownList2.DataValueField = "BranchID"; 
       DropDownList2.DataBind(); 
       DropDownList2.Items.Add(new ListItem("Please select", "0")); 
      } 
      else 
      { 
       Panel1.Visible = false; 
      } 
     } 

     protected void DropDownList2_SelectedIndexChanged(object sender, EventArgs e) 
     { 

       GridView1.DataSource = stMgmt.ViewLocations(DropDownList2.SelectedIndex); 
      GridView1.DataBind(); 
      GridView1.AllowSorting = true; 
      GridView1.AutoGenerateEditButton = true; 
      GridView1.AutoGenerateDeleteButton = true; 

     } 
    } 
} 

回答

0

GridView有一個AutoGenerateEditButton屬性,你可以用它來代替。此設置還會在編輯時顯示更新/取消按鈕。

<asp:GridView ID="GridView1" runat="server" AutoGenerateEditButton="true"> 
    <Columns> 

    </Columns> 
</asp:GridView> 

要更新您的網格,你還需要使用下面的GridView事件:OnRowEditingOnRowCancelingEditOnRowUpdating

在編輯事件中,您通常不需要執行任何操作。在代碼隱藏的更新事件中,您需要執行更新邏輯並最終設置GridView1.EditIndex = -1;以結束編輯。最後,在取消事件中,您只需要再次將EditIndex設置爲-1。

還有一個屬性可以自動生成刪除按鈕。如果我記得正確,那麼您需要從​​事件中的數據集中刪除該行。

GridView控件會是這個樣子,那麼:

<asp:GridView ID="GridView1" runat="server" AutoGenerateDeleteButton="true" OnRowDeleting="GridView1_RowDeleting" AutoGenerateEditButton="true" OnRowEditing="GridView1_RowEditing" OnRowCancelingEdit="GridView1_RowCancelingEdit" OnRowUpdating="GridView1_RowUpdating"> 
相關問題