【问题标题】:Setfocus() in master page母版页中的 Setfocus()
【发布时间】:2011-06-02 17:33:20
【问题描述】:

我已为我尝试在母版页中使用的属性设置焦点,但我无法这样做。

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' 不包含'ClientScript' 的定义,并且找不到接受'System.Web.UI.MasterPage' 类型的第一个参数的扩展方法'ClientScript'(您是否缺少使用指令还是汇编参考?)

此代码在正常内容页面上运行良好。该错误仅发生在母版页上。

您对我可以做些什么来使上述代码正常工作有什么建议吗?

【问题讨论】:

    标签: c# asp.net master-pages webforms clientscript


    【解决方案1】:

    我不确定这是否可行,但至少应该可以编译:

    替换:

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

    与:

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

    【讨论】:

      猜你喜欢
      • 2016-12-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多