tags:

views:

210

answers:

2

Hello all, 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)
                            );
        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);
        }
    }
}
+1  A: 

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

if (!Page.IsPostBack)
{
    // Populate list
}
Dan Diplo
10x, 10x and again 10x!!!!!!!!your great :-)hope to be like you one day...lol
Erez
A: 

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
}
abraganza
Didn't get ti ajax yet....but thanks....going to study that later this year :-)
Erez