SelectedIndexChanged需要花费很多时间

时间:2013-02-19 18:32:29

标签: c# asp.net

我使用下面的代码来加载我的下拉列表中的SelectedIndexChanged。 当我更改下拉列表的值时,它会花费很多时间,直到它在我的页面标签中显示值。

Label1.Text = "Review Number:" + newReview + "(for preparing of Rev." + newRev;


protected void ddlProjectDocument_SelectedIndexChanged(object sender, EventArgs e)
{
    _DataContext = new EDMSDataContext();

    int newRev;
    int newReview;

    var x = ddlProjectDocument.SelectedValue;

    var MaxRev = (from rev in _DataContext.tblTransmittalls
                  where rev.DocID.ToString() == ddlProjectDocument.SelectedValue
                  select  rev.REV).Max();

    if (MaxRev == null)
    {
        newRev = 0;
    }
    else
    {
         newRev = Convert.ToInt32(MaxRev) + 1;
    }

    var MaxReview = (from rev in _DataContext.tblFiles
                     where (rev.DocId.ToString() == ddlProjectDocument.SelectedValue) &&
                           (rev.Rev.ToString() == MaxRev)
                     select rev.Review).Max();

    if (MaxReview == null)
    {
        newReview = 1;
    }
    else
    {
         newReview = Convert.ToInt32(MaxReview) + 1;
    }

    Label1.Text = "Review Number:" + newReview + "(for preparing of Rev." + newRev;       
}

protected void ddlProjectDocument_Load(object sender, EventArgs e)
{
    var query = from p in _DataContext.tblDocuments
                orderby p.DocumentNo
                select p;

    int maxs = 0;

    foreach (tblDocument v in query)
    {
        if (v.DocumentNo.Length > maxs)
            maxs = v.DocumentNo.Length;
    }

    foreach (tblDocument vv in query)
    {
        string doctitle = vv.DocumentNo;

        for (int i = vv.DocumentNo.Length; i < maxs ; i++)
        {
             doctitle += "&nbsp;";
        }

        doctitle += "&nbsp;|&nbsp;";
        doctitle += vv.TITLE;

        // Use HtmlDecode to correctly show the spaces
        doctitle = HttpUtility.HtmlDecode(doctitle);
        ddlProjectDocument.Items.Add(new ListItem(doctitle, vv.DocId.ToString()));
    }
}

1 个答案:

答案 0 :(得分:0)

尝试以下代码。我重构它看起来更紧凑并从LINQ查询中删除了.ToString(),避免了不需要的数据:

protected void ddlProjectDocument_SelectedIndexChanged(object sender, EventArgs e)
{
    using(_DataContext = new EDMSDataContext())
    {
        int x = Convert.ToInt32(ddlProjectDocument.SelectedValue);

        var MaxRev = _DataContext.tblTransmittalls.Where(rev => rev.DocID == x).Max(rev => rev.REV);     
        int newRev = (MaxRev == null) ? 0 : Convert.ToInt32(MaxRev) + 1;   

        var MaxReview = _DataContext.tblFiles.Where(rev => rev.DocId == x && rev.Rev == MaxRev).Max(rev => rev.Review);               
        int newReview = (MaxReview == null) ? newReview = 1 : Convert.ToInt32(MaxReview) + 1;    

        Label1.Text = "Review Number:" + newReview + "(for preparing of Rev." + newRev;       
    }
}

我已将您的查询更改为lambda格式并同时处理了DataContext。