Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# ASP.NET
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:
 
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:
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 19-Nov-12 23:41pm
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

Hi Taresh,
 
You can use ItemDataBound Event to update a particular item.
 
<asp:datalist id="" runat="server" onitemdatabound="DataListItemEventHandler" xmlns:asp="#unknown"> 
</asp:datalist>
 
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;
  }
}
}
  Permalink  
v2
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

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 !
  Permalink  
Comments
Edo Tzumer at 24-Jan-13 8:07am
   
5+ Always a pleasure to solve something yourself, eh ;)
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Hi Taresh,
 
Use OnRowDataBound method to update each row.
 
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.
  Permalink  
Comments
Taresh Uppal at 20-Nov-12 5:56am
   
Is there even a event of this name exists for data list ???? I doubt ...:(
Mohd. Mukhtar at 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 at 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 at 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.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 OriginalGriff 300
1 Maciej Los 180
2 Richard MacCutchan 115
3 arvind mepani 104
4 Tino Fourie 92


Advertise | Privacy | Mobile
Web01 | 2.8.140709.1 | Last Updated 3 Dec 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid