1

我已经使用下面的代码来加载和 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()));
    }
}
4

1 回答 1

0

试试下面的代码。我已经对其进行了重构,使其看起来更紧凑,并.ToString()从 LINQ 查询中删除了 ,避免拉下不需要的数据:

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。

于 2013-02-19T18:54:10.723 回答