<asp:GridView ID="gridResult" runat="server" AutoGenerateColumns="False" class="tableShow"
                    HeaderStyle-CssClass="header" SelectedItemStyle-BackColor="#172F8C" SelectedItemStyle-ForeColor="#FFFFFF" 
                    OnRowDataBound="gridResult_RowDataBound">
                    <Columns>
                        <asp:TemplateField>
                            <EditItemTemplate>
                                <asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("PERIODNO") %>'></asp:TextBox>
                            </EditItemTemplate>
                            <ItemTemplate>
                                <asp:Label ID="Label1" runat="server" Text='<%# Bind("PERIODNO") %>'></asp:Label>
                            </ItemTemplate>
                        </asp:TemplateField>
                        <asp:TemplateField HeaderText="掲載期間" HeaderStyle-Width="70px">
                            <EditItemTemplate>
                                <asp:TextBox ID="TextBox2" runat="server" Text='<%# Bind("PERIOD_STARTDT") %>'></asp:TextBox>
                            </EditItemTemplate>
                            <ItemTemplate>
                                <asp:Label ID="Label2" runat="server" Text='<%# DateTimeFormat(Eval("PERIOD_STARTDT")) %>'></asp:Label>
                            </ItemTemplate>    
                            <HeaderStyle Width="70px" />
                        </asp:TemplateField>
                        <asp:TemplateField  HeaderStyle-Width="70px">
                            <EditItemTemplate>
                                <asp:TextBox ID="TextBox3" runat="server" Text='<%# Bind("PERIOD_ENDDT") %>'></asp:TextBox>
                            </EditItemTemplate>
                            <ItemTemplate>
                                <asp:Label ID="Label3" runat="server" Text='<%# DateTimeFormat(Eval("PERIOD_ENDDT")) %>'></asp:Label>
                            </ItemTemplate>
                            <HeaderStyle Width="70px" />
                        </asp:TemplateField>
                        <asp:TemplateField HeaderText="内容"  ItemStyle-HorizontalAlign="Left"
                               HeaderStyle-Width="600px">
                            <EditItemTemplate>
                                <asp:TextBox ID="TextBox4" runat="server" Text='<%# Bind("CONTENTS") %>'></asp:TextBox>
                            </EditItemTemplate>
                            <ItemTemplate>
                                <asp:Label ID="Label4" runat="server" Text='<%# Bind("CONTENTS") %>'></asp:Label>
                            </ItemTemplate>    
                        </asp:TemplateField>
                        <asp:TemplateField>
                            <ItemTemplate>
                                <asp:LinkButton ID="lnkSelect" runat="server" Text="" CommandName="Select" Width="0%"></asp:LinkButton>
                            </ItemTemplate>
                            <HeaderStyle Width="0px" />
                        </asp:TemplateField>
                    </Columns>
                    <HeaderStyle CssClass="header" />
                </asp:GridView>GridView绑定了4个字段,最后加了个“LinkButton”模板列,想根据这个来让鼠标移上、移开GridView的一行时变色,这样写好像不行,日期小于当前日期变颜色好像也有问题,大家帮我看下哪里出错了,代码如下:protected void gridResult_RowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow || e.Row.RowType == DataControlRowType.Header)
           {
               e.Row.Attributes.Add("onClick", GetPostBackClientHyperlink(e.Row.FindControl("lnkSelect") as LinkButton, "Select"));
                  //截止日期小于当前日期,则这行颜色变成“LightGray”
                 if (DateTime.Parse(e.Row.Cells[2].Text) < DateTime.Today)
                    {
                        e.Row.BackColor = Color.LightGray;
                    }
           }
}
    protected void gridResult_RowCommand(object sender, GridViewCommandEventArgs e)
        {
           if (e.CommandName == "Select")
           {
                   //这个事件里该写些什么?
           }
        }
            

解决方案 »

  1.   

      protected void gvFiles_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                //鼠标移动到GridView控件的任意行时,该行自动变成指定颜色
                e.Row.Attributes.Add("onmouseover", "this.style.backgroundColor='#BEC9F6';this.style.color='buttontext';this.style.cursor='default';");
                e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor='';this.style.color=''");
                //双击行打开新页
                e.Row.Attributes.Add("ondblclick", "window.open('FileInfo.aspx?id=" + e.Row.Cells[0].Text + "')");        }
        }记住是在GridView的 RowDataBound 事件里面
      

  2.   


     <script type="text/javascript">
        function fOver(obj){
            c=obj.style.backgroundColor;
            obj.style.backgroundColor="#8BB87D";
        }
        function fOut(obj){
            obj.style.backgroundColor=c;
        }
        </script>
      <tr style="border-bottom-style:none;" onmouseover="fOver(this);"
                            onmouseout="fOut(this);">
    ……   <tr style="background-color:<%#(Container.ItemIndex%2==0)?"red":"green"%>" onmouseover="fOver(this);" onmouseout="fOut(this);">
    protected void GridView2_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            int i;
            //执行循环,保证每条数据都可以更新
            for (i = -1; i < GridView2.Rows.Count; i++)
            {
                //首先判断是否是数据行
                if (e.Row.RowType == DataControlRowType.DataRow)
                {
                    //当鼠标停留时更改背景色
                    e.Row.Attributes.Add("onmouseover", "c=this.style.backgroundColor;this.style.backgroundColor='#8BB87D'");
                    //当鼠标移开时还原背景色
                    e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=c");
                }
      

  3.   

    看下我的代码,哪里需要修改下的,O(∩_∩)O谢谢!本来想仿造下面的代码写的,没有成功~ protected void gridResult_ItemDataBound(object sender, DataGridItemEventArgs e)
            {
                if (e.Item.ItemType == ListItemType.Item ||
                    e.Item.ItemType == ListItemType.AlternatingItem )
                {
                    e.Item.Attributes.Add("onClick", GetPostBackClientHyperlink(e.Item.FindControl("lnkSelect") as LinkButton, ""));                if (DateTime.Parse(e.Item.Cells[1].Text) < DateTime.Today)
                    {
                        e.Item.BackColor = Color.LightGray;
                    }
                }
            }        /// <summary>
            /// 
            /// </summary>
            protected void gridResult_ItemCommand(object source, DataGridCommandEventArgs e)
            {
                if (e.CommandName == "Select")
                {
                    gridResult.SelectedIndex = e.Item.ItemIndex;                HiddenField_Date.Value = e.Item.Cells[0].Text;
                }
            }
        }上面用的是 DataGrid,我用的是 GridView.
    HiddenField_Date 是一个隐藏控件~
      

  4.   

     <SelectedRowStyle BackColor="#669999" Font-Bold="True" ForeColor="White" />
                </asp:GridView>你的这种方式选中变色加这个属性就可以
      

  5.   

    引用 4 楼 moduofanchen 的回复:
    你是说 根据日期判断变不变色?选中一行变颜色是一件事,根据日期判断是另外件事情,截止日益小于当前日期,比如今天是5月26号,那么26号之前的数据让它们颜色暗下去,就是变灰色吧~