I am inserting data in asp .net grid on RowCommand event it is inserting data but when I am refreshing page using f5 or(selecting url and then press enter) same record get inserted again
What I have tried:
I have Asp Grid view as follows
<asp:GridView ID="GridViewRow" runat="server" AutoGenerateColumns="False" BackColor="White" BorderColor="#999999" BorderStyle="None" BorderWidth="1px"
CellPadding="3" DataKeyNames="Id" GridLines="Vertical" ShowFooter="true"
OnRowCommand="GridViewRow_RowCommand" OnRowEditing="GridViewRow_RowEditing" OnRowCancelingEdit="GridViewRow_RowCancelingEdit"
OnRowUpdating="GridViewRow_RowUpdating" OnRowDeleting="GridViewRow_RowDeleting">
<AlternatingRowStyle BackColor="#DCDCDC" />
<Columns>
<asp:TemplateField HeaderText="Id" InsertVisible="False" SortExpression="Id">
<EditItemTemplate>
<asp:Label ID="lblId" runat="server" Text='<%# Eval("Id") %>'></asp:Label>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Bind("Id") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="FirstName" SortExpression="FirstName">
<EditItemTemplate>
<asp:TextBox ID="txtFirstName" runat="server" Text='<%# Bind("FirstName") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="lblFirstName" runat="server" Text='<%# Bind("FirstName") %>'></asp:Label>
</ItemTemplate>
<FooterTemplate>
<asp:TextBox ID="txtFirstNameFooter" runat="server"></asp:TextBox>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="LastName" SortExpression="LastName">
<EditItemTemplate>
<asp:TextBox ID="txtLastName" runat="server" Text='<%# Bind("LastName") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="lblLastName" runat="server" Text='<%# Bind("LastName") %>'></asp:Label>
</ItemTemplate>
<FooterTemplate>
<asp:TextBox runat="server" ID="txtLastNameFooter"></asp:TextBox>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Contact" SortExpression="Contact">
<EditItemTemplate>
<asp:TextBox ID="txtContact" runat="server" Text='<%# Bind("Contact") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="lblContact" runat="server" Text='<%# Bind("Contact") %>'></asp:Label>
</ItemTemplate>
<FooterTemplate>
<asp:TextBox ID="txtContactFooter" runat="server"></asp:TextBox>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<asp:Button ID="btnEdit" runat="server" Text="Edit" CommandName="Edit"/>
<asp:Button ID="btnDelete" runat="server" Text="Delete" CommandName="Delete"/>
</ItemTemplate>
<EditItemTemplate>
<asp:Button ID="btnUpdate" runat="server" Text="Update" CommandName="Update" />
<asp:Button ID="btnCancel" runat="server" Text="Cancel" CommandName="Cancel"/>
</EditItemTemplate>
<FooterTemplate>
<asp:Button ID="btnAddNew" runat="server" CommandName="AddNew" Text="Add New"/>
</FooterTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
in footeer template I have button to insert data
<FooterTemplate>
<asp:Button ID="btnAddNew" runat="server" CommandName="AddNew" Text="Add New"/>
</FooterTemplate>
When I inserting Add New button It is inserting text box data into Database
but when i refresh after inserting (suppose i am pressing f5 then It is again inserting same data)
button insert code is as follows (written on
RowCommand
event) in which i have checkd command name as
if (e.CommandName.Equals("AddNew"))
entire code to insert data into grid view as follows
protected void GridViewRow_RowCommand(object sender, GridViewCommandEventArgs e)
{
try
{
if (e.CommandName.Equals("AddNew"))
{
using (SqlConnection con = new SqlConnection(strConStr))
{
SqlCommand cmd = new SqlCommand("SP_InsertPhoneBook", con);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@FirstName", (GridViewRow.FooterRow.FindControl("txtFirstNameFooter") as TextBox).Text.Trim());
cmd.Parameters.AddWithValue("@LastName", (GridViewRow.FooterRow.FindControl("txtLastNameFooter") as TextBox).Text.Trim());
cmd.Parameters.AddWithValue("@Contact", (GridViewRow.FooterRow.FindControl("txtContactFooter") as TextBox).Text.Trim());
cmd.Parameters.AddWithValue("@Email", (GridViewRow.FooterRow.FindControl("txtEmailFooter") as TextBox).Text.Trim());
con.Open();
cmd.ExecuteNonQuery();
}
}
}
catch (Exception ex)
{
string str=ex.Message.ToString();
}
finally
{
populateGrid();
}
}
but when I refreshing page using f5 after inserting record then
GridViewRow_RowCommand
even is firing and it is inserting what ever i have insert previous data
how I can prevent this