Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > DataGrid not responding to Update/Cancel event

Reply
Thread Tools

DataGrid not responding to Update/Cancel event

 
 
andrew.roberts@poundland.co.uk
Guest
Posts: n/a
 
      02-03-2006
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.DataGridCommandEventHand ler(this.DataGrid1_CancelCommand);
this.DataGrid1.EditCommand += new
System.Web.UI.WebControls.DataGridCommandEventHand ler(this.DataGrid1_EditCommand);
this.DataGrid1.UpdateCommand += new
System.Web.UI.WebControls.DataGridCommandEventHand ler(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>

 
Reply With Quote
 
 
 
 
=?Utf-8?B?RWx0b24gVw==?=
Guest
Posts: n/a
 
      02-03-2006
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



"(E-Mail Removed)" wrote:

> 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.DataGridCommandEventHand ler(this.DataGrid1_CancelCommand);
> this.DataGrid1.EditCommand += new
> System.Web.UI.WebControls.DataGridCommandEventHand ler(this.DataGrid1_EditCommand);
> this.DataGrid1.UpdateCommand += new
> System.Web.UI.WebControls.DataGridCommandEventHand ler(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>
>
>

 
Reply With Quote
 
 
 
 
andrew.roberts@poundland.co.uk
Guest
Posts: n/a
 
      02-03-2006
Thanks Elton, I have now got this working.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Cisco Switch not responding to Link event Notification OAMPDU Ramya Cisco 0 02-26-2009 09:45 AM
SelectedIndexChanged event not responding on selecting gridview =?Utf-8?B?QVQ=?= ASP .Net 1 04-01-2005 09:37 PM
Datagrid imagebutton not responding Dave ASP .Net 0 07-09-2004 09:53 AM
Datagrid not responding =?Utf-8?B?QnJhZCBTaW1vbg==?= ASP .Net 1 01-18-2004 02:18 AM
webuser control not responding to event? Comcast ASP .Net Building Controls 2 08-07-2003 10:43 PM



Advertisments