2011-06-02 73 views
1

我有一个属性的集合,我正尝试在母版中使用,但我无法这样做。Setfocus()在母版页

public partial class Site1 : System.Web.UI.MasterPage 
{ 
    protected void Page_Load(object sender, EventArgs e) 
    { 
      Page.MaintainScrollPositionOnPostBack = true; 
      SetFocus(this); 

    } 
     public void SetFocus(Site1 site1) 
    { 
     string[] sCtrl = null; 
     string sCtrlId = null; 
     Control sCtrlFound = default(Control); 
     string sCtrlClientId = null; 
     string sScript = null; 

     sCtrl = site1.Request.Form.GetValues("__EVENTTARGET"); 
     if ((sCtrl != null)) 
     { 
      sCtrlId = sCtrl[0]; 
      sCtrlFound = site1.FindControl(sCtrlId); 
      Global.logger.Info("sCtrlId = " + sCtrlId + ", " + sCtrlFound); 
      if ((sCtrlFound != null)) 
      { 
       sCtrlClientId = sCtrlFound.ClientID; 
       Global.logger.Info("sCtrlClientId = " + sCtrlClientId); 

       sScript = "<SCRIPT language='javascript'>document.getElementById('" + sCtrlClientId + "').focus(); if (document.getElementById('" + sCtrlClientId + "').scrollIntoView(false)) {document.getElementById('" + sCtrlClientId + "').scrollIntoView(true)} </SCRIPT>"; 

       site1.ClientScript.RegisterStartupScript(typeof(string), "controlFocus", sScript); 

      } 
     } 
    } 
    } 

我上最后一行的错误:

 
'System.Web.UI.MasterPage' does not contain a definition for 'ClientScript' and no extension method 'ClientScript' accepting a first argument of type 'System.Web.UI.MasterPage' could be found (are you missing a using directive or an assembly reference?) 

此代码工作正常正常的内容页面上。该错误只在母版页上很受欢迎。

对于我能做些什么来使上述代码有效,你有什么建议吗?

回答

1

我不知道,如果这个工程,但它至少应该编译:

替换:

site1.ClientScript.RegisterStartupScript(typeof(string), "controlFocus", sScript); 

有了:

site1.Page.ClientScript.RegisterStartupScript(GetType(String), "controlFocus", sScript);