C# 中的 DropDownList,每次使用选择项目后都会导致 DropDownList 项目溢出

发布于 2024-08-11 21:49:56 字数 1554 浏览 13 评论 0原文

问题是我试图让 DDL 达到: 1. 从数据库表接收类别 - 工作 2. OnChange 从不同的表中按 DDL 中的项目选择产品 - 工作 No1 有问题,但解决了该问题。我发现要让 No1 工作,我必须使用回发。这样做了,那部分的每件事都工作得很好,实际上每件事都在工作......但我的拥抱问题(我找不到任何好的答案)是,每次我改变项目时,我都会得到所有的时间再次(我有初始 8 项 - 第二次 16 - 24 等......) 尝试使用:ddlCatagories.Items.Clear(); 当我使用它时,我没有得到任何重复项,但是,我没有得到任何东西,它每次都会从列表中选择第一个类别,无论我在列表中选择什么。 试图弄清楚过去一周的情况...请帮忙:-)

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

她比我温柔 2024-08-18 21:49:56

仅在第一次加载时通过检查页面是否尚未回发来填充 DropDownList。

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

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

if (!Page.IsPostBack)
{
    // Populate list
}
还给你自由 2024-08-18 21:49:56

我同意 Dan 的观点,如果您有任何启用 ajax 的控件,也会添加以下内容,因为它们可能会生成回调。

if (!Page.IsPostBack && !Page.IsCallBack)
{
    // 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
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文