开发者

Asp.net Gridview - Why Are DataRowBound changes Lost on sort?

开发者 https://www.devze.com 2023-02-09 15:03 出处:网络
I am making conditional formatting changes to the data in my gridview using a RowDataBound event: void gvReg_RowDataBound(object sender, GridViewRowEventArgs e)

I am making conditional formatting changes to the data in my gridview using a RowDataBound event:

    void gvReg_RowDataBound(object sender, GridViewRowEventArgs e)
    {

       if (e.Row.RowType == DataControlRowType.DataRow)
       {

          DateTime lastUpdate DateTime.Parse(DataBinder.Eval   (e.Row.DataItem, "LAST_UPDATE");

          if (lastUpdate < DateTime.Today.AddMonths(-1))
          {

             Hyperlink hypLastUpdate = (Hyperlink)e.Row.FindControl("hypLastUpdate";
             hypLastUpdate.CssClass = "Error";
             hypLastUpdate.NavigateUrl = "http://www.someExampleErrorPage.com";

          }

       }

    }

This works, and sets the proper CssClass to the hyperlink (which makes it a jarring shade of bold red), but once the gridview is sorted (via the user clicking a column heading) the css class is reset on hypLastUpdate and it loses both it's style and associated NavigateUrl property.

The control hypLastUpdate is contained in a template field in a gridview, and it's text value is databound to a field called "LAST_UPDATE".

Is this a planned behavior (is sorting supposed to break the conditional formatting done in RowDataBound events?) or is there something I can check to make sure I am not doing something incorrectly?

I am not using the DataBind method anywhere in the code behind, and viewstate is turned on for the gridview in question.

--EDIT--

It ended up being a mistake in event handling.

I was doing:

gvReg.Sorted += {SomeEventHandler}

Inside of the page load event, but only when it wasn't a postback. This function called gvReg.DataBind after the grid view was sorted. I removed the hand开发者_高级运维ler wire up and instead added the event handler function to the OnSorted event. I guess assigned delegates to a gridview are not saved in ViewState between callbacks?


Hi here is a quick example of what I meant on my comment. This is the only way i could think of it:

    protected void gvReg_Sorting(object sender, GridViewSortEventArgs e)
    {
        GridView gridView = (GridView)sender;

        if (e.SortExpression.Length > 0)
        {
            foreach (DataControlField field in gridView.Columns)
            {
                if (field.SortExpression == e.SortExpression)
                {
                    cellIndex = gridView.Columns.IndexOf(field);
                    break;
                }
            }

            if (pSortExpression != e.SortExpression)
            {
                pSortDirection = SortDirection.Ascending;
            }
            else
            {
                pSortDirection = (pSortDirection == SortDirection.Ascending ? SortDirection.Descending : SortDirection.Ascending);
            }
            pSortExpression = e.SortExpression;
        }

        //Retrieve the table from the database 
        pSortOrder = pSortDirection == SortDirection.Ascending ? "ASC" : "DESC";
        List<Partners> partnerList = GetPartnerList();

        gvReg.DataSource = partnerList;
        gvReg.DataBind();

    }
0

精彩评论

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