按钮不会在第一次单击时提交,但在后续单击时会提交以前的信息

时间:2012-12-12 12:35:44

标签: c# asp.net html double-click

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

2 个答案:

答案 0 :(得分: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();
         }
     } 

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

答案 1 :(得分:0)

您必须在GridView1_RowEditing&amp;上重新绑定数据调用LoadData()。 GridView1_RowCancelingEdit

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

快乐编码!!!