1

我正在制作一个可编辑的GridView,但我的问题是,每当我点击一个按钮时,什么都没有发生。当我第二次单击时,我看到了上一次单击期间发生的情况。

aspx

 <%@ Page Title="Home Page" Language="C#"
 MasterPageFile="~/Site.master" AutoEventWireup="true"
 EnableEventValidation="true"
     CodeBehind="Default.aspx.cs" Inherits="BeheerSysteemWeb._Default" %>

 <asp:Content ID="HeaderContent" runat="server"
 ContentPlaceHolderID="HeadContent"> </asp:Content> <asp:Content
 ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">
     <h2>
         <asp:GridView ID="GridView1" runat="server"
         onrowcancelingedit="GridView1_RowCancelingEdit" 
             onrowediting="GridView1_RowEditing" 
             onrowupdating="GridView1_RowUpdating" AutoGenerateColumns="False">
             <Columns>               
                 <asp:CommandField ButtonType="Button" ShowEditButton="true" ShowCancelButton="true" />              
                 <asp:TemplateField HeaderText="36">
           <EditItemTemplate>
             <asp:TextBox runat="server" ID="txtSpoor" Text="TramNummer" />
           </EditItemTemplate>
       </asp:TemplateField>
             </Columns>        
         </asp:GridView>
     </h2>
     </asp:Content>

代码隐藏

 namespace BeheerSysteemWeb 
 {
     public partial class _Default : System.Web.UI.Page
     {
         List<string> leeg = new List<string>();
         protected void Page_Load(object sender, EventArgs e)
         {
             if (!Page.IsPostBack)
             {
                 LoadData();
             }
         }

         private void LoadData()
         {
             leeg.Add("");
             leeg.Add("");
             leeg.Add("");
             leeg.Add("");
             GridView1.DataSource = leeg;
             GridView1.DataBind();
         }

         protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
         {
             GridView1.EditIndex = e.NewEditIndex;
         }

         protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
         {
             e.Cancel = true;
             GridView1.EditIndex = -1;
         }

         protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
         {
             GridViewRow row = GridView1.Rows[e.RowIndex];

             TextBox txtSpoor = (TextBox)row.FindControl("txtSpoor");

             e.Cancel = true;
             GridView1.EditIndex = -1;
         }
     } 
}

如何让按钮在 ASP.NET 中工作?

4

2 回答 2

1

我不确定这是否是这里的问题,但您需要在每次页面调用时添加数据,并在操作后进行 DataBind:

   public partial class _Default : System.Web.UI.Page
     {
         List<string> leeg = new List<string>();
         protected void Page_Load(object sender, EventArgs e)
         {
             LoadData();

             if (!Page.IsPostBack)
             {
                 GridView1.DataBind();
             }
         }

         private void LoadData()
         {
             leeg.Add("");
             leeg.Add("");
             leeg.Add("");
             leeg.Add("");
             GridView1.DataSource = leeg;             
         }

         protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
         {
             GridView1.EditIndex = e.NewEditIndex;
             GridView1.DataBind();
         }

         protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
         {
             e.Cancel = true;
             GridView1.EditIndex = -1;
             GridView1.DataBind();
         }

         protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
         {
             GridViewRow row = GridView1.Rows[e.RowIndex];

             TextBox txtSpoor = (TextBox)row.FindControl("txtSpoor");

             e.Cancel = true;
             GridView1.EditIndex = -1;
             GridView1.DataBind();
         }
     } 

试试看你是否解决了你的问题。

于 2012-12-12T12:56:45.237 回答
0

您将不得不在 GridView1_RowEditing 和 GridView1_RowCancelingEdit 上重新绑定数据调用 LoadData()

可以参考http://dotnetdiscussion.wordpress.com/2007/09/26/aspnet-gridview-updateeditcancel-hyperlinkfields-and-datakey-retrieval/

快乐编码!!!

于 2012-12-12T12:54:21.497 回答