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