【问题标题】:ASP.NET GridView - Cannot set the colour of the row during databind?ASP.NET GridView - 在数据绑定期间无法设置行的颜色?
【发布时间】:2010-01-28 20:52:20
【问题描述】:

这让我发疯了!这是我用 Datagrid 做了 100 多次的事情。我现在正在使用 Gridview,但我无法弄清楚。

我有这个网格:

<asp:GridView AutoGenerateColumns="false" runat="server" ID="gvSelect" CssClass="GridViewStyle"
        GridLines="None" ShowHeader="False" PageSize="20" AllowPaging="True">
        <Columns>
            <asp:TemplateField>
                <ItemTemplate>
                    <asp:Label runat="server" ID="lbldas" Text="blahblah"></asp:Label>
                </ItemTemplate>
            </asp:TemplateField>
        </Columns>

在我尝试过的 RowDataBound 期间:

Protected Sub gvSelect_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles gvSelect.RowCreated
    If e.Row.RowType = DataControlRowType.DataRow Then
        e.Row.Attributes.Add("onMouseOver", "this.style.backgroundColor='lightgrey'")
    End If
End Sub

它从不设置行背景色。我已经成功使用:

gridrow.Cells(0).BackColor = Drawing.Color.Blue

但是做整行?不!这让我发疯了。有没有人可以解决我的问题?

为了好玩,我把它放在同一个页面上:

<asp:DataGrid AutoGenerateColumns="false" runat="server" ID="dgSelect" GridLines="None"
        ShowHeader="False" PageSize="20" AllowPaging="True">
        <Columns>
            <asp:TemplateColumn>
                <ItemTemplate>
                    <asp:Label runat="server" ID="lbldas" Text="blahblah"></asp:Label>
                </ItemTemplate>
            </asp:TemplateColumn>
        </Columns>
    </asp:DataGrid>

在我放的 ItemDataBound 中:

If Not e.Item.ItemType = ListItemType.Header And Not e.Item.ItemType = ListItemType.Footer Then
        e.Item.Attributes.Add("onMouseOver", "this.style.backgroundColor='lightgrey'")
End If

它按预期工作.. 那么我在 Gridview 上做错了什么?

**更新 ************************

我想我会发布生成的 HTML 以表明任何样式都不会影响这一点。

这是gridview html:

<div class="AspNet-GridView" id="gvSelect"> 
<table cellpadding="0" cellspacing="0" summary=""> 
    <tbody> 
        <tr> 
            <td> 
                <span id="gvSelect_ctl02_lbldas">blahblah</span> 
            </td> 
        </tr> 
    </tbody> 
</table> 
 </div>

这是生成的 Datagrid HTML:

<table cellspacing="0" border="0" id="dgSelect" style="border-collapse:collapse;"> 
<tr onMouseOver="this.style.backgroundColor='lightgrey'"> 
    <td> 
        <span id="dgSelect_ctl03_lbldas">blahblah</span> 
            </td> 
</tr>
 </table> 

见.. 主要区别在于标签。它永远不会在gridview中设置..我不知道为什么..我已经跟踪过它..代码运行了..:S

【问题讨论】:

  • 我从顶部尝试了您的代码,它工作正常。您是否检查过是否有样式规则在单元格上设置 bg 颜色以覆盖行上设置的任何 bg 颜色?
  • 我从页面中删除了所有样式.. 由于数据网格工作正常,它没有任何意义.. 有没有可能我的数据网格版本不好?是否有我不知道的 Visual Studio 2008 的服务包或修补程序?

标签: asp.net gridview


【解决方案1】:

这实际上应该在 RowCreatedEvent 期间完成。刚刚测试了以下代码,它在鼠标悬停时突出显示/取消突出显示一行的效果非常好。

Private Sub GridView1_RowCreated(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridView1.RowCreated
    If e.Row.RowType = DataControlRowType.DataRow Then
        e.Row.Attributes.Add("onmouseover", "this.style.backgroundColor='#ccaaaa';")
        e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor='#ffffff';")
    End If
End Sub

编辑:附加 html 输出(注意:在带有 RowCreated 的 VB 和 C# 中工作 - 相同的输出)

<div>
  <table cellspacing="0" rules="all" border="1" id="GridView1" style="border-collapse:collapse;">
    <tr>
      <th scope="col">ST_CD</th><th scope="col">ST_CD_ALPHA</th><th scope="col">ST_DESC</th>
    </tr><tr onmouseover="this.style.backgroundColor='#ccaaaa';" onmouseout="this.style.backgroundColor='#ffffff';">
      <td>04</td><td>CA</td><td>CALIFORNIA                    </td>
    </tr><tr onmouseover="this.style.backgroundColor='#ccaaaa';" onmouseout="this.style.backgroundColor='#ffffff';">
      <td>34</td><td>OH</td><td>OHIO                          </td>
    </tr><tr onmouseover="this.style.backgroundColor='#ccaaaa';" onmouseout="this.style.backgroundColor='#ffffff';">
      <td>41</td><td>TN</td><td>TENNESSEE                     </td>
    </tr><tr onmouseover="this.style.backgroundColor='#ccaaaa';" onmouseout="this.style.backgroundColor='#ffffff';">
      <td>42</td><td>TX</td><td>TEXAS                         </td>
    </tr><tr onmouseover="this.style.backgroundColor='#ccaaaa';" onmouseout="this.style.backgroundColor='#ffffff';">
      <td>45</td><td>VA</td><td>VIRGINIA                      </td>
    </tr><tr onmouseover="this.style.backgroundColor='#ccaaaa';" onmouseout="this.style.backgroundColor='#ffffff';">
      <td>46</td><td>WA</td><td>WASHINGTON                    </td>
    </tr><tr onmouseover="this.style.backgroundColor='#ccaaaa';" onmouseout="this.style.backgroundColor='#ffffff';">
      <td>49</td><td>WY</td><td>WYOMING                       </td>
    </tr><tr onmouseover="this.style.backgroundColor='#ccaaaa';" onmouseout="this.style.backgroundColor='#ffffff';">
      <td>14</td><td>IA</td><td>IOWA                          </td>
    </tr><tr onmouseover="this.style.backgroundColor='#ccaaaa';" onmouseout="this.style.backgroundColor='#ffffff';">
      <td>24</td><td>MO</td><td>MISSOURI                      </td>
    </tr><tr onmouseover="this.style.backgroundColor='#ccaaaa';" onmouseout="this.style.backgroundColor='#ffffff';">
      <td>40</td><td>SD</td><td>SOUTH DAKOTA                  </td>
    </tr><tr onmouseover="this.style.backgroundColor='#ccaaaa';" onmouseout="this.style.backgroundColor='#ffffff';">
      <td>43</td><td>UT</td><td>UTAH                          </td>
    </tr><tr onmouseover="this.style.backgroundColor='#ccaaaa';" onmouseout="this.style.backgroundColor='#ffffff';">
      <td>44</td><td>VT</td><td>VERMONT                       </td>
    </tr><tr onmouseover="this.style.backgroundColor='#ccaaaa';" onmouseout="this.style.backgroundColor='#ffffff';">
      <td>47</td><td>WV</td><td>WEST VIRGINIA                 </td>
    </tr><tr onmouseover="this.style.backgroundColor='#ccaaaa';" onmouseout="this.style.backgroundColor='#ffffff';">
      <td>48</td><td>WI</td><td>WISCONSIN                     </td>
    </tr><tr onmouseover="this.style.backgroundColor='#ccaaaa';" onmouseout="this.style.backgroundColor='#ffffff';">
      <td>54</td><td>AK</td><td>ALASKA                        </td>
    </tr>
  </table>
</div>

编辑:这是我所拥有的 HTML 方面。我保持简单。在您的 HTML 方面,您可能有一个干扰的 CSS 配置。

<div>
    <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" 
        DataSourceID="SqlDataSource1">
        <Columns>
            <asp:BoundField DataField="ST_CD" HeaderText="ST_CD" SortExpression="ST_CD" />
            <asp:BoundField DataField="ST_CD_ALPHA" HeaderText="ST_CD_ALPHA" 
                SortExpression="ST_CD_ALPHA" />
            <asp:BoundField DataField="ST_DESC" HeaderText="ST_DESC" 
                SortExpression="ST_DESC" />
        </Columns>
    </asp:GridView>
    <asp:SqlDataSource ID="SqlDataSource1" runat="server" 
        ConnectionString="<%$ ConnectionStrings:WebTestConnectionString %>" 

        SelectCommand="SELECT [ST_CD], [ST_CD_ALPHA], [ST_DESC] FROM [STATE_VALUES] WHERE ([ST_CD] LIKE '%' + @ST_CD + '%')">
        <SelectParameters>
            <asp:Parameter DefaultValue="4" Name="ST_CD" Type="String" />
        </SelectParameters>
    </asp:SqlDataSource>

</div>

【讨论】:

  • 我试过了..它没有用..但它适用于我的数据网格..这太令人沮丧了..肯定有其他一些潜在的问题..:S
  • @Dan:你用的是什么框架?我在 VS 2008 中使用了那个确切的代码,它运行得非常好。我可以在 2.0/3.0 中测试它,但如果这是你正在使用的。
  • 我正在使用 3.5.. 我检查了我的 Visual Studio,它的版本是 9.0.30729.1 所以.. 它有服务包 1... 你可以在运行后发布生成的网格 HTML页面?
  • @Dan:使用美国一些州的表格发布了结果。
  • 我想知道为什么你的和我的有很大不同。我的 div 有
    而你的只是
    并且 ID 在表名..我的怎么了?
【解决方案2】:

我是这样做的:

Protected Sub gvwCompounds_RowDataBound(ByVal sender As Object, ByVal e As GridViewRowEventArgs)
    If e.Row.RowType = DataControlRowType.DataRow Then
        e.Row.CssClass = "rowstyle"
    End If
End Sub

在example.css中:

.rowstyle
{
    background-color:#e5e5e5;
}

【讨论】:

  • 我不明白你为什么要检查它是否是一个数据行两次。我什至尝试了您的 e.row.cssclass,当我查看渲染页面的源代码时,它仍然只是: 不是预期的 :(
  • 谢谢,这是个错误。我修好了。
  • 是的.. 即使如此.. 奇怪的是它在 DataGrid 中的工作方式而不是在 GridView 中的工作方式.. 他们都坐在同一页面上。这让我很伤心..
  • @Dan 您在 RowDataBound 事件处理程序中的参数是否正确?即 GridViewRowEventArgs
  • 大卫,是的。我编辑了我的代码以显示它。也许这是这个 gridview 控件和 VB 的错误。我只在网上找到了 C# 中的代码..我知道它不是 RowDataBound 因为我可以设置单元格的颜色..并用鼠标突出显示单元格..但整行不起作用。 . 它从不向 添加任何内容
【解决方案3】:

您是否在为您的 GridView 使用 CSSFriendly 控件适配器?他们可能不会呈现您添加的属性。

【讨论】:

    【解决方案4】:

    这个问题出现在 IE6 中。我通过在 gridview 行中设置所有单元格的 CssClass 名称来解决它。代码如下:

    private void grdvw_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
           e.Row.Attributes.Add("onmouseover", "rowHighlight(this,'lightOn');"); 
           e.Row.Attributes.Add("onmouseout", "rowHighlight(this,'');");
        }
    }
    
    function rowHighlight(obj, nameOfTheClass) 
    {
       cells = obj.getElementsByTagName("td");
       for (var i = 0; i < cells.length; i++) 
       {
           cells[i].className = nameOfTheClass;
       }
    }
    

    【讨论】:

      猜你喜欢
      相关资源
      最近更新 更多
      热门标签