在gridview行鼠标悬停时,我想在另一个标签中显示行数据

时间:2011-08-29 19:28:19

标签: javascript asp.net gridview onmouseover onhover

当用户将鼠标放在行上时,我想在另一个面板中显示gridview行的详细信息。我怀疑通过后面的代码是可能的,所以我希望使用javascript。

对于这个简单的例子,我希望当行被鼠标悬停时,在“lblIdDetail”和“lblNameDetail”中显示用户的id和名称:

 <asp:GridView ID="GridView1" runat="server" EnableModelValidation="True" AutoGenerateColumns="false">
        <Columns>
            <asp:TemplateField>
                <ItemTemplate>
                    <asp:Label ID="lblId" Text=<%# Bind("id") %> runat="server"></asp:Label>
                </ItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField>
                <ItemTemplate>
                    <asp:Label ID="lblName" Text=<%# Bind("name") %> runat="server"></asp:Label>
                </ItemTemplate>
            </asp:TemplateField>
        </Columns>
    </asp:GridView>
    <asp:Panel ID="pnlDetails" runat="server">
        <asp:Label ID="Label1" runat="server" Text="The hovered Id is: "></asp:Label>
        <asp:Label ID="lblIdDetail" runat="server" Text="Label"></asp:Label>
        <br />
        <asp:Label ID="Label3" runat="server" Text="The hovered name is: "></asp:Label>
        <asp:Label ID="lblNameDetail" runat="server" Text="Label"></asp:Label>
    </asp:Panel>

用虚拟数据填充网格:

   protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            DataTable sampleData = new DataTable();
            sampleData.Columns.Add("id");
            sampleData.Columns.Add("name");


            sampleData.Rows.Add("1", "Dave");
            sampleData.Rows.Add("2", "John");
            sampleData.Rows.Add("3", "Jacob");
            sampleData.Rows.Add("4", "Smith");

            GridView1.DataSource = sampleData;
            GridView1.DataBind();
        }
    }

我使用javascript非常缺乏经验,所以我希望尽可能详细地回答。 谢谢: - )

2 个答案:

答案 0 :(得分:2)

您可以在RowDataBound事件中添加mouseover事件,如下所示:

//pass the needed row contens into showContents
e.Row.Attributes["onmouseover"] = "showContents(arg1, arg2, arg3)"; 

我会为要在标签中显示的列添加数据键,在RowDataBound事件中提取值,并将它们传递给showContents JS函数。

答案 1 :(得分:1)

在Jame的帮助下,我将我正在寻找的功能拼凑在一起。提供的代码示例将使用数据填充gridview,一些可见,一些隐藏。将鼠标悬停在一行上后,隐藏数据将显示在下方,用于显示比在gridview中更多的详细信息:

<script type="text/javascript">

window.onload = hideColumns;

    function hideColumns() {
    var gv = document.getElementById("GridView1");
    for (var i = 0; i < gv.rows.length; i++) {
        gv.rows[i].cells[1].style.display = "none";
    }
}

function showContents(rowIndex) {


    var gv = document.getElementById("GridView1");
    var rowElement = gv.rows[rowIndex];
    var id = rowElement.cells[0].innerHTML;
    document.getElementById('lblIdDetail').innerHTML = id;
    var name = rowElement.cells[1].innerHTML;
    document.getElementById('lblNameDetail').innerHTML = name;

}
</script>

<form id="form1" runat="server">
<div>
    <asp:GridView ID="GridView1" runat="server" EnableModelValidation="True" 
        AutoGenerateColumns="false" OnRowDataBound="setMouseover">
        <Columns>
            <asp:TemplateField>
                <ItemTemplate>
                    <asp:Label ID="lblId" Text="<%# Bind('id') %>" runat="server"></asp:Label>
                    <asp:Label ID="lblGreeting" Text="hello" runat="server" Visible="false" ></asp:Label>
                </ItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField >
                <ItemTemplate>
                    <asp:Label ID="lblName" Text="<%# Bind('name') %>" runat="server" ></asp:Label>
                </ItemTemplate>
            </asp:TemplateField>
        </Columns>
    </asp:GridView>
    <asp:Panel ID="pnlDetails" runat="server">
        <asp:Label ID="Label1" runat="server" Text="The hovered Id is: "></asp:Label>
        <asp:Label ID="lblIdDetail" runat="server" Text="Label"></asp:Label>
        <br />
        <asp:Label ID="Label3" runat="server" Text="The hovered name is: "></asp:Label>
        <asp:Label ID="lblNameDetail" runat="server" Text="Label"></asp:Label>
    </asp:Panel>
</div>
</form>

  protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            DataTable sampleData = new DataTable();
            sampleData.Columns.Add("id");
            sampleData.Columns.Add("name");


            sampleData.Rows.Add("1", "Dave");
            sampleData.Rows.Add("2", "John");
            sampleData.Rows.Add("3", "Jacob");
            sampleData.Rows.Add("4", "Smith");

            GridView1.DataSource = sampleData;
            GridView1.DataBind();


        }
    }

    protected void setMouseover(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowIndex != -1)
        {
            e.Row.Attributes["onmouseover"] = "showContents('" + (e.Row.RowIndex +1)+ "')";

        }
    }