I have a Gridview and the OnRowDeleting method with removes a record from the database and the gridview.
My problem is that when a user click the delete button in the Gridview the record will be removed from the database but the GridView does not show this update (does not refresh). If the user then refreshes the page causing a form re-submission then the GridView deletes the row below the originally deleted row and so on if the page is refreshed again.
How can I prevent this from happening?
Here is my gridview:
<asp:GridView runat="server" ID="gvShowQuestionnaires" HeaderStyle-CssClass="table_header" CssClass="view" AlternatingRowStyle-CssClass="alt" AlternatingRowStyle-BackColor="#f3f4f8" AutoGenerateColumns="False"
DataKeyNames='QuestionnaireID' OnRowDeleting="gvShowQuestionnaires_RowDeleting" OnRowEditing="gvShowQuestionnaires_RowEdit" OnSelectedIndexChanged="gvShowQuestionnaires_SelectedIndexChanged" FooterStyle-CssClass="view_table_footer" >
<Columns>
<asp:BoundField DataField="QuestionnaireID" HeaderText="ID" HeaderStyle-Width="80px" ItemStyle-CssClass="bo"></asp:BoundField>
<asp:BoundField DataField="QuestionnaireName" HeaderText="Questionnaire Name" />
<asp:ButtonField CommandName="select" ButtonType="Link" Text="hello" />
<asp:CommandField HeaderText="Options" CausesValidation="true" ShowDeleteButton="True" ShowEditButton="true" EditText="Edit">
</asp:CommandField>
</Columns>
</asp:GridView>
Here is the OnRowDeleting method.
protected void gvShowQuestionnaires_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
int questionnaireID = (int)gvShowQuestionnaires.DataKeys[Convert.ToInt32(e.RowIndex)].Value;
GetData.DeleteQuestionnaire(questionnaireID); // Deletes Questionniare from database
gvShowQuestionnaires.DataSource = DT;
gvShowQuestionnaires.DataBind();
}
You could always redirect the user to the initial page after the row was deleted, e.g. in the RowDeleted handler.
This should work because the delete action is a POST command to the current .aspx page.