ASP.NET DropDownList SelectedValue属性未立即设置 [英] ASP.NET DropDownList SelectedValue property not being set immediately

查看:195
本文介绍了ASP.NET DropDownList SelectedValue属性未立即设置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个ASP.NET Webform,我使用DropDownList控件来允许用户选择一个项目并查看相关的结果。由于某种原因,当我设置DropDownList的SelectedValue属性时,它设置的值不能立即可用。



这是我的代码:

  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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆