【问题标题】:SharePoint TextField ClientID changes?SharePoint TextField ClientID 更改?
【发布时间】:2009-08-25 01:59:12
【问题描述】:

我正在尝试在页面布局上使用一些 javascript,但我遇到了一个奇怪的问题,即 Sharepoint.WebControls.TextField 的 ClientID 似乎在 OnLoad 和正在显示的页面之间发生变化。

在 OnLoad 事件中,TextField3.ClientID 解析为“ctl00_PlaceHolderMain_TextField3”,但如果查看我的 js 不起作用的原因,页面源显示控件 id 为“ctl00_PlaceHolderMain_TextField3_ctl00_TextField”。

有什么想法吗?

这是我正在使用的代码:

public class PostingTemplate : Microsoft.SharePoint.Publishing.PublishingLayoutPage
{
    protected DropDownList author;
    protected TextField TextField3;
    private List<string> _authorNames;

    public List<string> AuthorName
    {
        get { return _authorNames; }
        set { _authorNames = value; }
    }

    protected override void OnLoad(EventArgs e)
    {
        base.OnLoad(e);
        author.AutoPostBack = false;
        this.Page.ClientScript.RegisterClientScriptBlock(this.GetType(),
        "fillInAuthorText", getQuery(), true);
        author.Attributes.Add("onChange", "fillInAuthorText()");
        if (!Page.IsPostBack)
        {
            _authorNames = new List<string>();
            _authorNames = Utilities.GetAuthorList(SPContext.Current.Site);
            author.DataSource = _authorNames;
            author.DataBind();
        }
    }

    protected override void OnPreRender(EventArgs e)
    {
        base.OnPreRender(e);
        if (author.Items.Count > 0)
        {
            author.SelectedIndex = 0;
            TextField3.Text = ((ListItem)author.Items[author.SelectedIndex]).Text;
        }
    }

    private string getQuery()
    {
        string query = @" function fillInAuthorText() {
        var IndexValue = document.getElementById('";
        query += author.ClientID;
        query += @"').selectedIndex;
        var SelectedVal = document.getElementById('";
        query += author.ClientID;
        query += @"').options[IndexValue].value;
        document.getElementById('";
        query += TextField3.ClientID;
        query += @"').value = SelectedVal;
        }";
        return query;
    }
}

【问题讨论】:

    标签: javascript sharepoint textfield


    【解决方案1】:

    您还需要包含父控件的客户端 ID。

    // Replace:
    query += author.ClientID;
    // With:
    query += base.ClientID + "_" + author.ClientID;
    

    (请注意,我只对 Web 部件进行过此操作,因此您可能需要进行一些调整才能使其在页面布局中工作。)

    另一个选择是解决这个客户端。大部分信息请参见Eric Shupp's blog

    【讨论】:

      【解决方案2】:

      在 Alex Angas 的帮助下,我发现了以下内容: TexField 呈现出一些文字,这些文字最终围绕着一个文本框,它确实是您感兴趣的文本框。这是修改后的代码部分:

       private string getQuery()
          {
              string query = @" function fillInAuthorText() {
              var IndexValue = document.getElementById('";
              query += author.ClientID;
              query += @"').selectedIndex;
              var SelectedVal = document.getElementById('";
              query += author.ClientID;
              query += @"').options[IndexValue].value;
              document.getElementById('";
              query += getTextFieldID(TextField3);
              query += @"').value = SelectedVal;
              }";
              return query;
          }
      
          private string getTextFieldID(Control txt)
          {
              foreach (Control c in txt.Controls)
              {
                  if (c.HasControls())
                  {
                      foreach (Control con in c.Controls)
                          if (con is TextBox)
                              return con.ClientID;
                  }
              }
      
              return "";
          }
      

      请记住,这是针对我的应用程序的,您的里程因人而异。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2020-09-23
        • 2012-10-05
        • 1970-01-01
        • 2015-03-19
        • 2017-07-14
        • 2019-12-23
        • 1970-01-01
        相关资源
        最近更新 更多