Wrox Programmer Forums
Go Back   Wrox Programmer Forums > ASP.NET and ASP > ASP.NET 1.0 and 1.1 > ASP.NET 1.0 and 1.1 Professional
|
ASP.NET 1.0 and 1.1 Professional For advanced ASP.NET 1.x coders. Beginning-level questions will be redirected to other forums. NOT for "classic" ASP 3 or the newer ASP.NET 2.0 and 3.5
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ASP.NET 1.0 and 1.1 Professional section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old January 30th, 2006, 01:21 PM
Registered User
 
Join Date: Jul 2005
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default Update/Cancel Command not responding in DataGrid

I hope some one can help with this. I have a data grid that responds to the Edit command but does not respond to either the Canel or Update button clicks, it just sits there. What makes this even more frustrating is the fact that in an earlier project I had this working on my first try. Now I cant see what I'm doing wrong having stared at the code for about 2 days. My code is below minus the Update_Command method, I have been toying with it. Thanks

C# server side code

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 DataGridTest
{
    /// <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 lblError;

        protected System.Data.DataTable datatbl;

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

        }

        private void onPageOpen()
        {
            dbc();
            createTable();
            getData();
        }

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

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

        private void getData()
        {
            DataRow row;

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

                while (this.rdr.Read())
                {
                    row = this.datatbl.NewRow();
                    row[0] = this.rdr.GetInt32(0);
                    row[1] = this.rdr.GetString(1);
                    row[2] = this.rdr.GetString(2);
                    this.datatbl.Rows.Add(row);
                }

                this.rdr.Close();
                this.cmd.Dispose();
                Session.Add("table", this.datatbl);
                bindGrid();
            }
            catch(SqlException sqlex)
            {

            }
            finally
            {
                this.con.Close();
            }
        }

        private void bindGrid()
        {
            this.DataGrid1.DataSource = (DataTable)Session["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.ItemCommand += new System.Web.UI.WebControls.DataGridCommandEventHand ler(this.DataGrid1_ItemCommand);
            this.DataGrid1.EditCommand += new System.Web.UI.WebControls.DataGridCommandEventHand ler(this.DataGrid1_EditCommand);
            this.DataGrid1.CancelCommand += new DataGridCommandEventHandler(this.DataGrid1_CancelC ommand);
            this.Load += new System.EventHandler(this.Page_Load);
            onPageOpen();
        }
        #endregion

        private void DataGrid1_EditCommand(object source, DataGridCommandEventArgs e)
        {
            this.lblError.Text = "Edit";
            this.DataGrid1.EditItemIndex = (int)e.Item.ItemIndex;
            this.lblError.Text = "Edit";
            this.bindGrid();
            this.lblError.Text = "Edit";

        }

        public void DataGrid1_CancelCommand(object source, DataGridCommandEventArgs e)
        {
            this.lblError.Text = "Cancel";
            this.DataGrid1.EditItemIndex = -1;
            //getData();
            this.lblError.Text = "Cancel";
        }

        private void DataGrid1_ItemCommand(object source, DataGridCommandEventArgs e)
        {
            if (e.CommandSource.ToString() == "Cancel")
            {
                this.lblError.Text = "Cancel";
            }
        }
    }
}

ASPX code

<%@ Page language="c#" Codebehind="default.aspx.cs" AutoEventWireup="false" Inherits="DataGridTest.WebForm1" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
    <HEAD>
        <title>WebForm1</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: 24px; POSITION: absolute; TOP: 32px" runat="server"
                DataKeyField="SKU" AutoGenerateColumns="False">
                <Columns>
                    <asp:ButtonColumn Text="Select" CommandName="Select"></asp:ButtonColumn>
                    <asp:BoundColumn DataField="SKU" ReadOnly="True" HeaderText="SKU"></asp:BoundColumn>
                    <asp:BoundColumn DataField="Description" HeaderText="Description"></asp:BoundColumn>
                    <asp:BoundColumn DataField="Grade" HeaderText="Grade"></asp:BoundColumn>
                    <asp:EditCommandColumn HeaderText="Edit" EditText="Edit" CancelText="Cancel" UpdateText="Update" ButtonType="PushButton"></asp:EditCommandColumn>
                </Columns>
            </asp:datagrid><asp:label id="lblError" style="Z-INDEX: 102; LEFT: 360px; POSITION: absolute; TOP: 536px"
                runat="server" Height="32px" Width="376px">g</asp:label></form>
    </body>
</HTML>
 
Old January 30th, 2006, 02:50 PM
Friend of Wrox
 
Join Date: Nov 2003
Posts: 1,348
Thanks: 0
Thanked 5 Times in 5 Posts
Default

In the cancel event, you have to rebind the grid. Also, I don't see an event for updating.

Jim

 
Old January 30th, 2006, 03:00 PM
Registered User
 
Join Date: Jul 2005
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default

The reason there is no rebind method is because I have a label on the web form which has its text property set to "Cancel" when ever the Cancel button is clicked. This is my measure to determine if the click event has been registered. I have something similar with the Edit method, I know this works since the label's text property changes. Since the label never changes when Cancel is clicked I can assume its because it has not been called. Why I can not understand. I have omitted the Update since it doesnt work either and I was trying to get the Cancel working first.






Similar Threads
Thread Thread Starter Forum Replies Last Post
Update command yogeshyl Oracle 0 November 28th, 2007 08:05 AM
How are the Update/Cancel links wired? BKahuna BOOK: ASP.NET 2.0 Website Programming Problem Design Solution ISBN: 978-0-7645-8464-0 5 October 7th, 2007 12:58 PM
Update command in Datagrid chnswam C# 0 March 17th, 2006 03:04 AM
Update Command In DataGrid RPG SEARCH ASP.NET 1.0 and 1.1 Basics 9 February 21st, 2005 09:20 AM
Help me ! Cancel Update Error ! minhtri Pro VB Databases 0 August 28th, 2004 09:03 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.