Saya sedang mengerjakan halaman Shop di mana saya ingin Dropdownlist menunjukkan Quantity yang tersedia. (Misalnya: jika stok berisi 2 item maka Dropdownlist berisi Item 1 diikuti oleh 2..)

Inilah kode yang saya coba, saya tidak tahu di acara mana saya harus meletakkannya.

`
Kode:

connect cu = new connect();

 System.Web.UI.WebControls.Label Label8 = (System.Web.UI.WebControls.Label)DataList1.FindControl("modelnoLabel");

//Modelnolabel contains model no


    cu.cmd.CommandText = "select qty from stock where modelno=@mod";
//qty is retrived based on modelnolabel text of datalist

    cu.cmd.Parameters.Clear();
    cu.cmd.Parameters.AddWithValue("@mod", Label8.Text);
    int qty = Convert.ToInt16(cu.cmd.ExecuteScalar());
    if (qty < 5 && qty > 0)
    {
        (DataList1.FindControl("DropDownList1") as DropDownList).Items.Clear();
        for (int i = 1; i < qty + 1; i++)
        {
            (DataList1.FindControl("DropDownList1") as DropDownList).Items.Add(Convert.ToString(i));
        }
    }
    else
    {
        (DataList1.FindControl("DropDownList1") as DropDownList).Visible = false;

    }`

Terima kasih sebelumnya

0
Shree Har 18 Maret 2019, 21:46

1 menjawab

Jawaban Terbaik

Terima kasih atas keheningannya, Ini sangat membantu saya mencari tahu sendiri :)

 protected void DataList1_Load(object sender, EventArgs e)
   {

for(int j=0;j<DataList1.Items.Count;j++)
       {
           System.Web.UI.WebControls.Label Label8 = (System.Web.UI.WebControls.Label)DataList1.Items[j].FindControl("modelnoLabel");
           connect cu = new connect();
           cu.cmd.CommandText = "select qty from stock where model=@mod";
           cu.cmd.Parameters.Clear();
           cu.cmd.Parameters.AddWithValue("@mod", Label8.Text);
           int qty = Convert.ToInt16(cu.cmd.ExecuteScalar());
           if (qty <= 5 && qty > 0)
           {
               (DataList1.Items[j].FindControl("DropDownList1") as DropDownList).Items.Clear();
               for (int i = 1; i < qty+1; i++)
               {
                   (DataList1.Items[j].FindControl("DropDownList1") as DropDownList).Items.Add(Convert.ToString(i));
                   (DataList1.Items[j].FindControl("qtylab") as System.Web.UI.WebControls.Label).Text = "Only "+qty+" Left";
                   (DataList1.Items[j].FindControl("qtylab") as System.Web.UI.WebControls.Label).ForeColor = Color.OrangeRed;
                   (DataList1.Items[j].FindControl("Button1") as System.Web.UI.WebControls.Button).Visible= true;
                   (DataList1.Items[j].FindControl("Button3") as System.Web.UI.WebControls.Button).Visible= true;
               }
           }
           else if (qty > 5)
           {
               (DataList1.Items[j].FindControl("DropDownList1") as DropDownList).Items.Clear();
               for (int i = 1; i < 6; i++)
               {
                   (DataList1.Items[j].FindControl("DropDownList1") as DropDownList).Items.Add(Convert.ToString(i));
                   (DataList1.Items[j].FindControl("qtylab") as System.Web.UI.WebControls.Label).ForeColor = Color.GreenYellow;
                   (DataList1.Items[j].FindControl("qtylab") as System.Web.UI.WebControls.Label).Text = "Available";
                   (DataList1.Items[j].FindControl("Button1") as System.Web.UI.WebControls.Button).Visible = true;
                   (DataList1.Items[j].FindControl("Button3") as System.Web.UI.WebControls.Button).Visible= true;

               }
           }
           else
           {
               (DataList1.Items[j].FindControl("DropDownList1") as DropDownList).Visible = false;
               (DataList1.Items[j].FindControl("qtylab") as System.Web.UI.WebControls.Label).Text = "Out of Stock";
               (DataList1.Items[j].FindControl("qtylab") as System.Web.UI.WebControls.Label).ForeColor = Color.Red;
               (DataList1.Items[j].FindControl("Button1") as System.Web.UI.WebControls.Button).Visible= false;
               (DataList1.Items[j].FindControl("Button3") as System.Web.UI.WebControls.Button).Visible = false;
               (DataList1.Items[j].FindControl("Label1") as System.Web.UI.WebControls.Label).Visible = false;

           } 

       }
}
0
Shree Har 24 Maret 2019, 18:24