Button does not submit on first click, but on subsequent click submits previous information

允我心安 提交于 2019-12-12 22:44:02

问题


I am making an editable GridView, but my problem is that whenever I click on a button nothing happens. When I click a second time I see what happened during the previous click.

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>

Codebehind

 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;
         }
     } 
}

How can I get the button to work in ASP.NET ?


回答1:


I am not sure if this is the issue here, but you need to add your data on every page call, and make DataBind after the actions as:

   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();
         }
     } 

Try that to see if you solve your issue.




回答2:


You will have to rebind the data call LoadData() both on GridView1_RowEditing & GridView1_RowCancelingEdit

You can refer to http://dotnetdiscussion.wordpress.com/2007/09/26/aspnet-gridview-updateeditcancel-hyperlinkfields-and-datakey-retrieval/

Happy Coding!!!



来源:https://stackoverflow.com/questions/13839934/button-does-not-submit-on-first-click-but-on-subsequent-click-submits-previous

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!