Wrox Programmer Forums
Go Back   Wrox Programmer Forums > .NET > Other .NET > General .NET
|
General .NET For general discussion of MICROSOFT .NET topics that don't fall within any of the other .NET forum subcategories or .NET language forums.  If your question is specific to a language (C# or Visual Basic) or type of application (Windows Forms or ASP.Net) try an applicable forum category. ** PLEASE BE SPECIFIC WITH YOUR QUESTION ** When posting here, provide details regarding the Microsoft .NET language you are using and/or what type of application (Windows/Web Forms, etc) you are working in, if applicable to the question. This will help others answer the question without having to ask.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the General .NET 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 September 13th, 2007, 06:33 AM
Registered User
 
Join Date: Sep 2007
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default DataGrid ItemCommand not fired

I have created a DataGrid with 4 columns. In the ItemTemplate tag I have two textboxes to accept inputs. 1 command button and one Result column. While loading the page I use a DataTable and Dataview is datasource for the DataGrid. Then I bind the grid. What I want to know is do I have to bind a grid in ItemCommand as well. My main problem is the control does not go in the ItemCommand event.

HTML and Code is as follows:


<body bgColor="#99ccff" MS_POSITIONING="GridLayout">
        <form id="Form1" method="post" runat="server">
            <asp:datagrid id="DataGrid1" style="Z-INDEX: 101; LEFT: 288px; POSITION: absolute; TOP: 80px"
                runat="server" BackColor="Silver" ForeColor="Blue" Font-Size="Small" Font-Names="Courier New"
                HorizontalAlign="Center" AutoGenerateColumns="False" BorderColor="Blue" Visible="True" EnableViewState =True >
                <EditItemStyle Font-Size="Small" Font-Names="Times New Roman" ForeColor="Red" BackColor="Silver"></EditItemStyle>
                <Columns>
                    <asp:TemplateColumn HeaderText="String1">
                        <ItemTemplate>
                            <asp:TextBox id="TextBox1" runat="server" AutoPostBack="True" Visible="True" Text= '<%# DataBinder.Eval(Container.DataItem,"val1")%>' EnableViewState = True >
                            </asp:TextBox>
                        </ItemTemplate>
                    </asp:TemplateColumn>
                    <asp:TemplateColumn HeaderText="String2">
                        <ItemTemplate>
                            <asp:TextBox id=TextBox2 runat="server" AutoPostBack="True" Text= '<%# DataBinder.Eval(Container.DataItem,"val2")%>' Visible = True EnableViewState = True >
                            </asp:TextBox>
                        </ItemTemplate>
                    </asp:TemplateColumn>
                    <asp:TemplateColumn HeaderText="Concat Buttons">
                        <ItemTemplate>

                            <asp:LinkButton ID="Button1" Runat="server" Text="Concatinate" CommandName="Concat" EnableViewState =True > </asp:LinkButton>
                        </ItemTemplate>
                    </asp:TemplateColumn>
                    <asp:TemplateColumn HeaderText="Result">
                        <ItemTemplate>
                            <asp:Label id="Label1" Visible="True" ForeColor="#C000C0" BackColor="#FFFFC0" Runat="server" EnableViewState =True></asp:Label>
                        </ItemTemplate>
                    </asp:TemplateColumn>
                </Columns>
            </asp:datagrid></form>
    </body>

Code is As follows:
namespace DataGridApp1
{
    /// <summary>
    /// Summary description for ReCalculateColumn.
    /// </summary>
    public class ReCalculateColumn : System.Web.UI.Page
    {
        protected System.Web.UI.WebControls.DataGrid DataGrid1;
        DataTable StringConcat;
        DataView dv1;
        private void Page_Load(object sender, System.EventArgs e)
        {
            // Put user code to initialize the page here
            if (!Page.IsPostBack)
            {

                BindGrid();


            }

                StringConcat = new DataTable();

                StringConcat.Columns.Add( new DataColumn("val1", typeof(string)));
                StringConcat.Columns.Add( new DataColumn("val2", typeof(string)));





        }

        public void BindGrid()
        {
            DataTable dt = new DataTable();
            dt.Columns.Add( new DataColumn("val1", typeof(string)));
            dt.Columns.Add( new DataColumn("val2", typeof(string)));

            DataRow dr1 = dt.NewRow();
            dr1[0] = "1";
            dr1[1] = "2";
            dt.Rows.Add(dr1);
            DataRow dr2 = dt.NewRow();
            dr2[0] = "3";
            dr2[1] = "4";
            dt.Rows.Add(dr2);

            DataView dv = new DataView(dt);
            DataGrid1.DataSource = dv;
            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.
        /// </summary>
        private void InitializeComponent()
        {
            this.DataGrid1.SelectedIndexChanged += new System.EventHandler(this.DataGrid1_SelectedIndexCh anged);
            this.Load += new System.EventHandler(this.Page_Load);

        }
        #endregion

        private void DataGrid1_SelectedIndexChanged(object sender, System.EventArgs e)
        {

        }

        private void DataGrid1_ItemDataBound(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
        {
            Response.Write(String.Concat("<b>ItemDataBound</b>: The = ", e.Item.ItemType.ToString(), "<br />"));

            if(e.Item.ItemType == ListItemType.Item)
            {
                //e.Item represents a DataRow
                //Cells Gets a collection of TableCell objects that represent the cells of a row in a Table control.
                //The Controls property allows you programmatic access to the instance of the ControlCollection class for any server control. You can add controls to the collection, remove controls from the collection, or iterate through the server controls in the collection.
                string str1 = e.Item.Cells[0].Text;
            }
        }

        private void DataGrid1_ItemCommand(object sender, System.Web.UI.WebControls.DataGridCommandEventArgs e)
        {
            Response.Write(String.Concat("<b>ItemCommand</b>: The CommandName = ", e.CommandName, "<br />"));

            if(e.CommandName == "Concat")
            {
                if(e.Item.ItemType==ListItemType.Item || e.Item.ItemType==ListItemType.AlternatingItem)
                {
                    Label l=(Label)e.Item.Cells[3].Controls[1];
                    string str1 = ((TextBox)e.Item.Cells[0].Controls[1]).Text;
                    string str2 = ((TextBox)e.Item.Cells[1].Controls[1]).Text;
                    string str3 = str1 + str2;
                    l.Text = str3;

                    DataRow dr = StringConcat.NewRow();
                    dr[0] = str1;
                    dr[1] = str2;
                    dr[2] = str3;
                    StringConcat.Rows.Add(dr);
                    dv1 = new DataView(StringConcat);
                    DataGrid1.DataSource = dv1;
                    DataGrid1.DataBind();
                }
            }
        }

    }
}





Similar Threads
Thread Thread Starter Forum Replies Last Post
OnRowCreated fired twice TweeZz ASP.NET 2.0 Basics 1 August 26th, 2008 03:08 AM
datalist itemcommand kethireddy435 ASP.NET 1.x and 2.0 Application Design 1 September 12th, 2007 06:58 AM
Datagrid ItemCommand firing twice spacky001 ASP.NET 1.0 and 1.1 Basics 1 May 16th, 2007 11:06 AM
DataList ItemCommand event problem in ASP.NET page naidukap ASP.NET 1.0 and 1.1 Professional 0 December 26th, 2005 03:26 AM
DataGrid SortCommand event not fired in UserContro vskumar ASP.NET 1.x and 2.0 Application Design 2 February 17th, 2005 08:06 AM





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