SharePoint TextField ClientID 更改? [英] SharePoint TextField ClientID changes?
问题描述
我正在尝试在页面布局上使用一些 javascript,但我遇到了一个奇怪的问题,其中 Sharepoint.WebControls.TextField 的 ClientID 似乎在 OnLoad 和正在显示的页面之间发生了变化.
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.
在 OnLoad 事件中,TextField3.ClientID 解析为ctl00_PlaceHolderMain_TextField3",但如果查看为什么我的 js 不起作用,页面源显示控件 ID 为ctl00_PlaceHolderMain_TextField3_ctl00_TextField".
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".
知道发生了什么吗?
这是我正在使用的代码:
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;
}
}
推荐答案
您还需要包含父控件的客户端 ID.
You need to include the client ID of the parent control as well.
// Replace:
query += author.ClientID;
// With:
query += base.ClientID + "_" + author.ClientID;
(请注意,我只使用 Web 部件完成过此操作,因此您可能需要进行一些调整才能使其在页面布局中工作.)
(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.)
另一个选择是解决这个客户端.参见 Eric Shupp 的博客了解大部分信息.
Another option is to resolve this client side. See Eric Shupp's blog for most info.
这篇关于SharePoint TextField ClientID 更改?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!