2

ページ レイアウトで 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;
    }
}
4

2 に答える 2

1

親コントロールのクライアント ID も含める必要があります。

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

(これまで Web パーツでしかこれを行ったことがないことに注意してください。そのため、ページ レイアウトで機能させるために微調整が必​​要になる場合があります。)

別のオプションは、このクライアント側を解決することです。ほとんどの情報については、 Eric Shupp のブログを参照してください。

于 2009-08-25T05:30:01.733 に答える
0

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 "";
    }

これは私のアプリケーションに固有のものであり、マイレージはさまざまです。

于 2009-08-25T20:10:53.403 に答える