开发者

Moving rows in dynamically created table not working (ASP.NET/LINQ/C#)

I'm maintaining a dynamically generated table in ASP.NET, which allows users to move items up and down in the table. What this essentially does behind the scenes is just swaps around the 'sortorder' which the table is sorted by. I have checks to see if a row is the first/last column, and if so only display up/down (whatever's relevant).

An issue has crept up now and I have no idea why. For some reason, most of the time (not all of the time strangely) the up and down buttons don't work. Using 'Up' as an example, the current record does get set to the 'above' rows sortorder, but the 'above' row doesn't take the below rows sort order.

Now I've set my breakpoints throughout my code, and strangely everything gets set fine. It's just when I call SubmitChanges() to the LINQ data context, that it seems to put the wrong value into the database.

Also what's strange is that i think that if you click it again, it actually works. But it doesn't work properly as the number is still out by a bit.

Here's my code:

if (Request.QueryString["dir"] != null) {
    if (Request.QueryString["dir"] == "up") {
        var currentrecordup = (from s in dc.InvoiceItems 
            where Request.QueryString["val"] == s.id.ToString() 
            select s).Single();
        int valsortidup = (int)currentrecordup.sortorder;
        var prevrecordup = (from s in dc.InvoiceItems 
            where s.sortorder < valsortidup 
            orderby s.sortorder descending 
            select s).First();
        int prevsortidup = (int)prevrecordup.sortorder;
        int tempval = valsortidup;
        currentrecordup.sortorder = prevsortidup;
        prevrecordup.sortorder = tempval;
    }
    else if (Request.QueryString["dir"] == "down") {
        var currentrecorddown = (from s in dc.InvoiceItems 
            where Request.QueryString["val"] == s.id.ToString() 
            select s).Single();
        int valsortiddown = 开发者_如何转开发(int)currentrecorddown.sortorder;
        var nextrecorddown = (from s in dc.InvoiceItems 
            where s.sortorder > valsortiddown 
            orderby s.sortorder ascending 
            select s).First();
        int nextsortiddown = (int)nextrecorddown.sortorder;
        int tempvaldown = valsortiddown;
        currentrecorddown.sortorder = nextsortiddown;
        nextrecorddown.sortorder = tempvaldown;
    }
    dc.SubmitChanges();
    Response.Redirect("EditInvoice.aspx?id=" + Request.QueryString["id"]);
}

If it's any help, the sortorder column in my table is an int field that allows nulls. Thanks


Very, very stupid problem. Not checking if the line in the column is an item of the same 'invoiceid' - duh!

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜