Click here to Skip to main content
14,690,503 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi Friends,
I am using dynamic Gridview with dynamic textbox inside the coloumns of the gridview to get user input and a button in one coloumn to add new row to the Gridview. My problem not get values enter in the textbox from client to server side. How to access the values in the textboxes. I am generating grid from the method in Page_Load event.
Please help me. Below is my code...



#region Properties
        /// <summary>
        /// Represents the property for Grid View Data Source
        /// </summary>
        public DataTable GridData
        {
            get
            {
                if (ViewState["GridData"] == null)
                {
                    DataTable GridData = new DataTable();
                    GridData.Columns.Add(new DataColumn("IndexNumber"));
                    GridData.Columns.Add(new DataColumn("ItemNumber"));
                    GridData.Columns.Add(new DataColumn("Particulars"));
                    GridData.Columns.Add(new DataColumn("Quantity"));
                    GridData.Columns.Add(new DataColumn("Meters"));
                    GridData.Columns.Add(new DataColumn("Rate"));
                    GridData.Columns.Add(new DataColumn("AddLess"));
                    GridData.Columns.Add(new DataColumn("Amount"));
                    GridData.Columns.Add(new DataColumn("AddRow"));

                    DataRow row = GridData.NewRow();
                    row["IndexNumber"] = "1";
                    row["ItemNumber"] = "ItemNumber";
                    row["Particulars"] = "Particulars";
                    row["Quantity"] = "Quantity";
                    row["Meters"] = "Meters";
                    row["Rate"] = "Rate";
                    row["AddLess"] = "AddLess";
                    row["Amount"] = "Amount";
                    row["AddRow"] = "AddRow";
                    GridData.Rows.Add(row);

                    return GridData;
                }

                return ViewState["GridData"] as DataTable;
            }
            set
            {
                ViewState["GridData"] = value;
            }
        }

        /// <summary>
        /// Represents the property for Grid row Index
        /// </summary>
        public int Index
        {
            get
            {
                if (ViewState["Index"] == null)
                {
                    ViewState["Index"] = 0;

                    return 0;
                }

                int index = (int)ViewState["Index"];

                index += 1;

                ViewState["Index"] = index;

                return index;
            }
            set
            {
                ViewState["Index"] = value;
            }
        }
        #endregion

        #region Page Methods        


        /// <summary>
        /// Represents the page method that is called on Page load
        /// </summary>
        /// <param name="sender">
        /// An <see cref="System.Object"/> that holds the sender's instance
        /// </param>
        /// <param name="e">
        /// An <see cref="System.EventArgs"/> that holds the event argument
        /// </param>
        protected void Page_Load(object sender, EventArgs e)
        {

            DefineGridColoumns();
            grdParticulars.DataSource = this.GridData;
            grdParticulars.DataBind();
        }
        #endregion


        #region Gridview Events
        /// <summary>
        /// Represents the event handler of Grid Data Bind
        /// </summary>
        /// <param name="sender">
        /// An <see cref="System.Object"/> that holds the sendes's instance
        /// </param>
        /// <param name="e">
        /// An <see cref="System.Web.UI.WebControls.GridViewRowEventArgs"/> that holds the Grid view row event Argument
        /// </param>
        protected void OnGridDataBound(object sender, GridViewRowEventArgs e)
        {
            grdParticulars.Columns.Clear();

            if (e.Row.RowType == DataControlRowType.Header ||
                e.Row.RowType == DataControlRowType.Footer)
                return;

            //int index = this.Index;
            String buttonID = String.Format("btn{0}", e.Row.Cells[0].Text);

            // Label for index coloumn
            Label IndexColoumn = new Label();
            IndexColoumn.ID = "lbl" + e.Row.Cells[0].Text;
            IndexColoumn.Text = e.Row.Cells[0].Text;
            e.Row.Cells[0].Controls.Add(IndexColoumn);

            // Text box for Item Number
            TextBox Items = new TextBox();
            Items.AutoCompleteType = AutoCompleteType.None;
            Items.ID = "txtItemNumber" + e.Row.Cells[0].Text;
            Items.Width = Unit.Percentage(90);
            e.Row.Cells[1].HorizontalAlign = HorizontalAlign.Center;
            e.Row.Cells[1].Controls.Add(Items);

            AutoCompleteExtender AutoComplete = new AutoCompleteExtender();
            AutoComplete.ID = "txtItemNumber";
            AutoComplete.ServiceMethod = "GetProducts";
            AutoComplete.TargetControlID = Items.ID;
            AutoComplete.CompletionInterval = 1000;
            AutoComplete.EnableCaching = true;
            AutoComplete.MinimumPrefixLength = 1;
            AutoComplete.CompletionSetCount = 20;
            e.Row.Cells[1].Controls.Add(AutoComplete);

            // Text box for Particulars
            Items = new TextBox();
            Items.AutoCompleteType = AutoCompleteType.None;
            Items.ID = "txtParticulars" + e.Row.Cells[0].Text;
            Items.Width = Unit.Percentage(99);
            e.Row.Cells[2].HorizontalAlign = HorizontalAlign.Center;
            e.Row.Cells[2].Controls.Add(Items);

            AutoComplete = new AutoCompleteExtender();
            AutoComplete.ID = "txtParticularsAutoComplete";
            AutoComplete.ServiceMethod = "GetProducts";
            AutoComplete.TargetControlID = Items.ID;
            AutoComplete.CompletionInterval = 1000;
            AutoComplete.EnableCaching = true;
            AutoComplete.MinimumPrefixLength = 1;
            AutoComplete.CompletionSetCount = 20;
            e.Row.Cells[2].Controls.Add(AutoComplete);

            //// Text box for Quantity
            Items = new TextBox();
            Items.AutoCompleteType = AutoCompleteType.None;
            Items.ID = "txtQuantity" + e.Row.Cells[0].Text;
            Items.Width = Unit.Percentage(90);
            e.Row.Cells[3].HorizontalAlign = HorizontalAlign.Center;
            e.Row.Cells[3].Controls.Add(Items);

            // Text box for Meters
            Items = new TextBox();
            Items.AutoCompleteType = AutoCompleteType.None;
            Items.ID = "txtMeters" + e.Row.Cells[0].Text;
            Items.Width = Unit.Percentage(90);
            e.Row.Cells[4].HorizontalAlign = HorizontalAlign.Center;
            e.Row.Cells[4].Controls.Add(Items);

            // Text box for Rate
            Items = new TextBox();
            Items.AutoCompleteType = AutoCompleteType.None;
            Items.ID = "txtRate" + e.Row.Cells[0].Text;
            Items.Width = Unit.Percentage(90);
            e.Row.Cells[5].HorizontalAlign = HorizontalAlign.Center;
            e.Row.Cells[5].Controls.Add(Items);

            // Text box for AddLess
            Items = new TextBox();
            Items.AutoCompleteType = AutoCompleteType.None;
            Items.ID = "txtAddLess" + e.Row.Cells[0].Text;
            Items.Width = Unit.Percentage(90);
            e.Row.Cells[6].HorizontalAlign = HorizontalAlign.Center;
            e.Row.Cells[6].Controls.Add(Items);

            // Text box for Amount
            Items = new TextBox();
            Items.AutoCompleteType = AutoCompleteType.None;
            Items.ID = "txtAmount" + e.Row.Cells[0].Text;
            Items.Width = Unit.Percentage(90);
            e.Row.Cells[7].Controls.Add(Items);


            Button AddRow = new Button();
            AddRow.ID = "btn";
            AddRow.Text = "+";
            AddRow.Width = Unit.Percentage(100);
            AddRow.CommandName = buttonID;
            AddRow.Click += new EventHandler(AddRow_Click);
            e.Row.Cells[8].HorizontalAlign = HorizontalAlign.Center;
            e.Row.Cells[8].Controls.Add(AddRow);
        }


        /// <summary>
        /// Represents the event handler of Grid view add row Button
        /// </summary>
        /// <param name="sender">
        /// An <see cref="System.Object"/> that holds the sender's instance
        /// </param>
        /// <param name="e">
        /// An <see cref="System.EventArgs"/> that holds the event argument
        /// </param>
        void AddRow_Click(object sender, EventArgs e)
        {

            DataTable Table = this.GridData;

            int index = this.Index;

            index += 2;
            DataRow row = Table.NewRow();
            row["IndexNumber"] = index;
            row["Particulars"] = "Particulars" + index;
            row["AddRow"] = "AddRow" + index;
            Table.Rows.Add(row);

            this.GridData = Table;

            DefineGridColoumns();
            grdParticulars.DataSource = this.GridData;
            grdParticulars.DataBind();

        }
        #endregion

        #region Gridview Methods
        /// <summary>
        /// Represents the method for Grid template definition
        /// </summary>
        private void DefineGridColoumns()
        {
            grdParticulars.Columns.Clear();

            BoundField boundColumn = new BoundField();
            boundColumn.DataField = "IndexNumber";
            grdParticulars.Columns.Insert(0, boundColumn);

            TemplateField TemplateColoumn = new TemplateField();
            TemplateColoumn.HeaderText = "ItemNumber";
            TemplateColoumn.HeaderStyle.Width = Unit.Percentage(5);
            TemplateColoumn.HeaderStyle.HorizontalAlign = HorizontalAlign.Center;
            grdParticulars.Columns.Insert(1, TemplateColoumn);

            TemplateColoumn = new TemplateField();
            TemplateColoumn.HeaderText = "Particluars";
            TemplateColoumn.HeaderStyle.Width = Unit.Percentage(65);
            TemplateColoumn.HeaderStyle.HorizontalAlign = HorizontalAlign.Center;
            grdParticulars.Columns.Insert(2, TemplateColoumn);

            TemplateColoumn = new TemplateField();
            TemplateColoumn.HeaderText = "Quantity";
            TemplateColoumn.HeaderStyle.Width = Unit.Percentage(5);
            grdParticulars.Columns.Insert(3, TemplateColoumn);

            TemplateColoumn = new TemplateField();
            TemplateColoumn.HeaderText = "Meters";
            TemplateColoumn.HeaderStyle.Width = Unit.Percentage(5);
            grdParticulars.Columns.Insert(4, TemplateColoumn);

            TemplateColoumn = new TemplateField();
            TemplateColoumn.HeaderText = "Rate";
            TemplateColoumn.HeaderStyle.Width = Unit.Percentage(5);
            grdParticulars.Columns.Insert(5, TemplateColoumn);

            TemplateColoumn = new TemplateField();
            TemplateColoumn.HeaderText = "AddLess";
            TemplateColoumn.HeaderStyle.Width = Unit.Percentage(5);
            grdParticulars.Columns.Insert(6, TemplateColoumn);

            TemplateColoumn = new TemplateField();
            TemplateColoumn.HeaderText = "Amount";
            TemplateColoumn.HeaderStyle.Width = Unit.Percentage(5);
            grdParticulars.Columns.Insert(7, TemplateColoumn);

            TemplateColoumn = new TemplateField();
            TemplateColoumn.HeaderText = "AddRow";
            TemplateColoumn.HeaderStyle.Width = Unit.Percentage(5);
            grdParticulars.Columns.Insert(8, TemplateColoumn);

            grdParticulars.HeaderStyle.BackColor = Color.Beige;
        }
Posted

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900