DataGrid not responding to Update/Cancel event

Discussion in 'ASP .Net' started by andrew.roberts@poundland.co.uk, Feb 3, 2006.

  1. Guest

    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>
     
    , Feb 3, 2006
    #1
    1. Advertising

  2. 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



    "" 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.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>
    >
    >
     
    =?Utf-8?B?RWx0b24gVw==?=, Feb 3, 2006
    #2
    1. Advertising

  3. Guest

    Thanks Elton, I have now got this working.
     
    , Feb 3, 2006
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?U3RldmUgS2FsbGFs?=

    CssClass on DataGrid Edit, Update & Cancel buttons

    =?Utf-8?B?U3RldmUgS2FsbGFs?=, Jul 6, 2004, in forum: ASP .Net
    Replies:
    0
    Views:
    746
    =?Utf-8?B?U3RldmUgS2FsbGFs?=
    Jul 6, 2004
  2. Lucas Tam
    Replies:
    1
    Views:
    491
    Lucas Tam
    Jul 30, 2004
  3. Craig G
    Replies:
    2
    Views:
    695
    Patrick Olurotimi Ige
    Dec 14, 2004
  4. =?Utf-8?B?cG11ZA==?=

    CANT see EDIT UPDATE CANCEL Buttons for DATAGRID

    =?Utf-8?B?cG11ZA==?=, Dec 28, 2004, in forum: ASP .Net
    Replies:
    3
    Views:
    2,429
    =?Utf-8?B?cG11ZA==?=
    Dec 28, 2004
  5. datagrid update/cancel command not working

    , Sep 15, 2006, in forum: ASP .Net Web Controls
    Replies:
    2
    Views:
    781
Loading...

Share This Page