Click here to Skip to main content
15,897,704 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Hi all,
I am displaying some products from database for online shopping with their no. of stock available in drop down list in a datalist. The problem is I want to hide the add to cart button and drop down of stock if the stock available is 0. So I have tried doing it on item databound event but it either hides all the ddls or hides none.
Kindly give me a solution for it. here is my code:

C#
DataTable dtResult = bl_NecklaceSets.fetching_products_NecklaceSets();
            if (dtResult.Rows.Count != 0)
            {

                for (int i_count = 0; i_count < dtResult.Rows.Count; i_count++)
                {
                    i_out_of_stock_value = Convert.ToInt32(dtResult.Rows[i_count]["Out_Of_stock"]);
                    if (i_out_of_stock_value == 1)
                    {
                        dtResult.Rows[i_count].SetField("Product_Name", (dtResult.Rows[i_count]["Product_Name"]).ToString() + " - Already Sold");
                        dtResult.AcceptChanges();
                        DropDownList ddlQuantity = ((DropDownList)(dtlst_Necklace_Sets.NamingContainer.FindControl("dd_quntity")));
                        string str_quantity = ddlQuantity.SelectedItem.Text.ToString();
                           ;
                        if (str_quantity == "0")
                        {
                            ddlQuantity.Visible = false;
                        }
                        else
                        {
                            ddlQuantity.Visible = true;
                        }

                        
                    }


The method above shown is in page load not in databound. In data bound i have done it like this:
C#
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                DataTable dtResult = bl_NecklaceSets.fetching_products_NecklaceSets();
                for (int i_count = 0; i_count < dtResult.Rows.Count; i_count++)
               {
                    i_out_of_stock_value = Convert.ToInt32(dtResult.Rows[i_count]["Out_Of_stock"]);
                    if (i_out_of_stock_value == 1)
                    {
                        DropDownList ddlQuantity  = ((DropDownList)(e.Item.FindControl("dd_quntity"))) ;
                       string str_quantity = Convert.ToString(((DropDownList)(e.Item.FindControl("dd_quntity"))).SelectedItem.Text);
                       if (str_quantity == "0")
                        {
                            ddlQuantity.Visible = false;
                       }
                        else
                       {
                           ddlQuantity.Visible = true;
                      }
.
Please help !
Posted

Hi Taresh,

You can use ItemDataBound Event to update a particular item.

C#
<asp:datalist id="" runat="server" onitemdatabound="DataListItemEventHandler" xmlns:asp="#unknown"> 
</asp:datalist>


C#
proctected void OnRowDataBound(object sender, DataListItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
  DropDownList ddlQuantity  = ((DropDownList)(e.Item.FindControl("dd_quntity"))) ;
  //Find current value
  if (currValue.Equals("0"))
  {
      ddlQuantity.Visible = false;
  }
  else
  {
      ddlQuantity.Visible = true;
  }
}
}
 
Share this answer
 
v2
Hi Taresh,

Use OnRowDataBound method to update each row.

C#
proctected void OnRowDataBound(object sender, EventArg e)
{
  DropDownList ddlQuantity  = ((DropDownList)(e.Item.FindControl("dd_quntity"))) ;
  if (ddlQuantity.SelectedItem.Text.Equals("0"))
  {
      ddlQuantity.Visible = false;
  }
  else
  {
      ddlQuantity.Visible = true;
  }
}

hope this will help you.
 
Share this answer
 
Comments
Taresh Uppal 20-Nov-12 5:56am    
Is there even a event of this name exists for data list ???? I doubt ...:(
Mohd. Mukhtar 20-Nov-12 6:02am    
Sorry the above event method I have posted for the GridView. In that case you can do it in databound method itself.
Taresh Uppal 20-Nov-12 6:09am    
dats what I am saying that i tried doing it dat way but it dint happened as i wanted .
Mohd. Mukhtar 20-Nov-12 6:19am    
Actually you are hiding the dropdown in page load. If you do so either it will hide all or hide none. Remove those line of code from page load and then check.
HI, I solved this myself on databound event of my datalist. I just made some condition and iterated through all the rows of the datalist.. if the condition was true than that particular row's DDL hides else stays normal. !

Thank u all for your help !
 
Share this answer
 
Comments
Joezer BH 24-Jan-13 8:07am    
5+ Always a pleasure to solve something yourself, eh ;)

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