SharePoint TextField ClientID 更改?

发布于 2024-08-02 08:05:57 字数 2022 浏览 4 评论 0原文

我正在尝试在页面布局上使用一些 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;
    }
}

I'm trying to use some javascript on a page layout, and I'm encountering a strange issue where the ClientID of a Sharepoint.WebControls.TextField seems to change between OnLoad and the page being displayed.

In the OnLoad event, TextField3.ClientID resolves to "ctl00_PlaceHolderMain_TextField3", but if look to see why my js doesn't work, the page source reveals that the control id to be "ctl00_PlaceHolderMain_TextField3_ctl00_TextField".

Any ideas what's going on?

Here's the code I'm using:

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

逐鹿 2024-08-09 08:05:57

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

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

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

另一个选择是解决此客户端问题。请参阅 Eric Shupp 的博客 了解大部分信息。

You need to include the client ID of the parent control as well.

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

(Note that I've only ever done this with a web part so there may be some tweaking you need to do for it to work in a page layout.)

Another option is to resolve this client side. See Eric Shupp's blog for most info.

和我恋爱吧 2024-08-09 08:05:57

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

请记住,这是特定于我的应用程序的,您的情况可能有所不同。

With help form Alex Angas, Here is what I discovered:
The TexField renders out some literals that end up surrounding a textbox, and it's really the textbox that you're interested in. Here's the modified section of code:

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

Keep in mind, this is specific to my application, your mileage my vary.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文