我有一个 GridView 包含在一个可滚动的 div 中。我还有一个 Button,它根据页面上 TextBox 的文本在此 GirdView 中选择一行。该行选择得很好,但我无法让 div 以编程方式滚动到选定的行。我尝试了多种涉及 JavaScript 的“scrollTop”方法的方法,但由于某种原因,我无法让它们正常工作。到目前为止,这是我的代码(“btnGo_OnClick”方法的一些代码取自对 asp.net 论坛上类似问题的回答,但它似乎对我不起作用):
.aspx 文件:
<%@ Page Title="" Language="C#" MasterPageFile="~/Site1.Master" AutoEventWireup="true"
CodeBehind="StudentList.aspx.cs" Inherits="WebApplication1.WebForm1" %>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">
<br />
<div style="overflow: scroll; height: 400px;">
<asp:GridView ID="StudentList" runat="server" AutoGenerateColumns="False" DataSourceID="DataSource1"
AllowSorting="true" CellPadding="4" DataKeyNames="Email">
<Columns>
<asp:BoundField DataField="LastName" HeaderText="Last" ReadOnly="True" SortExpression="LastName" />
<asp:BoundField DataField="FirstName" HeaderText="First" ReadOnly="True" SortExpression="FirstName" />
<asp:BoundField DataField="MiddleInitial" HeaderText="Middle" ReadOnly="True" SortExpression="MiddleInitial" />
<asp:BoundField DataField="Phone" HeaderText="Phone" ReadOnly="True" SortExpression="Phone" />
<asp:BoundField DataField="Email" HeaderText="Email" ReadOnly="True" SortExpression="Email" />
<asp:BoundField DataField="GPA" HeaderText="GPA" ReadOnly="True" SortExpression="GPA" />
<asp:TemplateField HeaderText="">
<ItemTemplate>
<asp:Button ID="btnEdit1" Text="Edit" AutoPostBack="True" runat="server" OnClick="btnEdit1_OnClick" />
 
<asp:Button ID="btnDelete" Text="Delete" AutoPostBack="True" runat="server" OnClick="btnDelete_OnClick" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
<SelectedRowStyle BackColor="Black" ForeColor="White" />
</asp:GridView>
</div>
<br />
<br />
Search by Last Name:
<asp:TextBox ID="txtSearch" runat="server"></asp:TextBox>
 
<asp:Button ID="btnGo" Text="Go" AutoPostBack="True" runat="server" OnClick="btnGo_OnClick" />
<br />
<br />
<asp:Label ID="lbl1" runat="server" Text=""></asp:Label>
<asp:AccessDataSource ID="DataSource1" runat="server" DataFile="~/App_Data/University(2).accdb"
SelectCommand="SELECT [LastName], [FirstName], [MiddleInitial], [Phone], [Email], [GPA]
FROM [Students] ORDER BY [LastName]" DeleteCommand=""></asp:AccessDataSource>
</asp:Content>
和 .aspx.cs 文件:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text.RegularExpressions;
namespace WebApplication1
{
public partial class WebForm1 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void btnDelete_OnClick(object sender, EventArgs e)
{
Button b = (Button)sender;
GridViewRow containingRow = (GridViewRow)b.NamingContainer;
String strEmail = containingRow.Cells[4].Text;
DataSource1.DeleteCommand = "DELETE FROM [CoursesTaken] WHERE [StudentID] = '" + strEmail + "'";
DataSource1.Delete();
DataSource1.DeleteCommand = "DELETE FROM [Students] WHERE [Email] = '" + strEmail + "'";
DataSource1.Delete();
StudentList.DataBind();
}
protected void btnEdit1_OnClick(object sender, EventArgs e)
{
Button b = (Button)sender;
GridViewRow containingRow = (GridViewRow)b.NamingContainer;
Session["Email"] = containingRow.Cells[4].Text;
Response.Redirect("~/StudentEdit.aspx");
}
protected void btnGo_OnClick(object sender, EventArgs e)
{
int i = 0;
foreach (GridViewRow rowItem in StudentList.Rows)
{
if (rowItem.Cells[0].Text.StartsWith(txtSearch.Text))
{
StudentList.SelectedIndex = i;
int intScrollTo = this.StudentList.SelectedIndex * (int)this.StudentList.RowStyle.Height.Value;
string strScript = "";
strScript += "var gridView = document.getElementById('" + this.StudentList.ClientID + "');\n";
strScript += "if (gridView != null && gridView.parentElement != null && gridView.parentElement.parentElement != null)\n";
strScript += " gridView.parentElement.parentElement.scrollTop = " + intScrollTo + ";\n";
ScriptManager.RegisterClientScriptBlock(this.Page, this.GetType(), "btnGo_OnClick", strScript, true);
break;
}
i++;
}
}
}
}
有人可以解释为什么它没有按预期工作吗?