在做GridView绑定的过程中为了实现单击行,此行变色的时候
发现一个怪现象
就是在单击零行时,该行的颜色没有任何的变化,不知道为什么
下面是我的代码:劳驾高手看看前台:
asp:GridView ID="gvLeveling" runat="server" AutoGenerateColumns="False"
BorderColor="#000000" BorderWidth="1px" CellPadding="3" meta:resourcekey="gvLevelingEmpty" OnRowCreated="gvLeveling_RowCreated" OnRowDataBound="gvLeveling_DataBound">
<Columns>
<asp:BoundField ItemStyle-HorizontalAlign="Center" ItemStyle-BorderColor="#000000" DataField="" />
<asp:BoundField ItemStyle-HorizontalAlign="Center" ItemStyle-BorderColor="#000000" DataField="" />
<asp:BoundField ItemStyle-HorizontalAlign="Center" ItemStyle-BorderColor="#000000" DataField="" />
<asp:BoundField ItemStyle-HorizontalAlign="Center" ItemStyle-BorderColor="#000000" DataField="project_name" />
<asp:BoundField ItemStyle-HorizontalAlign="Center" ItemStyle-BorderColor="#000000" DataField="project_cd" />
<asp:BoundField ItemStyle-HorizontalAlign="Center" ItemStyle-BorderColor="#000000" DataField="project_importance" />
<asp:BoundField ItemStyle-HorizontalAlign="Center" ItemStyle-BorderColor="#000000" DataField="pstart_date" />
<asp:BoundField ItemStyle-HorizontalAlign="Center" ItemStyle-BorderColor="#000000" DataField="pfinish_date" />
<asp:BoundField ItemStyle-HorizontalAlign="Center" ItemStyle-BorderColor="#000000" DataField="status" />
<asp:BoundField ItemStyle-HorizontalAlign="Center" ItemStyle-BorderColor="#000000" DataField="lstart_date" />
<asp:BoundField ItemStyle-HorizontalAlign="Center" ItemStyle-BorderColor="#000000" DataField="lfinish_date" />
<asp:BoundField ItemStyle-HorizontalAlign="Center" ItemStyle-BorderColor="#000000" DataField="message" />
</Columns>
</asp:GridView>
后台:protected void gvLeveling_RowCreated(object sender, GridViewRowEventArgs e)
{
switch (e.Row.RowType)
{
case DataControlRowType.Header:
TableCellCollection tcHeader = e.Row.Cells;
tcHeader.Clear();
tcHeader.Add(new TableHeaderCell());
tcHeader[0].Attributes.Add("rowspan", "2");
tcHeader[0].Attributes.Add("width", "2%");
tcHeader[0].Attributes.Add("class", "td_col pink");
tcHeader[0].Text = "";
tcHeader.Add(new TableHeaderCell());
tcHeader[1].Attributes.Add("rowspan", "2");
tcHeader[1].Attributes.Add("width", "5%");
tcHeader[1].Attributes.Add("align", "center");
tcHeader[1].Attributes.Add("class", "td_col pink");
tcHeader[1].Text = this.GetLocalResourceObject("lblupdate").ToString();
tcHeader.Add(new TableHeaderCell());
tcHeader[2].Attributes.Add("rowspan", "2");
tcHeader[2].Attributes.Add("width", "5%");
tcHeader[2].Attributes.Add("align", "center");
tcHeader[2].Attributes.Add("class", "td_col pink");
tcHeader[2].Text = this.GetLocalResourceObject("lblReduction").ToString();
tcHeader.Add(new TableHeaderCell());
tcHeader[3].Attributes.Add("rowspan", "2");
tcHeader[3].Attributes.Add("align", "center");
tcHeader[3].Attributes.Add("class", "td_col pink");
tcHeader[3].Text = this.GetLocalResourceObject("lblProjectName").ToString();
tcHeader.Add(new TableHeaderCell());
tcHeader[4].Attributes.Add("rowspan", "2");
tcHeader[4].Attributes.Add("align", "center");
tcHeader[4].Attributes.Add("class", "td_col pink");
tcHeader[4].Text = this.GetLocalResourceObject("lblProjectCD").ToString();
tcHeader.Add(new TableHeaderCell());
tcHeader[5].Attributes.Add("rowspan", "2");
tcHeader[5].Attributes.Add("align", "center");
tcHeader[5].Attributes.Add("class", "td_col pink");
tcHeader[5].Text = this.GetLocalResourceObject("lblValuableFlag").ToString();
tcHeader.Add(new TableHeaderCell());
tcHeader[6].Attributes.Add("colspan", "2");
tcHeader[6].Attributes.Add("align", "center");
tcHeader[6].Attributes.Add("class", "td_col pink");
tcHeader[6].Text = this.GetLocalResourceObject("lblProjectDuring").ToString();
tcHeader.Add(new TableHeaderCell());
tcHeader[7].Attributes.Add("rowspan", "2");
tcHeader[7].Attributes.Add("align", "center");
tcHeader[7].Attributes.Add("class", "td_col pink");
tcHeader[7].Text = this.GetLocalResourceObject("lblLevelingStatus").ToString();
tcHeader.Add(new TableHeaderCell());
tcHeader[8].Attributes.Add("colspan", "2");
tcHeader[8].Attributes.Add("align", "center");
tcHeader[8].Attributes.Add("class", "td_col pink");
tcHeader[8].Text = this.GetLocalResourceObject("lblLeveling").ToString();
tcHeader.Add(new TableHeaderCell());
tcHeader[9].Attributes.Add("rowspan", "2");
tcHeader[9].Attributes.Add("align", "center");
tcHeader[9].Attributes.Add("class", "td_col pink");
tcHeader[9].Text = this.GetLocalResourceObject("lblMessage").ToString() + "</th></tr><tr>"; tcHeader.Add(new TableHeaderCell());
tcHeader[10].Attributes.Add("align", "center");
tcHeader[10].Attributes.Add("class", "td_col pink");
tcHeader[10].Text = this.GetLocalResourceObject("lblProStartDate").ToString();
tcHeader.Add(new TableHeaderCell());
tcHeader[11].Attributes.Add("align", "center");
tcHeader[11].Attributes.Add("class", "td_col pink");
tcHeader[11].Text = this.GetLocalResourceObject("lblProFinishDate").ToString();
tcHeader.Add(new TableHeaderCell());
tcHeader[12].Attributes.Add("align", "center");
tcHeader[12].Attributes.Add("class", "td_col pink");
tcHeader[12].Text = this.GetLocalResourceObject("lblLevelingStartDate").ToString();
tcHeader.Add(new TableHeaderCell());
tcHeader[13].Attributes.Add("align", "center");
tcHeader[13].Attributes.Add("class", "td_col pink");
tcHeader[13].Text = this.GetLocalResourceObject("lblLevelingFinishDate").ToString();
break;
}
}protected void gvLeveling_DataBound(object sender, GridViewRowEventArgs e)
{
for (int i = 0; i < gvLeveling.Rows.Count; i++)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
e.Row.Attributes.Add("onclick", "if(window.oldtr!=null){window.oldtr.runtimeStyle.cssText='';}this.runtimeStyle.cssText='background-color:#e6c5fc';window.oldtr=this");
}
}
}以上都是部分代码
发现一个怪现象
就是在单击零行时,该行的颜色没有任何的变化,不知道为什么
下面是我的代码:劳驾高手看看前台:
asp:GridView ID="gvLeveling" runat="server" AutoGenerateColumns="False"
BorderColor="#000000" BorderWidth="1px" CellPadding="3" meta:resourcekey="gvLevelingEmpty" OnRowCreated="gvLeveling_RowCreated" OnRowDataBound="gvLeveling_DataBound">
<Columns>
<asp:BoundField ItemStyle-HorizontalAlign="Center" ItemStyle-BorderColor="#000000" DataField="" />
<asp:BoundField ItemStyle-HorizontalAlign="Center" ItemStyle-BorderColor="#000000" DataField="" />
<asp:BoundField ItemStyle-HorizontalAlign="Center" ItemStyle-BorderColor="#000000" DataField="" />
<asp:BoundField ItemStyle-HorizontalAlign="Center" ItemStyle-BorderColor="#000000" DataField="project_name" />
<asp:BoundField ItemStyle-HorizontalAlign="Center" ItemStyle-BorderColor="#000000" DataField="project_cd" />
<asp:BoundField ItemStyle-HorizontalAlign="Center" ItemStyle-BorderColor="#000000" DataField="project_importance" />
<asp:BoundField ItemStyle-HorizontalAlign="Center" ItemStyle-BorderColor="#000000" DataField="pstart_date" />
<asp:BoundField ItemStyle-HorizontalAlign="Center" ItemStyle-BorderColor="#000000" DataField="pfinish_date" />
<asp:BoundField ItemStyle-HorizontalAlign="Center" ItemStyle-BorderColor="#000000" DataField="status" />
<asp:BoundField ItemStyle-HorizontalAlign="Center" ItemStyle-BorderColor="#000000" DataField="lstart_date" />
<asp:BoundField ItemStyle-HorizontalAlign="Center" ItemStyle-BorderColor="#000000" DataField="lfinish_date" />
<asp:BoundField ItemStyle-HorizontalAlign="Center" ItemStyle-BorderColor="#000000" DataField="message" />
</Columns>
</asp:GridView>
后台:protected void gvLeveling_RowCreated(object sender, GridViewRowEventArgs e)
{
switch (e.Row.RowType)
{
case DataControlRowType.Header:
TableCellCollection tcHeader = e.Row.Cells;
tcHeader.Clear();
tcHeader.Add(new TableHeaderCell());
tcHeader[0].Attributes.Add("rowspan", "2");
tcHeader[0].Attributes.Add("width", "2%");
tcHeader[0].Attributes.Add("class", "td_col pink");
tcHeader[0].Text = "";
tcHeader.Add(new TableHeaderCell());
tcHeader[1].Attributes.Add("rowspan", "2");
tcHeader[1].Attributes.Add("width", "5%");
tcHeader[1].Attributes.Add("align", "center");
tcHeader[1].Attributes.Add("class", "td_col pink");
tcHeader[1].Text = this.GetLocalResourceObject("lblupdate").ToString();
tcHeader.Add(new TableHeaderCell());
tcHeader[2].Attributes.Add("rowspan", "2");
tcHeader[2].Attributes.Add("width", "5%");
tcHeader[2].Attributes.Add("align", "center");
tcHeader[2].Attributes.Add("class", "td_col pink");
tcHeader[2].Text = this.GetLocalResourceObject("lblReduction").ToString();
tcHeader.Add(new TableHeaderCell());
tcHeader[3].Attributes.Add("rowspan", "2");
tcHeader[3].Attributes.Add("align", "center");
tcHeader[3].Attributes.Add("class", "td_col pink");
tcHeader[3].Text = this.GetLocalResourceObject("lblProjectName").ToString();
tcHeader.Add(new TableHeaderCell());
tcHeader[4].Attributes.Add("rowspan", "2");
tcHeader[4].Attributes.Add("align", "center");
tcHeader[4].Attributes.Add("class", "td_col pink");
tcHeader[4].Text = this.GetLocalResourceObject("lblProjectCD").ToString();
tcHeader.Add(new TableHeaderCell());
tcHeader[5].Attributes.Add("rowspan", "2");
tcHeader[5].Attributes.Add("align", "center");
tcHeader[5].Attributes.Add("class", "td_col pink");
tcHeader[5].Text = this.GetLocalResourceObject("lblValuableFlag").ToString();
tcHeader.Add(new TableHeaderCell());
tcHeader[6].Attributes.Add("colspan", "2");
tcHeader[6].Attributes.Add("align", "center");
tcHeader[6].Attributes.Add("class", "td_col pink");
tcHeader[6].Text = this.GetLocalResourceObject("lblProjectDuring").ToString();
tcHeader.Add(new TableHeaderCell());
tcHeader[7].Attributes.Add("rowspan", "2");
tcHeader[7].Attributes.Add("align", "center");
tcHeader[7].Attributes.Add("class", "td_col pink");
tcHeader[7].Text = this.GetLocalResourceObject("lblLevelingStatus").ToString();
tcHeader.Add(new TableHeaderCell());
tcHeader[8].Attributes.Add("colspan", "2");
tcHeader[8].Attributes.Add("align", "center");
tcHeader[8].Attributes.Add("class", "td_col pink");
tcHeader[8].Text = this.GetLocalResourceObject("lblLeveling").ToString();
tcHeader.Add(new TableHeaderCell());
tcHeader[9].Attributes.Add("rowspan", "2");
tcHeader[9].Attributes.Add("align", "center");
tcHeader[9].Attributes.Add("class", "td_col pink");
tcHeader[9].Text = this.GetLocalResourceObject("lblMessage").ToString() + "</th></tr><tr>"; tcHeader.Add(new TableHeaderCell());
tcHeader[10].Attributes.Add("align", "center");
tcHeader[10].Attributes.Add("class", "td_col pink");
tcHeader[10].Text = this.GetLocalResourceObject("lblProStartDate").ToString();
tcHeader.Add(new TableHeaderCell());
tcHeader[11].Attributes.Add("align", "center");
tcHeader[11].Attributes.Add("class", "td_col pink");
tcHeader[11].Text = this.GetLocalResourceObject("lblProFinishDate").ToString();
tcHeader.Add(new TableHeaderCell());
tcHeader[12].Attributes.Add("align", "center");
tcHeader[12].Attributes.Add("class", "td_col pink");
tcHeader[12].Text = this.GetLocalResourceObject("lblLevelingStartDate").ToString();
tcHeader.Add(new TableHeaderCell());
tcHeader[13].Attributes.Add("align", "center");
tcHeader[13].Attributes.Add("class", "td_col pink");
tcHeader[13].Text = this.GetLocalResourceObject("lblLevelingFinishDate").ToString();
break;
}
}protected void gvLeveling_DataBound(object sender, GridViewRowEventArgs e)
{
for (int i = 0; i < gvLeveling.Rows.Count; i++)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
e.Row.Attributes.Add("onclick", "if(window.oldtr!=null){window.oldtr.runtimeStyle.cssText='';}this.runtimeStyle.cssText='background-color:#e6c5fc';window.oldtr=this");
}
}
}以上都是部分代码
开始你的window.oldtr是NULL吧
juedaihuaihuai(绝代坏坏(beyond myself))
我也试过,不用畸形表头,
该方式也一样在第一行不能变色
是等于NULL因为只有window.oldtr是NULL的时候才给该行变色