protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
// 滚动到顶部
ScriptManager.RegisterStartupScript(Page, Page.GetType(), "scrolltop", "setTimeout(scrollToTop, 1);function scrollToTop() { document.getElementById('" + GridView1.ClientID + "').scrollTop = 0; }", true);
}
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e) { if (e.Row.RowType == DataControlRowType.DataRow) { // 添加属性 e.Row.Attributes.Add("onclick", "scrollToTop()"); e.Row.Attributes.Add("style", "cursor:pointer;"); } }
这样,当点击每一行时会自动滚动到顶部。