- private void HandlePanelScrolBar()
- ...{
- //定義兩個HiddenField,分別紀錄Panel的ScrollBar的X與Y位置
- HiddenField HF_ScrollPosX = new HiddenField();
- HiddenField HF_ScrollPosY = new HiddenField();
- HF_ScrollPosX.ID = "ScrollPosX";
- HF_ScrollPosY.ID = "ScrollPosY";
- form1.Controls.Add(HF_ScrollPosX);
- form1.Controls.Add(HF_ScrollPosY);
- //生成JS:將Panel的ScrollBar的X,Y位置設置給兩個HiddenField
- string script;
- script = "window.document.getElementById('" + HF_ScrollPosX.ClientID + "').value = "
- + "window.document.getElementById('" + P_Container.ClientID + "').scrollLeft;"
- + "window.document.getElementById('" + HF_ScrollPosY.ClientID + "').value = "
- + "window.document.getElementById('" + P_Container.ClientID + "').scrollTop;";
- this.ClientScript.RegisterOnSubmitStatement(this.GetType(), "SavePanelScroll", script);
- if (IsPostBack) //如果是PostBack,將保存在HiddenField的ScrollBar的X,Y值重設回給Panel的ScrollBar
- ...{
- script = "window.document.getElementById('" + P_Container.ClientID + "').scrollLeft = "
- + "window.document.getElementById('" + HF_ScrollPosX.ClientID + "').value;"
- + "window.document.getElementById('" + P_Container.ClientID + "').scrollTop = "
- + "window.document.getElementById('" + HF_ScrollPosY.ClientID + "').value;";
- this.ClientScript.RegisterStartupScript(this.GetType(), "SetPanelScroll", script, true);
- }
- }
代碼中的P_Container爲Panel的名字