Miucin Sebastian
Miucin Sebastian

Reputation: 17

C# Drop Down List Not Trigger index changing

I select one value on the ddl , and it does not show the products in the page. The selected value remains binded but the page is blank.

Also , if I just call function getCat() without using if(!ispostback). When i load the page the drop down list is stuck on the first value , but it shows the products in page.

Drop Down List:

<asp:dropdownlist runat="server" id="ddcateg" AutoPostBack="true" onselectedindexchanged="Ddcateg_SelectedIndexChanged"></asp:dropdownlist>

This is the implementation:

protected void Page_Load(object sender, EventArgs e)
{
    //afisare();

    if (!IsPostBack)
    {
        getCateg();
    }

}


public void getCateg()
{

    ProdusTipModel model = new ProdusTipModel();
    FarmacieEntities db = new FarmacieEntities();

    var lizt = (from c in db.ProdusTips select c).ToList();


        ddcateg.DataSource = lizt;
        ddcateg.DataValueField = "ID";
        ddcateg.DataTextField = "Name";


    ddcateg.DataBind();
    ddcateg.SelectedIndexChanged += Ddcateg_SelectedIndexChanged;
}


public void afisare2(List<Produ> z)
{
    ProdusModel mdl = new ProdusModel();

        foreach (var produs in z)
        {
            Panel produsePnl = new Panel();
            ImageButton imageButton = new ImageButton();
            produsePnl.BorderColor = Color.AliceBlue;

            Label lblNume = new Label();
            Label lblPret = new Label();

            produsePnl.BorderStyle = BorderStyle.Groove;
            produsePnl.BorderColor = Color.LightSkyBlue;

            imageButton.ImageUrl = "~/Img/Produse/" + produs.Image;
            imageButton.CssClass = "imgProdus";
            imageButton.PostBackUrl = "~/Pages/PaginaProdus.aspx?id=" + produs.ID;

            lblNume.Text = produs.Name;
            lblNume.CssClass = "numeProd";

            lblPret.Text = produs.Price + "lei";
            lblPret.CssClass = "produsPret";


            produsePnl.Controls.Add(imageButton);
            produsePnl.Controls.Add(new Literal { Text = "<br /" });
            produsePnl.Controls.Add(lblNume);
            produsePnl.Controls.Add(new Literal { Text = "<br /" });
            produsePnl.Controls.Add(lblPret);

            pnlProduse.Controls.Add(produsePnl);

        }

}



private void Ddcateg_SelectedIndexChanged(object sender, EventArgs e)
{
   DropDownList selectedList = (DropDownList)sender;
   int selectedLit = Convert.ToInt32(selectedList.SelectedValue);

   ProdusModel mdl = new ProdusModel();
   List<Produ> list = mdl.GetProdCateg(selectedLit).ToList();

   afisare2(list);

}

Upvotes: 0

Views: 504

Answers (1)

SBFrancies
SBFrancies

Reputation: 4240

Your problem could be that your code behind method is private:

private void Ddcateg_SelectedIndexChanged(object sender, EventArgs e)

Try making it protected or public so it can be seen by the aspx page.

Upvotes: 1

Related Questions