要先建立隱藏欄位 HiddenField。
此網路上找到的範例為Panel,加以修改也可以用在大致上所有有ScrollBar的Control,
當然毫無疑問的Page的Scroll也可以。
個人是把 HiddenField 直接建在Page上,所以下面建構的部份是不需要的。
string sScript;
//HiddenField oScrollPosX; //記錄水平捲軸位置
//HiddenField oScrollPosY; //記錄垂直捲軸位直
//oScrollPosX = new HiddenField();
//oScrollPosX.ID = "ScrollPosX";
//this.Form.Controls.Add(oScrollPosX);
//oScrollPosY = new HiddenField();
//oScrollPosY.ID = "ScrollPosY";
//this.Form.Controls.Add(oScrollPosY);
//頁面 Sumbit 時,記錄 Panel 的水平及垂直捲軸位置
sScript = "window.document.getElementById('" + ScrollPosX.ClientID + "').value =" +
"window.document.getElementById('" + Panel1.ClientID + "').scrollLeft;";
sScript = sScript +
"window.document.getElementById('" + ScrollPosY.ClientID + "').value = " +
"window.document.getElementById('" + Panel1.ClientID + "').scrollTop;";
this.ClientScript.RegisterOnSubmitStatement(this.GetType(), "SavePanelScroll", sScript);
if (this.IsPostBack)
{
//當 PostBack 時,利用 HiddenField 記錄的值來維護 Panel 捲軸位置
//ScrollPosX.Value = this.Request.Form[ScrollPosX.ClientID];
//ScrollPosY.Value = this.Request.Form[ScrollPosY.ClientID];
sScript = "window.document.getElementById('" + Panel1.ClientID + "').scrollLeft = " + ScrollPosX.Value + ";" +
"window.document.getElementById('" + Panel1.ClientID + "').scrollTop = " + ScrollPosY.Value + ";";
this.ClientScript.RegisterStartupScript(this.GetType(), "SetPanelScroll", sScript, true);
}
沒有留言:
張貼留言