【问题标题】:How to add a tooltip on mouse over on the Grid View Column Heading如何在鼠标悬停在网格视图列标题上添加工具提示
【发布时间】:2012-11-08 21:23:13
【问题描述】:

当用户将鼠标悬停在 gridview 中列的列标题上时,例如:列标题 Year,当我将鼠标悬停在 Year 上时,我应该会看到该年的含义解释“这是这一年当学生加入大学等”。

下面是我的 ascx 代码:

 <asp:GridView ID="grdView" runat="server" Width="900px" AutoGenerateColumns="False"
                AllowPaging="true" AllowSorting="true" CellSpacing="0" CellPadding="5" PageSize="20"
        OnRowDataBound="grdView_RowDataBound">
                <Columns>
 <asp:TemplateField HeaderText="ID Number" ItemStyle-Width="90px" >
    <ItemTemplate>
      <asp:Label ID="Label1" runat="server" Text='<%# Bind("ID")%'></asp:Label>
    </ItemTemplate>
 </asp:TemplateField><asp:BoundField DataField="StudentName" HeaderText="StudentName"> </asp:BoundField>

请告诉我如何将鼠标悬停在我的 gridview 列标题上的文本或工具提示上。 谢谢,

【问题讨论】:

    标签: asp.net gridview


    【解决方案1】:

    protected void grd_popup_details_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            for (int i = 0; i < e.Row.Cells.Count; i++)
            {
                e.Row.Cells[i].ToolTip = e.Row.Cells[i].Text;
            }
        }
    

    Refference link

    【讨论】:

    • 信息图加 1 比什么都重要! :) 你是怎么做到的?
    • 如何仅为特定列添加工具提示,在您的回答中仅针对评论说
    【解决方案2】:

    我从来没有做过任何asp.net开发,但是这里似乎提供了一个解决方案:how to add title for every header column in gridview in ASP.NET

    您的示例可能如下所示:

     <asp:GridView ID="grdView" runat="server" Width="900px" AutoGenerateColumns="False"
                AllowPaging="true" AllowSorting="true" CellSpacing="0" CellPadding="5" PageSize="20"
        OnRowDataBound="grdView_RowDataBound">
                <Columns>
     <asp:TemplateField HeaderText="ID Number" ItemStyle-Width="90px" >
    <HeaderTemplate>
           <asp:Label ID="Header" ToolTip="HERE WE GO!!!!" runat="server" Text="Label"></asp:Label>
           </HeaderTemplate>
        <ItemTemplate>
          <asp:Label ID="Label1" runat="server" Text='<%# Bind("ID")%'></asp:Label>
        </ItemTemplate>
     </asp:TemplateField><asp:BoundField DataField="StudentName" HeaderText="StudentName"> </asp:BoundField>
    

    我会试一试:)

    【讨论】:

      【解决方案3】:

      在后面的代码中,为 GridView 创建方法 rowDataBound 并添加以下代码

      protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
      {
          if (e.Row.RowType == DataControlRowType.Header)
          {
              foreach (TableCell cell in e.Row.Cells)
              {
                  cell.Attributes.Add("title", "Tooltip text for " + cell.Text);
              }
          }
      }
      

      不要忘记在 GridView 中设置属性 OnRowDataBound。

      http://rosshawkins.net/archive/2007/04/15/adding-tooltips-to-gridview-headers.html.aspx

      【讨论】:

      • 那个是cell,那header呢?
      • 发布的链接 crs0794 是向标题添加工具提示的绝佳方式。值得一读。
      【解决方案4】:
      if (e.Row.RowType == DataControlRowType.DataRow)
      {
           e.Row.Cells[1].ToolTip = Grd.Columns[1].HeaderText;
      }
      

      【讨论】:

      • 感谢您提供此代码 sn-p,它可能会提供一些有限的即时帮助。 proper explanation would greatly improve its long-term value 通过展示为什么这是一个很好的解决问题的方法,并将使其对未来有其他类似问题的读者更有用。请edit您的回答添加一些解释,包括您所做的假设。
      【解决方案5】:

      这是一个示例,显示即使在 Autogenerate=True 并且动态确定 GridView 列时也可以使用 ColumnName。

      当文本包含双引号等转义字符时,似乎也需要 HtmlDecode()。

      Dictionary<string, int> _headerIndiciesForDetailsReportGridView = null;
      
      protected void detailsReportGridView_RowDataBound(object sender, GridViewRowEventArgs e)
      {
          if (_headerIndiciesForDetailsReportGridView == null)
          {
              int index = 0;
              _headerIndiciesForDetailsReportGridView = ((Table)((GridView)sender).Controls[0]).Rows[0].Cells
                  .Cast<TableCell>()
                  .ToDictionary(c => c.Text, c => index++);
          }
      
          if (e.Row.RowType == DataControlRowType.DataRow)
          {                            
              TableCell cell = e.Row.Controls[_headerIndiciesForDetailsReportGridView["theColumnName"]] as TableCell;
      
              // Shorten text in a particular column to a max of 20 characters.
              // Set tooltip to the full original text. Decode to remove &quot, etc.
              //
              string orgText = cell.ToolTip = HttpUtility.HtmlDecode(cell.Text);
      
              if (orgText.Length > 20)    // If cell text should be shortened
                  cell.Text = HttpUtility.HtmlEncode(orgText.Substring(0, 20) + "...");
          }
      }
      

      【讨论】:

        【解决方案6】:
         <ItemTemplate>                                                                               <asp:HiddenField ID="HiddenField1" runat="server" Value='<%#Bind("EmpInfoDetail") %>' />                                                                                                                                                              <asp:Label ID="Label3" runat="server" Text="" Visible="false" Font-Bold="true" ForeColor="#cc3300" CssClass="tooltip"></asp:Label>                                                                                                                                                                                                                                                                                         </ItemTemplate> 
            
        
        
             use this code on grid view row data bound   
                #region show grid text on mouse hover
                                if (e.Row.RowType == DataControlRowType.DataRow)
                                {
                                    DataRowView drv = e.Row.DataItem as DataRowView;
                                    Label test = e.Row.FindControl("Label3") as Label;
                                    if (drv["EmpInfoDetail"].ToString().Length > 500)
                                    {
                                        test.Text = drv["EmpInfoDetail"].ToString().Substring(0, 500) + "...";
                                    }
                                    else
                                    {
                                        test.Text = drv["EmpInfoDetail"].ToString();
                                    }
                    
                                    e.Row.Cells[1].ToolTip = drv["EmpInfoDetail"].ToString();
                    
                                }
                                #endregion
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2014-12-31
          • 1970-01-01
          • 1970-01-01
          • 2014-04-09
          • 2014-03-09
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多