NO IMAGE

using System;

namespace test
{
 /// <summary>
 /// ScriptHelper 的摘要說明。
 /// </summary>
 /// <summary>
 /// 提供一些產生頁面指令碼的方法
 /// </summary>
 public class ScriptHelper
 {

 /// <summary>
 /// 獲取客戶端檢視控制元件的指令碼
 /// </summary>
 /// <param name=”controlName”></param>
 /// <returns>指令碼程式碼</returns>
  public ScriptHelper()
  {
  }

   public static string GetViewControlScript(string controlName)
   {

   //建立客戶端函式ViewObj
   string script = “/n”;
   script = “<script language=/”javascript/”>/n”;
   script = “function ViewObj(objName)/n”;
   script = “{/n”;
   script = “var obj = document.all.item(objName);/n”;
   script = “if (obj != null)/n”;
   script = “{/n”;
   script = “/tobj.scrollIntoView();/n”;
   script = “/tobj.focus();/n”;
   script = “}/n”;
   script = “}/n”;

    //建立客戶端函式ToDo
   script = “function ToDo()”;
   script = “{/n”;
   script = string.Format(“setTimeout(/”ViewObj(‘{0}’)/”,1000);/n”, controlName);
   script = “}/n”;

   script = “window.onload = ToDo;/n”;
   script = “</script>/n”;

   return script;
   }

 }

}

使用示例:

  為了方便輸入指令碼,我在頁面上放了個Label:lblScript,並把lblScript的EnableViewState屬性和Visible屬性設成False。

  然後在lblScrpt的Click事件的操作程式碼後新增輸入指令碼的程式碼,如下所示:

  private void btnSave_Click(object sender, System.EventArgs e)
  {
  project.UpdateProjectInfo(ds);
  lblScript.Text = ScriptHelper.GetViewControlScript(“btnSave”);
  }

  在點選btnSave按鈕後,頁面會自動滾動到btnSave的位置,減少了頁面重新整理帶來的不便。