Hi,
I have gridview in my module and applying edit and delete functionality manually instead of using datasource. But when i click on the edit button in gridview its give me this error. when i turn of enableEventValidation in config file false or add EnableEventValidation in page its gave me an other error. what i have to rid off this error.
A critical error has occurred. Invalid postback or callback argument. Event validation is enabled using <pages enableEventValidation="true"/> in configuration or <%@ Page EnableEventValidation="true" %> in a page. For security purposes, this feature verifies that arguments to postback or callback events originate from the server control that originally rendered them. If the data is valid and expected, use the ClientScriptManager.RegisterForEventValidation method in order to register the postback or callback data for validation.
Here is my ascx code
<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="Location.ascx.cs" Inherits="KontorBokning.Location"%>
<asp:Button ID="btnCreate" runat="server" Text="Create"
onclick="btnCreate_Click" /><br />
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
DataKeyNames="ID" onrowupdating="GridView1_RowUpdating"
onrowediting="GridView1_RowEditing">
<Columns>
<asp:BoundField DataField="ID" HeaderText="ID" InsertVisible="False"
ReadOnly="True" SortExpression="ID" Visible="False" />
<asp:BoundField DataField="Location" HeaderText="Location"
SortExpression="Location" />
<asp:BoundField DataField="CreateDate" HeaderText="CreateDate"
SortExpression="CreateDate" Visible="False" />
<asp:BoundField DataField="UpdateDate" HeaderText="UpdateDate"
SortExpression="UpdateDate" Visible="False" />
<asp:BoundField DataField="SystemUser" HeaderText="SystemUser"
SortExpression="SystemUser" Visible="False" />
<asp:TemplateField HeaderText="Editera" ShowHeader="False">
<EditItemTemplate>
<asp:ImageButton ID="btnUpdate" runat="server" CausesValidation="True"
CommandName="Update" ImageUrl="Images/save.png" Text="Update" />
<asp:ImageButton ID="btnCancel" runat="server" CausesValidation="False"
CommandName="Cancel" ImageUrl="Images/cancel.png" Text="Cancel" />
</EditItemTemplate>
<ItemTemplate>
<asp:ImageButton ID="ImageButton1" runat="server" CausesValidation="False"
CommandName="Edit" ImageUrl="Images/update.png" Text="Edit" />
</ItemTemplate>
<ItemStyle HorizontalAlign="Center" />
</asp:TemplateField>
<asp:TemplateField HeaderText="Ta bort" ShowHeader="False">
<ItemTemplate>
<asp:ImageButton ID="ImageButton2" runat="server" CausesValidation="False"
CommandName="Delete" ImageUrl="Images/delete.png" Text="Delete" />
</ItemTemplate>
<ItemStyle HorizontalAlign="Center" />
</asp:TemplateField>
</Columns>
</asp:GridView>
Here is my code behind file code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DotNetNuke;
using DotNetNuke.Common.Utilities;
using DotNetNuke.Security;
using DotNetNuke.Services.Exceptions;
using DotNetNuke.Services.Localization;
using DotNetNuke.Entities.Modules;
using DotNetNuke.Entities.Modules.Actions;
using System.Data;
using System.Data.OleDb;
namespace KontorBokning
{
public partial class Location : PortalModuleBase, IActionable
{
protected void Page_Load(object sender, EventArgs e)
{
LoadLocations();
}
#region "Optional Interfaces"
/// -----------------------------------------------------------------------------
/// <summary>
/// Registers the module actions required for interfacing with the portal framework
/// </summary>
/// <value></value>
/// <returns></returns>
/// <remarks></remarks>
/// <history>
/// </history>
/// -----------------------------------------------------------------------------
public ModuleActionCollection ModuleActions
{
get
{
ModuleActionCollection Actions = new ModuleActionCollection();
Actions.Add(GetNextActionID(), Localization.GetString(ModuleActionType.AddContent, this.LocalResourceFile),
ModuleActionType.AddContent, "", "add.gif", EditUrl(), false, DotNetNuke.Security.SecurityAccessLevel.Edit,
true, false);
return Actions;
}
}
#endregion
public void LoadLocations() {
Connection Conn = new Connection();
Conn.OledbConntion();
Conn.OpenOledbConnection();
string Qry = "SELECT * FROM [KontorLocation]";
System.Data.OleDb.OleDbCommand cmd = new OleDbCommand(Qry, Conn.oledbconn);
System.Data.OleDb.OleDbDataAdapter da = new OleDbDataAdapter(cmd);
System.Data.DataTable dt = new DataTable("Location");
da.Fill(dt);
GridView1.DataSource = dt;
GridView1.DataBind();
Conn.CloseOledbConnection();
}
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
Connection Conn = new Connection();
Conn.OledbConntion();
Conn.OpenOledbConnection();
string Qry="", Location="";
if (this.ViewState["Control"] == "Insert") {
Qry = "Insert Into [KontorLocation] ([Location],[CreateDate],[UpdateDate],[SystemUser]) ";
Qry += "Values('" +Location+"', '"+ DateTime.Now.ToShortDateString() +"', '"+ DateTime.Now.ToShortDateString()+"', '"+ UserInfo.Username.ToString() +"'";
System.Data.OleDb.OleDbCommand cmd = new OleDbCommand(Qry, Conn.oledbconn);
System.Data.OleDb.OleDbDataAdapter da = new OleDbDataAdapter(cmd);
System.Data.DataTable dt = new DataTable("New Location");
da.Fill(dt);
this.ViewState["Control"] = "Update";
}
else {
Qry = "Update [KontorLocation] Set [Location] = '" + Location + "', [UpdateDate] = '" + DateTime.Now.ToShortDateString() + "', [SystemUser] = '" + UserInfo.Username.ToString() + "' Where ID= " + GridView1.DataKeys[Convert.ToInt32(e.RowIndex)]["ID"].ToString() + "";
System.Data.OleDb.OleDbCommand cmd = new OleDbCommand(Qry, Conn.oledbconn);
System.Data.OleDb.OleDbDataAdapter da = new OleDbDataAdapter(cmd);
System.Data.DataTable dt = new DataTable("Update Location");
da.Fill(dt);
}
Conn.CloseOledbConnection();
}
protected void btnCreate_Click(object sender, EventArgs e)
{
Connection Conn = new Connection();
Conn.OledbConntion();
Conn.OpenOledbConnection();
string Qry = "SELECT * FROM [KontorLocation]";
System.Data.OleDb.OleDbCommand cmd = new OleDbCommand(Qry,Conn.oledbconn);
System.Data.OleDb.OleDbDataAdapter da = new OleDbDataAdapter(cmd);
System.Data.DataTable dt = new DataTable("Location");
da.Fill(dt);
DataRow Row = dt.NewRow();
dt.Rows.InsertAt(Row, 0);
GridView1.EditIndex = 0;
GridView1.DataSource = dt;
GridView1.DataBind();
this.ViewState["Control"] = "Insert";
Conn.CloseOledbConnection();
}
protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{
GridView1.EditIndex = e.NewEditIndex;
LoadLocations();
}
}
}