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:
Hello,

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.
Posted
Comments
   
You forgot to mention your progress and issue.

Button add code :

protected void btnAdd_Click(object sender, ImageClickEventArgs e)
       {
           try
           {
               AddToOrder();
           }
           catch
           {
               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;
               context.Orders.InsertOnSubmit(order);
               context.SubmitChanges();
           this.RefreshGrid();
       }


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();
           OrderGridView.DataBind();

       }


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


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

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

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