SAP.NET GridView CommandEventArgs Question

EDN Admin

Well-known member
Joined
Aug 7, 2010
Messages
12,794
Location
In the Machine
I have a GridView on my ASP.NET to display/edit/insert/delete the data in a database table. The problem is that after I insert a new record, if I refresh the page directly it will post back the same insert transaction again. Please let me know how to solve this problem. Thank you. I have the gridview defination and insert code as following. protected void gdvActivityTypes_RowCommand(object sender, GridViewCommandEventArgs e)
{
try
{
if (e.CommandName == "InsertNew")
{
GridViewRow row = gdvActivityTypes.FooterRow;
//TextBox tbxPartnerId = (TextBox)row.FindControl("tbxActPartnerInsert") as TextBox;
int pid = (int)Session["PartnerID"];
DropDownList ddlActType = (DropDownList)row.FindControl("ddlActTypeInsert") as DropDownList;
string actTypeSym = ddlActType.SelectedValue;
using (service_bus_databaseEntities1 db = new service_bus_databaseEntities1())
{
var actTypeQuery = from act in db.act_types
where act.sym == actTypeSym
select act.code;
Sync_Activity_Type syncActType = new Sync_Activity_Type();
syncActType.partner_id = pid;
syncActType.act_type = actTypeQuery.First();
db.Sync_Activity_Types.AddObject(syncActType);
db.SaveChanges();
}
PopulateActType(pid);
}
}
catch (Exception ex)
{
throw new Exception("gdvActivityTypes_RowCommand Error: " + ex.Message, ex.InnerException);
}
} <asp:GridView ID="gdvActivityTypes" runat="server" ShowFooter="True" AutoGenerateColumns="False"
CellPadding="2" BorderColor="#666666" BorderStyle="Solid" OnRowDataBound="gdvActivityTypes_RowDataBound"
OnRowEditing="gdvActivityTypes_RowEditing" OnRowCancelingEdit="gdvActivityTypes_RowCancelingEdit"
OnRowCommand="gdvActivityTypes_RowCommand" OnRowUpdating="gdvActivityTypes_RowUpdating"
OnRowDeleting="gdvActivityTypes_RowDeleting" BorderWidth="3px" Visible="False
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:LinkButton ID="lnkEditAct" runat="server" Text="" CommandName="Edit" ToolTip="Edit"
CommandArgument=>
<img src="../images/edit-icon.png" alt="Edit" height="20px" width="20px" style="border: thin ridge #C0C0C0" />
<asp:LinkButton ID="lnkDeleteAct" runat="server" Text="Delete" CommandName="Delete"
ToolTip="Delete" OnClientClick=return confirm("Are you sure you want to delete this entry?");
CommandArgument=><img src="../images/edit-delete.png" alt="Delete" height="20px" width="20px" style="border: thin ridge #C0C0C0" />
</asp:LinkButton>
</asp:LinkButton>
</ItemTemplate>
<EditItemTemplate>
<asp:LinkButton ID="lnkUpdateAct" runat="server" Text="Update" ToolTip="Save" CommandName="Update"
ValidationGroup="vlgAct" OnClientClick=return confirm("Are you sure you want to save the changes of this entry?");
CommandArgument=>
<img src="../images/Save-icon.png" alt="Save" height="20px" width="20px" style="border: thin ridge #C0C0C0" />
</asp:LinkButton>
<asp:LinkButton ID="lnkCancelAct" runat="server" Text="Cancel" CommandName="Cancel"
ToolTip="Cancel" CommandArgument=>
<img src="../images/Cancel-icon.png" alt="Cancel" height="20px" width="20px" style="border: thin ridge #C0C0C0" />
</asp:LinkButton>
</EditItemTemplate>
<FooterTemplate>
<asp:LinkButton ID="lnkInsertAct" runat="server" Text="" ValidationGroup="vlgExceptionAct"
CommandName="InsertNew" ToolTip="Add" CommandArgument= EnableViewState="False <img src="../images/add_icon.png" alt="Add" height="20px" width="20px" style="border: thin ridge #C0C0C0" />
</asp:LinkButton>
</FooterTemplate>
<ItemStyle Wrap="False </ItemStyle>
<FooterStyle Wrap="false" HorizontalAlign="Center" />
</asp:TemplateField>
<asp:TemplateField HeaderText="ID" Visible="False
<ItemTemplate>
<asp:Label ID="lblActId" runat="server" Text=<%# Bind("id") %>></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:Label ID="lblActIdEdit" runat="server" Text=<%# Bind("id") %>></asp:Label>
</EditItemTemplate>
<FooterTemplate>
<asp:Label ID="lblActIdInsert" runat="server" Text=" </asp:Label>
</FooterTemplate>
<ItemStyle Wrap="False </ItemStyle>
<FooterStyle Wrap="false" />
</asp:TemplateField>
<asp:TemplateField HeaderText="Partner ID
<ItemTemplate>
<asp:Label ID="lblActPartner" runat="server" Text=<%# Bind("partner_id") %>></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:Label ID="lblActPartnerEdit" runat="server" Text=<%# Bind("partner_id") %>></asp:Label>
</EditItemTemplate>
<FooterTemplate>
<asp:Label ID="lblActPartnerInsert" runat="server" Text=" </asp:Label>
</FooterTemplate>
<ItemStyle Wrap="False" HorizontalAlign="Center </ItemStyle>
<FooterStyle Wrap="false" />
</asp:TemplateField>
<asp:TemplateField HeaderText="Activity Type
<ItemTemplate>
<asp:Label ID="lblAct" runat="server" Text=<%# Bind("act_type") %>></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:DropDownList ID="ddlActType" runat="server
</asp:DropDownList>
<asp:RequiredFieldValidator ID="valActType" runat="server" ControlToValidate="ddlActType"
Display="Dynamic" ErrorMessage="Please select a activity type" ForeColor="Red"
SetFocusOnError="True" ValidationGroup="vlgActEdit" InitialValue="-1 *</asp:RequiredFieldValidator>
</EditItemTemplate>
<FooterTemplate>
<asp:DropDownList ID="ddlActTypeInsert" runat="server
</asp:DropDownList>
<asp:RequiredFieldValidator ID="valActTypeInsert" runat="server" ControlToValidate="ddlActTypeInsert"
Display="Dynamic" ErrorMessage="Please select a activity type" ForeColor="Red"
SetFocusOnError="True" ValidationGroup="vlgActInsert" InitialValue="-1 *</asp:RequiredFieldValidator>
</FooterTemplate>
<ItemStyle Wrap="False </ItemStyle>
<FooterStyle Wrap="false" />
</asp:TemplateField>
</Columns>
<AlternatingRowStyle BackColor="Silver" />
<HeaderStyle BackColor="#4D78BF" ForeColor="White" HorizontalAlign="Center" VerticalAlign="Middle" />
<EditRowStyle BackColor="#2461BF" />
<FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
<RowStyle BackColor="#EFF3FB" />
<SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
<SortedAscendingCellStyle BackColor="#F5F7FB" />
<SortedAscendingHeaderStyle BackColor="#6D95E1" />
<SortedDescendingCellStyle BackColor="#E9EBEF" />
<SortedDescendingHeaderStyle BackColor="#4870BE" />
</asp:GridView>

View the full article
 
Back
Top