【问题标题】:ASP.NET DataList - Row Background Color Change When MouseOverASP.NET DataList - 鼠标悬停时行背景颜色更改
【发布时间】:2013-10-07 23:50:21
【问题描述】:

我试图在移动时改变行背景颜色,但我无法让它工作。我不确定我使用 OnItemDataBound 是否正确或我的代码隐藏不正确。

请帮忙。谢谢!

<asp:DataList BackColor="#ffffff" id="DataList1" DataSourceID="dsCompanyListPartialMatch"  runat="server" Width="80%" DataKeyField="Company1Word"
    UseAccessibleHeader="true"
    CssClass="books"
    HeaderStyle-CssClass="header"
    ItemStyle-CssClass="item"
    AlternatingItemStyle-CssClass="alternating" 
    GridLines="Both"
    CellPadding="0"
    CellSpacing="0" BorderColor="Black"
    ItemStyle-BorderColor="Black" BorderWidth="0"
     HorizontalAlign="Center"
    RepeatDirection="Vertical" 
    OnItemDataBound="DataList1_ItemDataBound"
    >                
    <HeaderTemplate>
    </HeaderTemplate>

    <ItemStyle BorderColor="black" Font-Size="Medium" /> 
    <ItemTemplate>
        <table border="0">
        <tr>
            <td style="width: 50px; border-right:1px solid black; border-spacing:0;  text-align:center; ">        
            <asp:LinkButton>
                <asp:LinkButton ID="LinkButton2" runat="server" Text="+" CommandArgument='<%#Container.ItemIndex%>'
                OnCommand="LinkButton1_Command"  
                Font-Underline="false"       
                Height="25"      
                Font-Bold="true"                  
                ></asp:LinkButton>
            </td>
              <td style="width: 50px; border-right:1px solid black; border-spacing:0;"><%#Eval("Row")%></td>
              <td style="width: 800px"><asp:Literal ID="litFoo" runat="server" Text='<%#Eval("Company")%>' /> </td>
             <td style="width: 600px; text-align:right;">
                <asp:CheckBox  id="check1" runat="server" />
            </td>
            <asp:Label ID="lblRow" Visible="False" runat="Server" Text='<%# DataBinder.Eval(Container.DataItem, "Row") %>' />
        </tr>    
        </table>     

              <asp:Panel ID="pnlChildView" runat="server" style="padding-left:200px;">
                <asp:DataList ID="childList" runat="server" Width="100%">
                   <ItemTemplate>
                        <table class="table1" border="1">
                       <tr>
                          <td style="width: 800px; border-right:0px solid black; border-spacing:0;">&#8226; <%#Eval("CompanyName")%></td>
                           <td style="text-align:right; "><a href="/Apps/ERP/Other/CompanyInfo.asp?CompanyID=<%#Eval("CompanyID")%>" ><%#Eval("CompanyID")%></a></td>                      
                       </tr>
                        </table>
                   </ItemTemplate>
               </asp:DataList>
           </asp:Panel>
    </ItemTemplate>
     <FooterTemplate>
    </FooterTemplate>                
</asp:DataList>

后面的代码

protected void DataList1_ItemDataBound(object sender, DataListItemEventArgs e)
{
    if (e.Item.ItemType == ListItemType.Item)
    {

        e.Item.Attributes.Add("onmouseover", "this.className='BGMouseOver'");

        e.Item.Attributes.Add("onmouseout", "this.className='BGMouseOut'");

    }
}

【问题讨论】:

  • 你能展示你的模板吗?您可以使用可以在模板级别应用的 CSS 更轻松地做到这一点,甚至不需要为它执行代码隐藏。
  • 我已经用模板更新了代码。谢谢。

标签: c# asp.net webforms


【解决方案1】:

试试 jQuery mouseover() 方法

Click here

【讨论】:

  • 感谢您的回复。它只会改变 LinkBut​​ton Text 的 bgcolor。我想突出显示整行。
  • jQuery 解决方案看起来也不错。我稍后会尝试。谢谢
【解决方案2】:

我建议使用 CSS。你可以在这里找到这个想法:div background color, to change onhover

将 CSS 添加到顶部:

<style type="text/css">
    .div_hover:hover { background-color: #000000; } <-- this is where you set your hover color
</style>

基本上,将 ItemTemplate 中的所有内容包装在一个 div 中,如下所示:

<ItemTemplate>    
    <div class="div_hover">
        [ItemTemplate stuff...]
    </div>
</ItemTemplate>

【讨论】:

  • 我按照你的建议尝试了,但是 getter 错误:类型 'System.Web.UI.WebControls.DataList' 没有名为 'div' 的公共属性。
  • 您必须将 div 放入项目模板中。我正在更新我的答案给你看。
  • 我明白了。有用。非常感谢(我把 )
猜你喜欢
  • 1970-01-01
  • 2010-12-25
  • 1970-01-01
  • 2014-05-19
  • 1970-01-01
  • 1970-01-01
  • 2015-09-29
  • 1970-01-01
相关资源
最近更新 更多