ASP.NET DropDownList SelectedValue属性未立即设置 [英] ASP.NET DropDownList SelectedValue property not being set immediately
问题描述
这是我的代码:
protected void Page_Load(object sender, EventArgs e)
{
if(!Page.IsPostBack)
{
DropDownList1.SelectedValue =5;
BindView();
}
}
protected void BindActivities()
{
DataClassesDataContext dc = new DataClassesDataContext();
var query = from dc.Activities中的活动
其中activity.AssignedTo == Convert.ToInt32(DropDownList1.SelectedValue);
GridView1.DataSource = query;
GridView1.DataBind();
}
在上一个代码中,我收到一个DropDownList1.SelectedValue为null的错误。最重要的是,如果我注释掉使用DropDownList1.SelectedValue的代码,并让页面加载,DropDownList1实际上设置为值5.所以它看起来像是正确设置,但不是立即可用。调试器确认DropDownList.SelectedValue在设置代码行之后不会立即设置为5。
任何想法在这里发生什么?
您是否在绑定下拉列表之前设置值?
I have an ASP.NET webform on which I use a DropDownList control to allow the user to select an item and see related results. For some reason when I set the SelectedValue property of the DropDownList the value it's set to is not immediately available.
Here's my code:
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
DropDownList1.SelectedValue = "5";
BindView();
}
}
protected void BindActivities()
{
DataClassesDataContext dc = new DataClassesDataContext();
var query = from activity in dc.Activities
where activity.AssignedTo == Convert.ToInt32(DropDownList1.SelectedValue);
GridView1.DataSource = query;
GridView1.DataBind();
}
In the previous code I get an error that DropDownList1.SelectedValue is null. The wierd thing is that if I comment out the code that uses DropDownList1.SelectedValue and let the page load, DropDownList1 is actually set to value 5. So it looks like it's getting set correctly but is just not immediately available. The debugger confirms that DropDownList.SelectedValue is not set to 5 immediately after the line of code that sets it.
Any ideas what is going on here?
Are you setting the value before you have bound the dropdown list?
这篇关于ASP.NET DropDownList SelectedValue属性未立即设置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!