jroyce
jroyce

Reputation: 2148

Gridview inside UpdatePanel Deleting Records, Updating Wrong Rows

I have several gridviews each inside their own UpdatePanels. I have buttons that filter the data but have noticed unusual updating and deletions at times that I cannot detect the source of. One thing I have isolated is that this occurs when I sort a gridviews data in one UpdatePanel and then try to update another UpdatePanel.

(My gridviews all get their data from the same DataLoad procedure that populates each gridview based on different Linq queries.)

I resolved most of the unusual updates by calling .Update() on all UpdatePanels after my sorting function so all gridviews are "refreshed". But then there are still some instances were data is getting updated and I can not isolate the source.

It looks like data outside one UpdatePanel is actually being updated "behind the scenes" and only cached data is on the screen and then when I edit what is on the screen, wrong data gets updated because it was not refreshed.

I am guessing that my sorting strategy is incorrect as I am sorting all gridviews instead of just that specific gridview when user clicks a column header.

Here is my sorting procedure that is called by each gridview:

protected void TaskGridView_Sorting(object sender, GridViewSortEventArgs e)
{               
    string sortExp = ViewState["SortExpression"] as string;     
    string sortDir = ViewState["SortDirection"] as string;
    if(sortDir == "asc" & sortExp == e.SortExpression.ToString())
        ViewState["SortDirection"] = "desc";
    else
        ViewState["SortDirection"] = "asc";
    ViewState["SortExpression"] = e.SortExpression.ToString();

    if(searchCol != "" && searchText != "")
        DataGrid_Load(DAL.Search_reg_log(OrgText.Text, searchText, searchCol), "reg");
    else
        DataGrid_Load(DAL.reg_log(HeadText.Text, OrgText.Text), "reg");     

    UpdatePanels();

}

I am new to using Ajax and UpdatePanels and would like any direction on resolving this situation.

Upvotes: 2

Views: 286

Answers (2)

VDWWD
VDWWD

Reputation: 35514

There is nothing wrong with your code you posted. The problem is not in that snippet and has nothing to do with ViewState or an UpdatePanel. If you are unsure what is happening it helps to visualize it. Either by debugging or just display the result in a Label and see if it is what you expect it to be.

protected void TaskGridView_Sorting(object sender, GridViewSortEventArgs e)
{
    //load the previous sorting settings
    string sortExp = ViewState["SortExpression"] as string;
    string sortDir = ViewState["SortDirection"] as string;

    //reverse the direction if the column is the same as the previous sort
    if (sortDir == "asc" & sortExp == e.SortExpression.ToString())
        ViewState["SortDirection"] = "desc";
    else
        ViewState["SortDirection"] = "asc";

    //put the current sort column in the viewstate
    ViewState["SortExpression"] = e.SortExpression.ToString();

    //show sorting result in a literal for testing
    Literal1.Text = ViewState["SortExpression"] + " " + ViewState["SortDirection"];

    //rebind data
    if (searchCol != "" && searchText != "")
        DataGrid_Load(DAL.Search_reg_log(OrgText.Text, searchText, searchCol), "reg");
    else
        DataGrid_Load(DAL.reg_log(HeadText.Text, OrgText.Text), "reg");

    //update the updatepanels
    UpdatePanels();
}

Upvotes: 3

Use properties for ViewState it could be caused by Post Process of your PostBack

private string p_SortExp
    {
        get
        {
            if (ViewState["p_SortExp"] != null)
            {
                return ViewState["dtSupplierList"] as string;
            }
            else
            {
                return null;
            }
        }
        set
        {
            ViewState["p_SortExp"] = value;
        }
    }

then in your function try calling the properties and remember to set them at the right functions for better usability

Upvotes: 2

Related Questions