Hi,
i want a drop down menu to choose how many of that ITEMS they want to purchase. The max number in that box should be the max number of items we have in stock for that base and add. drop down list consist , ,1,2,3,4,5, that way if someone chooses that item and then choose the blank space and it will remove that item from what they want to purchase. For example, if we have 4 items in stock, the drop down box will go blank, 1, 2, 3, 4.
Here i need how to write code for dropdown list to update automatically when stock is reduces.
Francis SusaimichaelPosted Jun 2, 2016, 6:33 AM
You should code like below:
Mark up (aspx file)
<asp:DropDownList runat="server" ID="ddlStockCount">asp:DropDownList>
protected void Page_Load(object sender, EventArgs e)
{
if(!Page.IsPostBack) // Check the page is freshly loaded
{
//Get the stock count from DB, omitted for simplicity
int stockcount = 5;
// Add the items count to Dropdown
for(int i=1;i <=stockcount;i++)
{
ListItem li = new ListItem(Convert.ToString(i), Convert.ToString(i));
ddlStockCount.Items.Add(li);
}
// finally add an empty item
ListItem litem = new ListItem(String.Empty,string.Empty);
ddlStockCount.Items.Add(litem);
//select the empty item by default
litem.Selected = true;
}
}