DataGrid not responding to Update/Cancel event

A

andrew.roberts

This is my second post now on this subject has I seem to be getting no
where and the problem is really starting to bug me now. I can get a
datagrid into an editable state but then neither the Update or Cancel
events seem to be firing and I am at a lost to explan why. My code is
below, if any one can tell me what I am doing wrong then that would be
excellent!

using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;

namespace DataGrid2
{
/// <summary>
/// Summary description for WebForm1.
/// </summary>
public class WebForm1 : System.Web.UI.Page
{
protected System.Web.UI.WebControls.DataGrid DataGrid1;
protected System.Data.SqlClient.SqlConnection con;
protected System.Data.SqlClient.SqlCommand cmd;
protected System.Data.SqlClient.SqlDataReader rdr;
protected System.Web.UI.WebControls.Label lbloutput;
protected System.Data.DataTable tbl;

private void Page_Load(object sender, System.EventArgs e)
{
// Put user code to initialize the page here
}

private void onPageOpen()
{
dbc();
createTable();
getData();
this.DataGrid1.Visible = true;
}

private void dbc()
{
this.con = new SqlConnection("Data Source = *;Initial Catalog =
PLDWH;user id = *;password = *");
}

private void createTable()
{
tbl = new DataTable();
tbl.Columns.Add(new DataColumn("ID",
System.Type.GetType("System.Int32")));
tbl.Columns.Add(new DataColumn("SKU",
System.Type.GetType("System.Int32")));
tbl.Columns.Add(new DataColumn("Description", typeof(string)));
tbl.Columns.Add(new DataColumn("Grade", typeof(string)));
tbl.Columns.Add(new DataColumn("Status", typeof(string)));
}

private void getData()
{
DataRow row;
int i = 1;

try
{
this.con.Open();
this.cmd = new SqlCommand("select top 10 productnumber,
productdescription, productgrade, productstatus from
productdetailscurrent", this.con);
this.cmd.CommandType = CommandType.Text;
this.rdr = this.cmd.ExecuteReader();

while (this.rdr.Read())
{
row = this.tbl.NewRow();
row[0] = i;
row[1] = this.rdr.GetInt32(0);
row[2] = this.rdr.GetString(1);
row[3] = this.rdr.GetString(2);
row[4] = this.rdr.GetString(3);
this.tbl.Rows.Add(row);

i++;
}

this.rdr.Close();
this.cmd.Dispose();
Cache.Insert("table", this.tbl);
bindGrid();
}
catch (SqlException sqlex)
{
this.lbloutput.Text = sqlex.ToString();
}
finally
{
this.con.Close();
}
}

private void bindGrid()
{
this.DataGrid1.DataSource = (DataTable)Cache["table"];
this.DataGrid1.DataBind();
}

#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: This call is required by the ASP.NET Web Form Designer.
//
InitializeComponent();
base.OnInit(e);
}

/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// onPageOpen();
/// </summary>
private void InitializeComponent()
{
this.DataGrid1.CancelCommand += new
System.Web.UI.WebControls.DataGridCommandEventHandler(this.DataGrid1_CancelCommand);
this.DataGrid1.EditCommand += new
System.Web.UI.WebControls.DataGridCommandEventHandler(this.DataGrid1_EditCommand);
this.DataGrid1.UpdateCommand += new
System.Web.UI.WebControls.DataGridCommandEventHandler(this.DataGrid1_UpdateCommand);
this.Load += new System.EventHandler(this.Page_Load);
onPageOpen();
}
#endregion

private void DataGrid1_EditCommand(object source,
System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
this.DataGrid1.EditItemIndex = (int)e.Item.ItemIndex;
bindGrid();
this.lbloutput.Text = "Edit";
}

private void DataGrid1_CancelCommand(object source,
System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
this.DataGrid1.EditItemIndex = -1;
this.bindGrid();
this.lbloutput.Text = "Cancel";
}

private void DataGrid1_UpdateCommand(object source,
System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
this.DataGrid1.EditItemIndex = -1;
this.bindGrid();
this.lbloutput.Text = "Update";

}
}
}

<%@ Page language="c#" Codebehind="test.aspx.cs"
AutoEventWireup="false" Inherits="DataGrid2.WebForm1" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<HEAD>
<title>Test</title>
<meta content="Microsoft Visual Studio .NET 7.1" name="GENERATOR">
<meta content="C#" name="CODE_LANGUAGE">
<meta content="JavaScript" name="vs_defaultClientScript">
<meta content="http://schemas.microsoft.com/intellisense/ie5"
name="vs_targetSchema">
</HEAD>
<body MS_POSITIONING="GridLayout">
<form id="Form1" method="post" runat="server">
<asp:datagrid id="DataGrid1" style="Z-INDEX: 101; LEFT: 32px;
POSITION: absolute; TOP: 24px" runat="server"
AutoGenerateColumns="False" DataKeyField="ID" Visible="False">
<Columns>
<asp:BoundColumn DataField="ID" ReadOnly="True"
HeaderText="ID"></asp:BoundColumn>
<asp:BoundColumn DataField="SKU" ReadOnly="True"
HeaderText="SKU"></asp:BoundColumn>
<asp:TemplateColumn HeaderText="Description">
<ItemTemplate>
<%# DataBinder.Eval(Container.DataItem, "Description") %>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox Runat=server ID="Description" Text='<%#
DataBinder.Eval(Container.DataItem, "Description") %>' />
</EditItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn HeaderText="Grade">
<ItemTemplate>
<%# DataBinder.Eval(Container.DataItem, "Grade") %>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox Runat=server ID="Textbox1" Text='<%#
DataBinder.Eval(Container.DataItem, "Grade") %>' />
</EditItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn HeaderText="Status">
<ItemTemplate>
<%# DataBinder.Eval(Container.DataItem, "Status") %>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox Runat=server ID="Textbox2" Text='<%#
DataBinder.Eval(Container.DataItem, "Status") %>' />
</EditItemTemplate>
</asp:TemplateColumn>
<asp:EditCommandColumn ButtonType="LinkButton" UpdateText="Update"
CancelText="Cancel" EditText="Edit"></asp:EditCommandColumn>
</Columns>
</asp:datagrid>
<asp:Label id="lbloutput" style="Z-INDEX: 102; LEFT: 24px; POSITION:
absolute; TOP: 496px"
runat="server" Width="456px" Height="160px"></asp:Label></form>
</body>
</HTML>
 
G

Guest

Hi Andrew,

Put

onPageOpen();

in Page_Load rather than in InitializeComponent.

And it's better only query DB in first load, like

private void Page_Load(object sender, System.EventArgs e)
{
if (!this.IsPostBack)
{
onPageOpen();
}
}


HTH

Elton Wang



This is my second post now on this subject has I seem to be getting no
where and the problem is really starting to bug me now. I can get a
datagrid into an editable state but then neither the Update or Cancel
events seem to be firing and I am at a lost to explan why. My code is
below, if any one can tell me what I am doing wrong then that would be
excellent!

using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;

namespace DataGrid2
{
/// <summary>
/// Summary description for WebForm1.
/// </summary>
public class WebForm1 : System.Web.UI.Page
{
protected System.Web.UI.WebControls.DataGrid DataGrid1;
protected System.Data.SqlClient.SqlConnection con;
protected System.Data.SqlClient.SqlCommand cmd;
protected System.Data.SqlClient.SqlDataReader rdr;
protected System.Web.UI.WebControls.Label lbloutput;
protected System.Data.DataTable tbl;

private void Page_Load(object sender, System.EventArgs e)
{
// Put user code to initialize the page here
}

private void onPageOpen()
{
dbc();
createTable();
getData();
this.DataGrid1.Visible = true;
}

private void dbc()
{
this.con = new SqlConnection("Data Source = *;Initial Catalog =
PLDWH;user id = *;password = *");
}

private void createTable()
{
tbl = new DataTable();
tbl.Columns.Add(new DataColumn("ID",
System.Type.GetType("System.Int32")));
tbl.Columns.Add(new DataColumn("SKU",
System.Type.GetType("System.Int32")));
tbl.Columns.Add(new DataColumn("Description", typeof(string)));
tbl.Columns.Add(new DataColumn("Grade", typeof(string)));
tbl.Columns.Add(new DataColumn("Status", typeof(string)));
}

private void getData()
{
DataRow row;
int i = 1;

try
{
this.con.Open();
this.cmd = new SqlCommand("select top 10 productnumber,
productdescription, productgrade, productstatus from
productdetailscurrent", this.con);
this.cmd.CommandType = CommandType.Text;
this.rdr = this.cmd.ExecuteReader();

while (this.rdr.Read())
{
row = this.tbl.NewRow();
row[0] = i;
row[1] = this.rdr.GetInt32(0);
row[2] = this.rdr.GetString(1);
row[3] = this.rdr.GetString(2);
row[4] = this.rdr.GetString(3);
this.tbl.Rows.Add(row);

i++;
}

this.rdr.Close();
this.cmd.Dispose();
Cache.Insert("table", this.tbl);
bindGrid();
}
catch (SqlException sqlex)
{
this.lbloutput.Text = sqlex.ToString();
}
finally
{
this.con.Close();
}
}

private void bindGrid()
{
this.DataGrid1.DataSource = (DataTable)Cache["table"];
this.DataGrid1.DataBind();
}

#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: This call is required by the ASP.NET Web Form Designer.
//
InitializeComponent();
base.OnInit(e);
}

/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// onPageOpen();
/// </summary>
private void InitializeComponent()
{
this.DataGrid1.CancelCommand += new
System.Web.UI.WebControls.DataGridCommandEventHandler(this.DataGrid1_CancelCommand);
this.DataGrid1.EditCommand += new
System.Web.UI.WebControls.DataGridCommandEventHandler(this.DataGrid1_EditCommand);
this.DataGrid1.UpdateCommand += new
System.Web.UI.WebControls.DataGridCommandEventHandler(this.DataGrid1_UpdateCommand);
this.Load += new System.EventHandler(this.Page_Load);
onPageOpen();
}
#endregion

private void DataGrid1_EditCommand(object source,
System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
this.DataGrid1.EditItemIndex = (int)e.Item.ItemIndex;
bindGrid();
this.lbloutput.Text = "Edit";
}

private void DataGrid1_CancelCommand(object source,
System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
this.DataGrid1.EditItemIndex = -1;
this.bindGrid();
this.lbloutput.Text = "Cancel";
}

private void DataGrid1_UpdateCommand(object source,
System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
this.DataGrid1.EditItemIndex = -1;
this.bindGrid();
this.lbloutput.Text = "Update";

}
}
}

<%@ Page language="c#" Codebehind="test.aspx.cs"
AutoEventWireup="false" Inherits="DataGrid2.WebForm1" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<HEAD>
<title>Test</title>
<meta content="Microsoft Visual Studio .NET 7.1" name="GENERATOR">
<meta content="C#" name="CODE_LANGUAGE">
<meta content="JavaScript" name="vs_defaultClientScript">
<meta content="http://schemas.microsoft.com/intellisense/ie5"
name="vs_targetSchema">
</HEAD>
<body MS_POSITIONING="GridLayout">
<form id="Form1" method="post" runat="server">
<asp:datagrid id="DataGrid1" style="Z-INDEX: 101; LEFT: 32px;
POSITION: absolute; TOP: 24px" runat="server"
AutoGenerateColumns="False" DataKeyField="ID" Visible="False">
<Columns>
<asp:BoundColumn DataField="ID" ReadOnly="True"
HeaderText="ID"></asp:BoundColumn>
<asp:BoundColumn DataField="SKU" ReadOnly="True"
HeaderText="SKU"></asp:BoundColumn>
<asp:TemplateColumn HeaderText="Description">
<ItemTemplate>
<%# DataBinder.Eval(Container.DataItem, "Description") %>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox Runat=server ID="Description" Text='<%#
DataBinder.Eval(Container.DataItem, "Description") %>' />
</EditItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn HeaderText="Grade">
<ItemTemplate>
<%# DataBinder.Eval(Container.DataItem, "Grade") %>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox Runat=server ID="Textbox1" Text='<%#
DataBinder.Eval(Container.DataItem, "Grade") %>' />
</EditItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn HeaderText="Status">
<ItemTemplate>
<%# DataBinder.Eval(Container.DataItem, "Status") %>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox Runat=server ID="Textbox2" Text='<%#
DataBinder.Eval(Container.DataItem, "Status") %>' />
</EditItemTemplate>
</asp:TemplateColumn>
<asp:EditCommandColumn ButtonType="LinkButton" UpdateText="Update"
CancelText="Cancel" EditText="Edit"></asp:EditCommandColumn>
</Columns>
</asp:datagrid>
<asp:Label id="lbloutput" style="Z-INDEX: 102; LEFT: 24px; POSITION:
absolute; TOP: 496px"
runat="server" Width="456px" Height="160px"></asp:Label></form>
</body>
</HTML>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top