gpt4 book ai didi

c# - 回发后如何在 gridview 上保留颜色? ASP.NET C#

转载 作者:太空狗 更新时间:2023-10-29 23:01:12 27 4
gpt4 key购买 nike

我有一个 gridview,我在使用 RowDataBound 后更改了一些行的颜色....

这是我的 GridView

<asp:GridView ID="grdvEventosVendedor" runat="server" AllowPaging="True" 
AutoGenerateColumns="False" CellPadding="4" DatakeyNames="idCita"
EmptyDataText="No Hay Eventos Para Este Vendedor" ForeColor="#333333"
GridLines="None" AllowSorting="True"
onpageindexchanging="grdvEventosVendedor_PageIndexChanging"
onrowcommand="grdvEventosVendedor_RowCommand"
onsorting="grdvEventosVendedor_Sorting" CellSpacing="1"
onrowdatabound="grdvEventosVendedor_RowDataBound" >
<AlternatingRowStyle BackColor="White" ForeColor="#284775"/>

<Columns>
<asp:TemplateField HeaderText="" ItemStyle-Width="35px">
<ItemTemplate>
<asp:ImageButton ID="imgBtnEdicEvento" runat="server"
CommandArgument='<%# Eval("idCita")%>' CommandName="Edicion"
Height="32px" ImageUrl="~/img/pencil_32.png" Width="32px" />
</ItemTemplate>
</asp:TemplateField>

<asp:TemplateField HeaderText="" ItemStyle-Width="35px">
<ItemTemplate>
<asp:ImageButton ID="imgBtnDelete" runat="server"
CommandName="Borrar"
ImageUrl="~/img/1385_Disable_16x16_72.png"
onclientclick="return confirm('¿Desea eliminar el registro?');"
CommandArgument='<%# Eval("idCita")%>' />
</ItemTemplate>
</asp:TemplateField>

<asp:BoundField DataField="Cliente" HeaderText="Cliente" InsertVisible="False" ReadOnly="True" SortExpression="Cliente" ItemStyle-Width="50px" />
<asp:BoundField DataField="Empresa" HeaderText="Empresa" InsertVisible="False" ReadOnly="True" SortExpression="Empresa" ItemStyle-Width="50px"/>
<asp:BoundField DataField="Telefono" HeaderText="Telefono" InsertVisible="False" ReadOnly="True" SortExpression="Telefono" ItemStyle-Width="50px"/>
<asp:BoundField DataField="Nextel" HeaderText="Nextel" InsertVisible="False" ReadOnly="True" SortExpression="Nextel" ItemStyle-Width="50px"/>
<asp:BoundField DataField="Tipo" HeaderText="Tipo" InsertVisible="False" ReadOnly="True" SortExpression="Tipo" ItemStyle-Width="50px"/>
<asp:BoundField DataField="Descripcion" HeaderText="Descripcion" InsertVisible="False" ReadOnly="True" SortExpression="Descripcion" ItemStyle-Width="150px"/>

<asp:TemplateField HeaderText="Fecha" SortExpression="Fecha" ItemStyle-Width="50px">
<ItemTemplate>
<asp:Label ID="lblFecha" runat="server" Text='<%#Bind("Fecha", "{0:dd/MM/yyyy}")%>'>
</asp:Label>
</ItemTemplate>

<EditItemTemplate>
<asp:TextBox ID="tbxFecha" runat="server" Text='<%#Bind("Fecha","{0:dd/MM/yyyy}") %>' ValidationGroup="gpEdicionAgenda">
</asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>

<asp:BoundField DataField="HoraInicio" HeaderText="Hora" InsertVisible="False" ReadOnly="True" SortExpression="HoraInicio" ItemStyle-Width="50px"/>
<asp:BoundField DataField="Lugar" HeaderText="Lugar" InsertVisible="False" ReadOnly="True" SortExpression="Lugar" ItemStyle-Width="50px"/>
<asp:BoundField DataField="Estado" HeaderText="Estado" InsertVisible="False" ReadOnly="True" SortExpression="Estado" ItemStyle-Width="50px"/>

<%--<asp:TemplateField HeaderText="Estado" ItemStyle-Width="50px">
<ItemTemplate>
<asp:DropDownList ID="dpdListEstado" runat="server" OnSelectedIndexChanged="dpdListEstado_SelectedIndexChanged" AutoPostBack="True" CommandArgument="">
<asp:ListItem>Pendiente</asp:ListItem>
<asp:ListItem>Atendido</asp:ListItem>
</asp:DropDownList>
</ItemTemplate>
</asp:TemplateField>--%>

<asp:TemplateField HeaderText="Atender" ItemStyle-Width="35px">
<ItemTemplate>
<asp:ImageButton ID="imgBtnAtender" runat="server"
CommandArgument='<%# Eval("idCita")%>' CommandName="Atender"
ImageUrl="~/img/activar.png" Width="16px" Height="16px"
onclientclick="return confirm('¿Desea marcar como atendido este registro?');" />
</ItemTemplate>
</asp:TemplateField>

<asp:TemplateField HeaderText="CRM" ItemStyle-Width="35px">
<ItemTemplate>
<asp:ImageButton ID="imgBtnCRM" runat="server"
CommandArgument='<%# Eval("IdCliente")%>' CommandName="CRM"
ImageUrl="~/img/go.gif" Width="16px" Height="16px" />
</ItemTemplate>
</asp:TemplateField>

<asp:TemplateField HeaderText="VM" ItemStyle-Width="35px">
<ItemTemplate>
<asp:ImageButton ID="imgBtnVerMas" runat="server"
CommandArgument='<%# Eval("IdCliente")%>' CommandName="VerMas"
ImageUrl="~/img/search.png" Width="16px" Height="16px" />
</ItemTemplate>
</asp:TemplateField>
</Columns>

<EditRowStyle BackColor="#999999" Font-Size="Small" />
<FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" Font-Size="Larger" />
<PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
<RowStyle BackColor="#F7F6F3" ForeColor="#333333" HorizontalAlign="Center" Font-Size="Small" />
<SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
<SortedAscendingCellStyle BackColor="#E9E7E2" />
<SortedAscendingHeaderStyle BackColor="#506C8C" />
<SortedDescendingCellStyle BackColor="#FFFDF8" />
<SortedDescendingHeaderStyle BackColor="#6F8DAE" />
</asp:GridView>

在我的cs中我有...

    private static string strColorUrgente = "#FF525D";
private static string strColorEnCurso = "#FFDE00";
private static string strColorHoy = "#3ECF00";

这是 rowbounddata 函数

protected void grdvEventosVendedor_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowIndex >= 0 && e.Row.Cells[11].Text == "Pendiente")
{
string strFecha = (e.Row.FindControl("lblFecha") as Label).Text;
int intYear = Convert.ToInt32(strFecha.Split('/')[2]);
int intMonth = Convert.ToInt32(strFecha.Split('/')[1]);
int intDay = Convert.ToInt32(strFecha.Split('/')[0]);
DateTime dtmFecha = new DateTime(intYear, intMonth, intDay);

if (dtmFecha < DateTime.Today)
{
e.Row.BackColor = Color.FromName(strColorUrgente);
}

else if (dtmFecha == DateTime.Today)
{
DateTime dtmHoraInicio = Convert.ToDateTime(e.Row.Cells[9].Text);

if (dtmHoraInicio > DateTime.Now)
{
e.Row.BackColor = Color.FromName(strColorHoy);
}

else
{
e.Row.BackColor = Color.FromName(strColorEnCurso);
}
}
}
}

每当我进入此页面或使用搜索引擎时,我都会使用数据源刷新 gridview,当然,RowDataBound 会被触发...最终某些行的颜色会发生变化...

您会注意到在 gridview 中,在最后一列上有一个名为 imgBtnVerMas 的按钮...

按下时,会触发下面的功能

string strMensaje = "";

try
{
//Method that brings back data from a specific client (returns a datatable)
//intIdCliente was defined in the CommandArgument
DataTable dtDatosCliente = ConexionBD.GetInstanciaConexionBD().GetClienteEspecifico2(intIdCliente);
//Assigning to a string a message that needs info from the previous datatable
strMensaje += "DATOS DEL CLIENTE\\n\\nNombre:\\t" + dtDatosCliente.Rows[0]["Cliente"] + "\\nEmpresa: " + dtDatosCliente.Rows[0]["Empresa"];
}

catch
{
if (strMensaje == "")
{
strMensaje = "Hubo Un Error Al Traer Los Datos Del Cliente";
}

else
{
strMensaje += "\n\nERROR!!!!!!!";
}
}

ClientScript.RegisterStartupScript(this.GetType(), "Mensaje", "alert('" + strMensaje + "');", true);

之后,显示了消息,但是,所有行的颜色都消失了......我想知道......即使在回发后我怎样才能保持 gridview 行的颜色......

谢谢

最佳答案

您需要在 onprerender 事件上设置颜色,而不是在 RowDataBound 上设置颜色,因此在 GridView 上设置事件

onprerender="gvMyLista_PreRender"

和代码背后

protected void gvMyLista_PreRender(object sender, EventArgs e)
{
// loop your data, and fix your colors.
for (int i = 0; i < gvMyLista.Rows.Count; i++)
{
// get your data as...
// gvMyLista.Rows[i].Cells[2].Text
}
}

关于c# - 回发后如何在 gridview 上保留颜色? ASP.NET C#,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12042725/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com