从Codebehind下拉列表项 [英] Drop Down List Items from Codebehind
问题描述
你好
我试图根据FormView的值在页面后面的代码中构造一个下拉列表.
我已经对大多数逻辑进行了排序(我认为)以放入正确的值,但是当我从下拉列表中选择一个项目时,原始下拉列表会再次附加相同的值,因此列表会不断增长,那么我要做的就是保留所选项目和原始列表内容(无论页面从下拉列表中被发布回多少次).
我将以下代码放置在Page_Load事件部分中,这可能会导致问题吗?
任何帮助表示赞赏,但请注意,我是这个领域的新手...
Hello
I''m trying to construct a dropdown list in a code behind page, based on the values of a FormView.
I have most of the logic sorted (I think) to put in the correct values, but when I select an item from the drop down list, the orginial drop down list is appended with the same values again, so the list keeps growing and growing, when all I want it to do is retain the selected item and the original list content (no matter how many times the page is posted back from the drop down).
I have placed the following code in the Page_Load event section, could this be causing the problem?
Any help appreciated, but beaware, I''m a newbie in this.....
DropDownList3.Items.Add("-");
if (callDetails.DataKey["SOURCE_REFERENCE"] != "")
{
DropDownList3.Items.Add("Source");
}
if (callDetails.DataKey["SUBJECT_REFERENCE"] != "")
{
DropDownList3.Items.Add("Subject");
}
if (callDetails.DataKey["REFERENCE"] != "")
{
DropDownList3.Items.Add("3rd Party");
}
推荐答案
将您的代码放入
Put your code like
if(!IsPostBack)
{
DropDownList3.Items.Add("-");
if (callDetails.DataKey["SOURCE_REFERENCE"] != "")
{
DropDownList3.Items.Add("Source");
}
if (callDetails.DataKey["SUBJECT_REFERENCE"] != "")
{
DropDownList3.Items.Add("Subject");
}
if (callDetails.DataKey["REFERENCE"] != "")
{
DropDownList3.Items.Add("3rd Party");}
}
}
现在,您的代码将不会执行一次以上.
Now your code will not be executed more than once.
布里吉
如果是回发,我会结合使用您的代码和一些要处理的东西.
似乎可以完成工作,但是可能会有一种更流畅的方式.
还是谢谢你.
干杯
彼得
Hi Brij
I used a combination of your code and something to handle if it is a postback.
Seems to do the job but there might be a slicker way.
Thanks anyway.
Cheers
Peter
if (!IsPostBack)
{
DropDownList3.Items.Add(new ListItem("-", "-"));
if (callDetails.DataKey["SOURCE_REFERENCE"] != "")
{
DropDownList3.Items.Add(new ListItem("Source", callDetails.DataKey["SOURCE_REFERENCE"].ToString()));
Label10.Text = DropDownList3.SelectedValue.ToString();
}
if (callDetails.DataKey["SUBJECT_REFERENCE"] != "")
{
DropDownList3.Items.Add(new ListItem("Subject", callDetails.DataKey["SUBJECT_REFERENCE"].ToString()));
Label10.Text = DropDownList3.SelectedValue.ToString();
}
if (callDetails.DataKey["REFERENCE"] != "")
{
DropDownList3.Items.Add(new ListItem("3rd Party", callDetails.DataKey["REFERENCE"].ToString()));
Label10.Text = DropDownList3.SelectedValue.ToString();
}
}
if (IsPostBack)
{
if (callDetails.DataKey["SOURCE_REFERENCE"] != "")
{
Label10.Text = DropDownList3.SelectedValue.ToString();
}
if (callDetails.DataKey["SUBJECT_REFERENCE"] != "")
{
Label10.Text = DropDownList3.SelectedValue.ToString();
}
if (callDetails.DataKey["REFERENCE"] != "")
{
Label10.Text = DropDownList3.SelectedValue.ToString();
}
}
这篇关于从Codebehind下拉列表项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!