C# 中的 DropDownList,每次使用选择项目后都会导致 DropDownList 项目溢出
问题是我试图让 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
仅在第一次加载时通过检查页面是否尚未回发来填充 DropDownList。
Only populate the DropDownList on first load by checking whether the page has not posted back ie.
我同意 Dan 的观点,如果您有任何启用 ajax 的控件,也会添加以下内容,因为它们可能会生成回调。
I agree with Dan and would add the following as well if you have any ajax enabled controls as they may generate callbacks.