我有一个DropDownList,它通过OnInit方法填充其值。它使用HiddenField并查询数据库以自行填充:
protected void DropDown_Init(object sender, EventArgs e)
{
try
{
string value = Page.Request[choice.UniqueID] == null ? choice.Value : Page.Request[choice.UniqueID].ToString();
PricingModelDataContext ctx = new PricingModelDataContext();
var list = from p in ctx.servers
where p.server_type == (((from a in ctx.server_types
where a.name == value
select new { a.id }).Single()).id)
&& p.isActive.Value
select p;
DropDownList dList = sender as DropDownList;
dList.Items.Clear();
dList.Items.Add("");
foreach (var item in list)
{
dList.Items.Add(item.name);
}
}
catch { }
}
Everthing工作正常,直到我点击一个暂时自动填充值的按钮:
DropDown_Init(DropDown, e);
DropDown.SelectedValue = "Set Value";
现在,每当页面重新加载DropDownList时,值都不会改变(它们仍然包含从按钮单击触发的原始值)。
为什么会出现这种情况?有没有解决这个问题的方法?感谢。
答案 0 :(得分:0)
好的,我明白了。解决方案是基本填充Page_Load中的值,使用!IsPostBack使其仅填充一次,并在运行时需要更改值时调用它。
这是遵循Cam Bruce和gunr2171提到的观点。
非常感谢大家的帮助!