Hi,
I'm testing the Grid control --- Grid is displayed, connection to database is working too, but the control for edit, delete and add does not work. i.e. it does not update in the database and the waiting popup keeps running.
Thanks in advance.
Here is the aspx :
<%@ Page Title="" Language="C#" MasterPageFile="~/Site.Master" AutoEventWireup="true" CodeBehind="ManipulateData.aspx.cs" Inherits="ARID.Tests.ManipulateData" %>
<asp:Content ID="Content1" ContentPlaceHolderID="MainContent" runat="server">
<script src="../Scripts/jquery.validate.min.js" type="text/javascript"></script>
<hr>
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<ej:Grid ID="Grid1" runat='server' AllowPaging="True" AllowSorting="True" OnServerEditRow="EditEvents_ServerEditRow" OnServerAddRow="EditEvents_ServerAddRow" OnServerDeleteRow="EditEvents_ServerDeleteRow">
<ClientSideEvents ActionComplete="complete" EndAdd="endAdd" EndDelete="endDelete" EndEdit="endEdit"
RecordDoubleClick="doubleClick" />
<Columns>
<ej:Column Field="TestID" DataType="number" AllowEditing="False" IsIdentity="True"></ej:Column>
<ej:Column Field="TestName" DataType="string"></ej:Column>
</Columns>
<EditSettings AllowEditing="True" AllowAdding="True" AllowDeleting="True"></EditSettings>
<ToolbarSettings ShowToolbar="True" ToolbarItems="add,edit,delete,update,cancel"></ToolbarSettings>
</ej:Grid>
<asp:SqlDataSource runat="server" ID="SqlData" ConnectionString='<%$ ConnectionStrings:DefaultConnection %>' SelectCommand="SELECT * FROM [Test]"></asp:SqlDataSource>
</ContentTemplate>
</asp:UpdatePanel>
<hr />
<script type="text/javascript">
function endAdd(args) {
$('#<%= Grid1.ClientID %>').ejWaitingPopup("show");
}
function endDelete(args) {
$('#<%= Grid1.ClientID %>').ejWaitingPopup("show");
}
function endEdit(args) {
$('#<%= Grid1.ClientID %>').ejWaitingPopup("show");
}
function complete(args) {
if (args.requestType == "refresh") {
$('#<%= Grid1.ClientID %>').ejWaitingPopup("hide");
}
}
</script>
</asp:Content>
Here is the Code Behind:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using Syncfusion.JavaScript.Web;
namespace ARID.Tests
{
public partial class ManipulateData : System.Web.UI.Page
{
DataTable dt = new DataTable("Test");
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
SqlConnection myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["DefaultConnection"].ToString());
dt = new DataTable("Test");
SqlCommand cmd = new SqlCommand();
cmd.Connection = myConnection;
cmd.CommandText = "select * from Test";
cmd.CommandType = CommandType.Text;
SqlDataAdapter da = new SqlDataAdapter();
da.SelectCommand = cmd;
if (myConnection.State == ConnectionState.Closed)
{
myConnection.Open();
}
da.Fill(dt);
Session["SqlDataSource"] = dt;
dataBind();
}
}
protected void dataBind()
{
Grid1.DataSource = (DataTable)Session["SqlDataSource"];
Grid1.DataBind();
}
protected void EditEvents_ServerEditRow(object sender, GridEventArgs e)
{
EditAction(e.EventType, e.Arguments["data"]);
}
protected void EditEvents_ServerAddRow(object sender, GridEventArgs e)
{
EditAction(e.EventType, e.Arguments["data"]);
}
protected void EditEvents_ServerDeleteRow(object sender, GridEventArgs e)
{
EditAction(e.EventType, e.Arguments["data"]);
}
protected void EditAction(string eventType, object record)
{
SqlConnection myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["DefaultConnection"].ToString());
dt = Session["SqlDataSource"] as DataTable;
Dictionary<string, object> KeyVal = record as Dictionary<string, object>;
if (eventType == "endEdit")
{
var Test = KeyVal.Values.ToArray();
foreach (DataRow dr in dt.Rows)
{
if (Convert.ToInt32(dr["TestID"]) == Convert.ToInt32(Test[0]))
{
dr["TestName"] = Test[1];
dr.AcceptChanges();
}
}
}
else if (eventType == "endAdd")
{
var Test = KeyVal.Values.ToArray();
DataRow dr = dt.NewRow();
dr["TestID"] = Test[0];
dr["TestName"] = Test[1];
dt.Rows.Add(dr);
}
else if (eventType == "endDelete")
{
var Test = KeyVal.Values.ToArray();
if (Session["SqlDataSource"] != null)
{
DataRow[] rows = dt.Select("TestID = " + Test[0]);
foreach (DataRow row in rows)
dt.Rows.Remove(row);
}
}
Session["SqlDataSource"] = dt;
dataBind();
}
}
}