Click here to Skip to main content
14,971,670 members
Please Sign up or sign in to vote.
2.33/5 (2 votes)
See more:

I have a drop-down list and a text box with a button named add.

The functionality should be something like this:

After selecting a certain value from the drop-down, writing something in the text box when the Add button is pressed it will populate the values from the fields in a grid view below.

When a second value is selected from the drop-down with some text from the text box, the button add should append a new row in the grid with the previous one.

And new rows will keep on adding on button press.
Please help.
You forgot to mention your progress and issue.

Button add code :

protected void btnAdd_Click(object sender, ImageClickEventArgs e)
               string Msg = "<script>alert('Error occurred during adding the product..!');</script>";
               ScriptManager.RegisterClientScriptBlock(this, typeof(Page), "alert", Msg, false);

public void AddToOrder()
           OrdersDataContext context = DataContextSingleton.Instance;

               _order = new Order();
               _order.quantity= Convert.ToInt32(dropdownlist.SelectedItem.Value);
               _order.price = Convert.ToInt32(dropdownlist2.SelectedItem.Value);
               _order.note = TextBox4.Text;

public void RefreshGrid()
           OrdersDataContext contex = DataContextSingleton.Instance;

           var query = (from o in _context.Orders
                        select new
                            Quantity = o.quant,
                            Price = o.price,
                            notes= o.note

           OrderGridView.DataSource = query.ToList();


I hope this could help you!
You can use DataTable along with Session to achieve this. Below is code snippet for reference
<html xmlns="" >
<head runat="server">
    <title>Untitled Page</title>
    <form id="form1" runat="server">
        <asp:dropdownlist id="DropDownList1" runat="server" xmlns:asp="#unknown">
        <asp:button id="Button1" runat="server" onclick="Button1_Click" text="Button" xmlns:asp="#unknown" />
    <asp:gridview id="GridView1" runat="server" xmlns:asp="#unknown">

protected void Button1_Click(object sender, EventArgs e)
    DataTable dt = null;
    if (Session["datatable"] != null)
        dt = (DataTable)Session["datatable"];
        dt = GetDataTable();
   DataRow dr = dt.NewRow();
   dr["Items"] = DropDownList1.SelectedValue;

   GridView1.DataSource = dt;
   Session["datatable"] = dt;
private DataTable GetDataTable()
    DataTable dt = new DataTable();
    dt.Columns.Add(new DataColumn("Items"));
    return dt;

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