How to add datalist item to gridview using javascript

I want to add items to gridview when datalist item link button is clicked by using javascript.

This is the example i try, this in C# code i have placed below for your clarification.

 protected void DataList_itemCommand(object source, DataListCommandEventArgs e)
        {
            lblError.Text = "";
            int index = Convert.ToInt32(e.Item.ItemIndex);
            DataListItem item = dlOrderList.Items[index];
            Label lblProductCode = (Label)item.FindControl("lblProductCode");
            Label lblProductName = (Label)item.FindControl("lblProductName");
            Label lblUnitCode = (Label)item.FindControl("lblUnitCode");
            TextBox txtQty = (TextBox)item.FindControl("txtQty");
            Label lblMinQty = (Label)item.FindControl("lblMinQty");
            Label lblRate = (Label)item.FindControl("lblRate");
            if (txtQty.Text.Trim() == "")
            {
                ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "alertMessage", "alert('Please enter the Quantity!')", true);
            }
            else if (txtQty.Text.Trim() == "0")
            {
                ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "alertMessage", "alert('Please enter the valid Quantity')", true);
            }
            else if (Convert.ToDouble(txtQty.Text.Trim()) < Convert.ToDouble(lblMinQty.Text))
            {
                ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "alertMessage", "alert('Minimum Quantity to Order is " + lblMinQty.Text + "')", true);
            }
            else
            {
                Double Amount = (Convert.ToDouble(txtQty.Text.Trim()) * Convert.ToDouble(lblRate.Text));


                DataTable dtOrder = new DataTable();
                dtOrder.Columns.Add("ProductCode", typeof(string));
                dtOrder.Columns.Add("ProductName", typeof(string));
                dtOrder.Columns.Add("Qty", typeof(string));
                dtOrder.Columns.Add("Rate", typeof(string));
                dtOrder.Columns.Add("Amount", typeof(string));

                if (Session["CurrentTable"] != null)
                {
                    dtOrder = (DataTable)Session["CurrentTable"];
                }

                bool Valid = false;

                for (int row = 0; row <= dtOrder.Rows.Count - 1; row++)
                {
                    if (lblProductCode.Text == dtOrder.Rows[row]["ProductCode"].ToString())
                    {
                        dtOrder.Rows[row]["ProductCode"] = lblProductCode.Text;
                        dtOrder.Rows[row]["ProductName"] = lblProductName.Text;
                        if (lblUnitCode.Text.ToString() == "1")
                        {
                            dtOrder.Rows[row]["Qty"] = Convert.ToDecimal(txtQty.Text).ToString("#0.000");
                        }
                        else
                        {
                            dtOrder.Rows[row]["Qty"] = txtQty.Text;
                        }
                        dtOrder.Rows[row]["Rate"] = Convert.ToDecimal(lblRate.Text).ToString("#0.00");
                        dtOrder.Rows[row]["Amount"] = Amount.ToString("N2");
                        Valid = true;
                        break;
                    }

                }

                if (Valid == false)
                {
                    DataRow dr = dtOrder.NewRow();
                    dr[0] = lblProductCode.Text;
                    dr[1] = lblProductName.Text;

                    if (lblUnitCode.Text.ToString() == "1")
                    {
                        dr[2] = Convert.ToDecimal(txtQty.Text).ToString("#0.000");
                    }
                    else
                    {
                        dr[2] = txtQty.Text;
                    }
                    dr[3] = Convert.ToDecimal(lblRate.Text).ToString("#0.00");
                    dr[4] = Amount.ToString("#0.00");

                    dtOrder.Rows.Add(dr);
                }

                gdOrder.DataSource = dtOrder;

                Session["CurrentTable"] = dtOrder;
                gdOrder.DataBind();
                if (dtOrder.Rows.Count == 0)
                {
                    btnOrder.Visible = false;
                }
                else
                {
                    btnOrder.Visible = true;
                }
                txtQty.Text = string.Empty;
            }

        }

        protected void gdOrder_Delete(object sender, GridViewDeleteEventArgs e)
        {
            try
            {
                if (Session["CurrentTable"] != null)
                {
                    DataTable dt = (DataTable)Session["CurrentTable"];
                    DataRow drCurrentRow = null;
                    int rowIndex = Convert.ToInt32(e.RowIndex);
                    if (dt.Rows.Count > 0)
                    {
                        dt.Rows.Remove(dt.Rows[rowIndex]);
                        drCurrentRow = dt.NewRow();
                        Session["CurrentTable"] = dt;

                        gdOrder.DataSource = dt;
                        gdOrder.DataBind();

                        if (dt.Rows.Count == 0)
                        {
                            btnOrder.Visible = false;
                            lblTotalAmount.Text = "";
                        }
                        else
                        {
                            btnOrder.Visible = true;
                        }
                    }
                }

            }
            catch (Exception exMessage)
            {
                lblError.ForeColor = System.Drawing.Color.Red;
                lblError.Text = exMessage.Message;
            }
        }

Answers


you have to replace grid view by html table and use append function to fill table


Need Your Help

How to use the 'sweep' function

r statistics

When I look at the source of R Packages, i see the function sweep used quite often.