gridview doesn't return to normal after cancelling an edit operation

dhenderson12
dhenderson12 used Ask the Experts™
on
it appears that the gridview control will not return to "normal/view" mode when "cancel" button is clicked.  I have a label for view and a textbox for edit defined in the item template:

<asp:TemplateField HeaderText="First Name">
<ItemTemplate>
<asp:Label ID=lblFirstName runat="server" Text='<%# Bind("FirstName") %>' ></asp:Label>
</ItemTemplate>

<EditItemTemplate>
<asp:TextBox ID="txtFirstName" runat="server" Text='<%# Bind("FirstName") %>' />
</EditItemTemplate>
                                                                        
<FooterTemplate>
<asp:TextBox ID="txtFirstName" runat="server" />
</FooterTemplate>

</asp:TemplateField>

The event for cancel operation is:
protected void gvContacts_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
                  gvContacts.EditIndex = -1;
                  gvContacts.DataBind();
}


The event fires, but the gridview still has the textbox visible, rather than the label.
Any ideas?
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
You don't need to call the DataBind methods of gridview.
Instead you need to call the function which is populating the data in gridview

gvContacts.EditIndex = -1;
DataBindMethod();

Author

Commented:
thanks for the reply.  Nope, that didn't work either.  I replaced your databindmethod() with the call to the actual method and that didn't change the gridview back to "normal" view.
Can you post your cs file code?
Microsoft Azure 2017

Azure has a changed a lot since it was originally introduce by adding new services and features. Do you know everything you need to about Azure? This course will teach you about the Azure App Service, monitoring and application insights, DevOps, and Team Services.

Author

Commented:
here it is:

the pre-render function for the page loads the gridview:

        
protected override void OnPreRender(EventArgs e)
{
            base.OnPreRender(e);

            long CompanyID = long.Parse(Request.QueryString["id"]);
            PopulateContacts(CompanyID);
}

Open in new window


here is the function to populate grid:
private void PopulateContacts(long CompanyID)
{

gvContacts.DataSource = 
GetDataSetFromCache(string.Format("AdditionalContacts_{0}", CompanyID), .25, 
"DataConnection", "Contact_Get", inParams);
gvContacts.DataBind();

}

Open in new window


call to set the edit mode:

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

Open in new window


call to cancel edit mode:

protected void gvContacts_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
	gvContacts.EditIndex = -1;
	//PopulateContacts(CompanyID); 

[b]//I have commented the function call to bind, since this happens at pre-render, but it //doesn't matter because even with it un-commented the mode still won't change.[/b]
}

Open in new window

PreRender is not the best event to bind a grid.
Try to use the page_load event with IsPostback=False

Author

Commented:
I followed your suggestion: I placed the pre-render code in the If (! IsPostBack) section, commented out the pre-render code, and called the method to load the gridview control but it didn't make a difference.

It just won't come out of edit mode ...
Let me put the same code in my page, will tell you the result.
OK, I found the problem posted on another site (thanks to user356978 at stackoverflow.com).  Add the bolded line and all will work just fine:

protected void gvContacts_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
      gvContacts.Rows[e.RowIndex].RowState = DataControlRowState.Normal;
      gvContacts.EditIndex = -1;
      long CompanyID = long.Parse(Request.QueryString["id"]);
      PopulateContacts(CompanyID);
                  
}

Author

Commented:
I'm the one that found the answer.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial