admin管理员组

文章数量:1391947

I would like to display the details of a gridview row in another panel when the user mouses over the row. I doubt it is possible through the code behind, so I am looking to use javascript.

for this simple example, I would like to display the id and name of the user in "lblIdDetail" and "lblNameDetail" when the row is moused over:

 <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>

To fill the grid with dummy data:

   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();
        }
    }

I am very inexperienced using javascript, so I would appreciate as detailed an answer as possible. Thanks :-)

I would like to display the details of a gridview row in another panel when the user mouses over the row. I doubt it is possible through the code behind, so I am looking to use javascript.

for this simple example, I would like to display the id and name of the user in "lblIdDetail" and "lblNameDetail" when the row is moused over:

 <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>

To fill the grid with dummy data:

   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();
        }
    }

I am very inexperienced using javascript, so I would appreciate as detailed an answer as possible. Thanks :-)

Share Improve this question asked Aug 29, 2011 at 19:28 StarwfanaticStarwfanatic 6143 gold badges14 silver badges29 bronze badges 0
Add a ment  | 

2 Answers 2

Reset to default 3

You can add a mouseover event in the RowDataBound event, like this:

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

I would add data keys for the columns you want to display in the labels, pull the values in the RowDataBound event, and pass them into the showContents JS function.

With Jame's help I pieced together the functionality I was looking for. Provided code sample will fill a gridview with data, some visible, some hidden. upon mousing over a row, the hidden data will be displayed below, used to show more details than in the 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)+ "')";

        }
    }

本文标签: