English 中文(简体)
DropDownList in C#, getting DropDownList items overflow after every time using selecting an item
原标题:
  • 时间:2009-11-18 21:05:18
  •  标签:
  • ddl
  • c#-2.0

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);
        }
    }
}
最佳回答

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
}




相关问题
Column type of set and Rails

I am building a database of online offers. Each offer can be viewed in one or more countries, for instance an advertiser may be interested in reaching offers in the US & Canada. The list of ...

ORACLE - tables

After entering code to create a new table in SQL ORACLE, would the changes be saved if I was to log out of the SQL Session?

How do I add a foreign key to an existing SQLite table?

I have the following table: CREATE TABLE child( id INTEGER PRIMARY KEY, parent_id INTEGER, description TEXT); How do I add a foreign key constraint on parent_id? Assume foreign keys are ...

How do I DROP a constraint from a sqlite (3.6.21) table?

I have the following table: CREATE TABLE child( id INTEGER PRIMARY KEY, parent_id INTEGER CONSTRAINT parent_id REFERENCES parent(id), description TEXT); How do I drop the constraint?

Generate DDL programmatically on Postgresql

How can I generate the DDL of a table programmatically on Postgresql? Is there a system query or command to do it? Googling the issue returned no pointers.

PostgreSQL create table if not exists

In a MySQL script you can write: CREATE TABLE IF NOT EXISTS foo ...; ... other stuff ... and then you can run the script many times without re-creating the table. How do you do this in PostgreSQL?

热门标签