开发者

DropDownList in C#, getting DropDownList items overflow after every time using selecting an item

开发者 https://www.devze.com 2022-12-12 09:49 出处:网络
well the problem is that i am trying to get DDL to: 1. Recive catagories from a DB tabel - working 2. OnChange select from a different table the products by the item in the DDL - working

well the problem is that i am trying to get DDL to: 1. Recive catagories from a DB tabel - working 2. OnChange select from a different table the products by the item in the DDL - working had a problem with No1 but fixed that problem. i found out that to get No1 working i have to use postback. did that and every thing in that part is working well and actualy every thing is working...but my hug problem (and i cant find any good answer for it) is that every time i change the item i a getting all the times all over again(i have initialy 8 item - secont time 16 - 24 etc....) tried to use: ddlCatagories.Items.Clear(); when i use that i am not getting any duplicates but then, i am not getting any thing, it takes the first catagory from the list every time, no matter what i chose in the list.. trying to figure it out for the past week...please help :-)

    public partial class selectNamesFromCatagories : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        ddlCatagories.Items.Clear();
        SqlDataReader dr = DbHelper.ExecuteReader(
            sqlConn1.home,
            "spSelectNamesFromCatagories");
        while (dr.Read())
        {
            ListItem li = new ListItem(dr["CategoryName"].ToString());
            ddlCatagories.Items.Add(li);
        }
        dr.Close();
    }
    protected void ddlCatagories_SelectedIndexChanged(object sender, EventArgs e)
    {
        SqlDataReader dr = DbHelper.ExecuteReader(
                            sqlConn1.home,
                            "spProductsByCatagoryID",
                            new SqlParameter("@catName", ddlCatagories.Text)
          开发者_Go百科                  );
        while (dr.Read())
        {
            TableRow tr = new TableRow();
            for (int i = 0; i < dr.FieldCount; i++)
            {
                TableCell td = new TableCell();
                td.Text = dr[i].ToString();
                tr.Controls.Add(td);
            }
            tblProductsByCatagories.Controls.Add(tr);
        }
    }
}


Only populate the DropDownList on first load by checking whether the page has not posted back ie.

if (!Page.IsPostBack)
{
    // Populate list
}


I agree with Dan and would add the following as well if you have any ajax enabled controls as they may generate callbacks.

if (!Page.IsPostBack && !Page.IsCallBack)
{
    // Populate list
}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号