首页 > 解决方案 > 为什么 Gridview 在单击按钮时会丢失 BackColor

问题描述

我有一个基于其中一个列的值gridview将某些行设置为 a的行。BackColor

ASPX

<asp:GridView ID="uxTktGridView" runat="server" ShowHeaderWhenEmpty="true" CssClass="GridView" BorderStyle="Solid" onRowDataBound="uxTktGridView_RowDataBound" AutoGenerateColumns="False" OnSorting="uxTktGridView_Sorting" BackColor="White" BorderColor="#D6D2D2" BorderWidth="1px" CellPadding="3" SelectedIndex="-1" DataKeyNames="Ticket Number" AllowSorting="True"  Font-Size="Small" Width="100%" Visible="True" EnableModelValidation="True" style=" margin-top: 10px; margin-bottom: 10px;" OnSelectedIndexChanged="uxTktGridView_SelectedIndexChanged1" EnableViewState="true">
  <Columns>
    <asp:CommandField ShowSelectButton="True" SelectText="Details" ButtonType="Button" HeaderText="Select" />
    <asp:BoundField DataField="Ticket Number" HeaderText="Ticket Number" SortExpression="Ticket Number" />
    <asp:BoundField DataField="Date Of Request" HeaderText="Date Of Request" SortExpression="Date Of Request" />
    <asp:BoundField DataField="Requestor Name" HeaderText="Requestor Name" SortExpression="Requestor Name" />
    <asp:BoundField DataField="Requestor State" HeaderText="Requestor State" SortExpression="Requestor State" />
    <asp:BoundField DataField="Complexity" HeaderText="Complexity" SortExpression="Complexity" />
    <asp:BoundField DataField="Nature of Inquiry" HeaderText="Nature of Inquiry" SortExpression="Nature of Inquiry" />
    <asp:BoundField DataField="Staff" HeaderText="Staff" SortExpression="Staff" />
    <asp:BoundField DataField="Ticket Status" HeaderText="Ticket Status" SortExpression="Ticket Status" />
    <asp:BoundField DataField="Ticket Closure Date" HeaderText="Ticket Closure Date" SortExpression="Ticket Closure Date" />
  </Columns> 
  <FooterStyle BackColor="White" ForeColor="#000066" />
  <HeaderStyle BackColor="#006699" Font-Bold="True" ForeColor="White" />
  <PagerStyle BackColor="White" ForeColor="#000066" HorizontalAlign="Left" />
  <RowStyle ForeColor="#000066" />
  <SelectedRowStyle BackColor="#669999" Font-Bold="True" ForeColor="White" />
  <SortedAscendingCellStyle BackColor="#F1F1F1" />
  <SortedAscendingHeaderStyle BackColor="#007DBB" />
  <SortedDescendingCellStyle BackColor="#CAC9C9" />
  <SortedDescendingHeaderStyle BackColor="#00547E" />
</asp:GridView>

要设置BackColor,我_RowDataBound在后端使用函数:

C#

protected void uxTktGridView_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        DataRowView drv = (DataRowView)e.Row.DataItem;
        DateTime currentDate = DateTime.UtcNow.Date;
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            if (drv != null)
            {
                for (int i = 1; i < uxTktGridView.Columns.Count; i++) //index starts with 1 because the first column in the "select" button.
                {
                    if (e.Row.Cells[8].Text.Equals("OPEN"))
                    {
                        string tier = e.Row.Cells[5].Text.ToString();
                        string date = e.Row.Cells[2].Text.ToString();
                        DateTime recordDate = Convert.ToDateTime(date, CultureInfo.InvariantCulture);
                        int measureDate = (currentDate.Date - recordDate.Date).Days;
                        if (tier == "1")
                        {
                            if (measureDate >= 20)
                            {
                                e.Row.BackColor = Color.FromName("#E56E94");
                            }
                        }
                        else if (tier == "2")
                        {
                            if (measureDate >= 30)
                            {
                                e.Row.BackColor = Color.FromName("#E56E94");
                            }
                        }
                        else if (tier == "3")
                        {
                            if (measureDate >= 35)
                            {
                                e.Row.BackColor = Color.FromName("#E56E94");
                            }
                        }
                        else if (tier == "4")
                        {
                            if (measureDate >= 40)
                            {
                                e.Row.BackColor = Color.FromName("#E56E94");
                            }
                        }  
                    }
                     ...
                }
            }
        }
    }

我有两个相同问题的问题。如果我 1) 单击一个按钮或 2) 单击其中一行 - 网格变白并且不应用BackColor设置。我猜这是一些与 PostBack 相关的问题。我尝试添加AutoPostBack="false"到按钮以及网格中。该行为仍然存在。我也尝试过添加 'EnableViewState="false" (based on a suggestion) to thegridview . When I click a button, this basically makes mygridview` 消失。有关如何解决此问题的任何建议?

标签: asp.netgridviewwebformspostbackrowdatabound

解决方案


了解 WebForms 中的事件顺序对这类事情最有帮助。例如,Page 和 Control 可以与事件名称互换。事件顺序如下:

  1. Page_Init(...)(没有可用的 ViewState 数据)
  2. Page_Load(...) (ViewState 可用)
  3. 控制事件(例如 Click、RowSelect 等)
  4. 预渲染 (...)

If you are not databinding the grid after the postback (it is best to call DataBind() in PreRender after all of your page logic has completed) and do not have EnableViewState set to true, then you will lose this information posted in the grid and will be unable to access it in the postback. The ViewState is assigned between Init and Load and allows you to access properties of the controls. If you're calling DataBind() in Page_Load, then your control events may yield unpredictable results as the underlying data may differ.

In other words, if you want to use the GridView as you have it defined, EnableViewState, at least for the GridView, needs to be true. Then, you should not DataBind it again during the postback

void Page_PreRender(object sender, EventArgs e)
{
    if (!Page.IsPostback)
    {
        DataBind();
    }
}

and the state should effectively be preserved. Also, don't change the value of your AutoPostBack property.


推荐阅读