我正在制作一个可编辑的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 中工作?