显示整个GridView行的工具提示

时间:2018-09-26 15:09:25

标签: c# asp.net tooltip

我想创建一个工具提示功能,以显示在网格视图中显示的所有用户数据。

预期结果:GridView显示姓氏,名字,电话,手机。我想要一个工具提示,将鼠标悬停在该用户信息行上并显示该工具提示中的所有数据。仅当用户使用我的搜索框功能时才生成数据,因此它不能是静态的工具提示字符串。我正在使用sqldatasource填充不知道如何调用的gridview。我唯一能找到的就是将gridview设为我不想做的标签。任何指导将不胜感激。

<asp:GridView ID="GridView2" runat="server" AutoGenerateColumns="False" CellPadding="4" DataSourceID="SqlDataSource1" GridLines="None" HorizontalAlign="Center" AllowPaging="True" AllowSorting="True" ForeColor="#333333">
    <AlternatingRowStyle BackColor="White" ForeColor="#284775" />
    <Columns>
        <asp:BoundField DataField="Last_Name" HeaderText="Last_Name" SortExpression="Last_Name" />
        <asp:BoundField DataField="First_Name" HeaderText="First_Name" SortExpression="First_Name" />
        <asp:BoundField DataField="Middle_Int" HeaderText="Middle_Int" SortExpression="Middle_Int" />
        <asp:BoundField DataField="Telephone" HeaderText="Work Telephone" SortExpression="Telephone" />
        <asp:BoundField DataField="Cell_Phone" HeaderText="Work Cell" SortExpression="Cell_Phone" />
    </Columns>
</asp:GridView>

SQLDataSource连接:

<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:myconnection %>" SelectCommand="SELECT [Last_Name], [First_Name], [Middle_Int], [Rank], [Telephone], [Cell_Phone], [Unit], [UserName]  FROM [Person_Search] WHERE (([Middle_Int] LIKE '%' + @Middle_Int + '%') OR ([Cell_Phone] LIKE '%' + @Cell_Phone + '%')  OR ([First_Name] LIKE '%' + @First_Name + '%')  OR ([Telephone] LIKE '%' + @Telephone + '%') OR ([Unit] = @Unit) OR ([Last_Name] LIKE '%' + @Last_Name + '%') OR ([UserName] LIKE '%' + @UserName + '%'))">
    <SelectParameters>
        <asp:ControlParameter ControlID="TextBox1" Name="Middle_Int" PropertyName="Text" />
        <asp:ControlParameter ControlID="TextBox1" Name="Cell_Phone" PropertyName="Text" Type="String" />
        <asp:ControlParameter ControlID="TextBox1" Name="First_Name" PropertyName="Text" Type="String" />
        <asp:ControlParameter ControlID="TextBox1" Name="Telephone" PropertyName="Text" Type="String" />
        <asp:ControlParameter ControlID="TextBox1" Name="Last_Name" PropertyName="Text" Type="String" />
    </SelectParameters>
</asp:SqlDataSource>

标签控件查看代码:

<asp:TemplateField HeaderText="Logid">
<ItemTemplate>
     <asp:Label ID="Label1" runat="server" Text='<%# Bind("cityid") %>' ToolTip='<%# Bind("cityName") %>' ></asp:Label>
 </ItemTemplate>

1 个答案:

答案 0 :(得分:0)

您可以使用RowDataBound事件将ToolTip属性添加到GridViewRow。

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
    //check if the row is d datarow
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        //cast the row back to a datarowview
        DataRowView row = e.Row.DataItem as DataRowView;

        StringBuilder tooltip = new StringBuilder();

        //add the data to the tooltip stringbuilder
        tooltip.Append(row["Last_Name"] + ", ");
        tooltip.Append(row["First_Name"] + ", ");
        tooltip.Append(row["Middle_Int"] + ", ");
        tooltip.Append(row["Telephone"] + ", ");
        tooltip.Append(row["Cell_Phone"]);

        //add the tooltip attribute to the row
        e.Row.Attributes.Add("ToolTip", tooltip.ToString());
    }
}