C#下拉列表不触发索引更改

时间:2017-12-31 10:48:10

标签: c# asp.net drop-down-menu

我在ddl上选择了一个值,但它没有在页面中显示产品。所选值仍保持绑定,但页面为空白。

另外,如果我只是调用函数getCat()而不使用if(!ispostback)。当我加载页面时,下拉列表卡在第一个值上,但它显示页面中的产品。

下拉列表:

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

这是实施:

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);

}

1 个答案:

答案 0 :(得分:1)

您的问题可能是您的代码隐藏方法是私有的:

private void Ddcateg_SelectedIndexChanged(object sender, EventArgs e)

尝试将其保护或公开,以便aspx页面可以看到它。