为什么 Gridview 在单击按钮时会丢失 BackColor
Why does Gridview lose BackColor on button click
我有一个 gridview
,它根据其中一列的值将某些行设置为 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");
}
}
}
...
}
}
}
}
同一个问题我有 2 个问题。如果我 1) 单击一个按钮或 2) 单击其中一行 - 网格变白并且不应用 BackColor
设置。我猜这是一些 PostBack 相关的问题。我尝试将 AutoPostBack="false"
添加到按钮和网格中。该行为仍然发生。我还尝试添加 'EnableViewState="false"(based on a suggestion) to the
gridview. When I click a button, this basically makes my
gridview` 消失。关于如何解决此问题的任何建议?
了解 WebForms 中的事件顺序对于此类事情最有帮助。例如,Page 和 Control 可以与事件名称互换。活动顺序如下:
- Page_Init(...)(没有可用的 ViewState 数据)
- Page_Load(...)(ViewState 可用)
- 控件事件(例如单击、行选择等)
- 预渲染 (...)
如果您在回发后没有对网格进行数据绑定(最好在所有页面逻辑完成后在 PreRender 中调用 DataBind())并且没有将 EnableViewState
设置为 true,那么您将丢失发布在网格中的此信息,并且将无法在回发中访问它。 ViewState 在 Init 和 Load 之间分配,并允许您访问控件的属性。如果您在 Page_Load
中调用 DataBind()
,那么您的控制事件可能会产生不可预测的结果,因为基础数据可能不同。
换句话说,如果您想按定义使用 GridView,EnableViewState(至少对于 GridView)需要为真。然后,你不应该在回发期间再次对其进行 DataBind
void Page_PreRender(object sender, EventArgs e)
{
if (!Page.IsPostback)
{
DataBind();
}
}
并且应该有效地保留状态。另外,不要更改 AutoPostBack
属性 的值。
我有一个 gridview
,它根据其中一列的值将某些行设置为 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");
}
}
}
...
}
}
}
}
同一个问题我有 2 个问题。如果我 1) 单击一个按钮或 2) 单击其中一行 - 网格变白并且不应用 BackColor
设置。我猜这是一些 PostBack 相关的问题。我尝试将 AutoPostBack="false"
添加到按钮和网格中。该行为仍然发生。我还尝试添加 'EnableViewState="false"(based on a suggestion) to the
gridview. When I click a button, this basically makes my
gridview` 消失。关于如何解决此问题的任何建议?
了解 WebForms 中的事件顺序对于此类事情最有帮助。例如,Page 和 Control 可以与事件名称互换。活动顺序如下:
- Page_Init(...)(没有可用的 ViewState 数据)
- Page_Load(...)(ViewState 可用)
- 控件事件(例如单击、行选择等)
- 预渲染 (...)
如果您在回发后没有对网格进行数据绑定(最好在所有页面逻辑完成后在 PreRender 中调用 DataBind())并且没有将 EnableViewState
设置为 true,那么您将丢失发布在网格中的此信息,并且将无法在回发中访问它。 ViewState 在 Init 和 Load 之间分配,并允许您访问控件的属性。如果您在 Page_Load
中调用 DataBind()
,那么您的控制事件可能会产生不可预测的结果,因为基础数据可能不同。
换句话说,如果您想按定义使用 GridView,EnableViewState(至少对于 GridView)需要为真。然后,你不应该在回发期间再次对其进行 DataBind
void Page_PreRender(object sender, EventArgs e)
{
if (!Page.IsPostback)
{
DataBind();
}
}
并且应该有效地保留状态。另外,不要更改 AutoPostBack
属性 的值。