【发布时间】:2019-09-22 01:00:39
【问题描述】:
我有一个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");
}
}
}
...
}
}
}
}
我有两个相同的问题。如果我 1) 单击一个按钮或 2) 单击其中一行 - 网格变白并且不应用 BackColor 设置。我猜这是一些与 PostBack 相关的问题。我尝试将AutoPostBack="false" 添加到按钮和网格中。该行为仍然存在。我也试过添加 'EnableViewState="false"(based on a suggestion) to thegridview. When I click a button, this basically makes mygridview` 消失。有关如何解决此问题的任何建议?
【问题讨论】:
标签: asp.net gridview webforms postback rowdatabound