Javascript SharePoint TextField客户端ID更改?
我试图在页面布局上使用一些javascript,但遇到了一个奇怪的问题,Sharepoint.WebControl.TextField的ClientID似乎在OnLoad和正在显示的页面之间发生变化 在OnLoad事件中,TextField3.ClientID解析为“ctl00\u placeholder main\u TextField3”,但如果查看my js不工作的原因,页面源代码会显示控件id为“ctl00\u placeholder main\u TextField3\u ctl00\u TextField” 知道发生了什么事吗 以下是我使用的代码:Javascript SharePoint TextField客户端ID更改?,javascript,sharepoint,textfield,Javascript,Sharepoint,Textfield,我试图在页面布局上使用一些javascript,但遇到了一个奇怪的问题,Sharepoint.WebControl.TextField的ClientID似乎在OnLoad和正在显示的页面之间发生变化 在OnLoad事件中,TextField3.ClientID解析为“ctl00\u placeholder main\u TextField3”,但如果查看my js不工作的原因,页面源代码会显示控件id为“ctl00\u placeholder main\u TextField3\u ctl00\
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;
}
}
公共类发布模板:Microsoft.SharePoint.Publishing.PublishingLayoutPage
{
受保护的下拉列表作者;
受保护的文本字段TextField3;
私人名单(作者姓名);;
公共列表作者名称
{
获取{return\u authorNames;}
设置{u authorNames=value;}
}
受保护的覆盖无效加载(事件参数e)
{
基础荷载(e);
author.AutoPostBack=false;
this.Page.ClientScript.RegisterClientScriptBlock(this.GetType(),
“fillinuthortext”,getQuery(),true);
author.Attributes.Add(“onChange”、“fillinthortext()”);
如果(!Page.IsPostBack)
{
_authorNames=新列表();
_authorNames=Utilities.GetAuthorList(SPContext.Current.Site);
author.DataSource=\u authorNames;
author.DataBind();
}
}
受保护的覆盖无效OnPreRender(EventArgs e)
{
基于预渲染(e);
如果(author.Items.Count>0)
{
author.SelectedIndex=0;
TextField3.Text=((ListItem)author.Items[author.SelectedIndex]).Text;
}
}
私有字符串getQuery()
{
字符串查询=@“函数fillInUserText(){
var IndexValue=document.getElementById(“”;
query+=author.ClientID;
查询+=@“)。选择索引;
var SelectedVal=document.getElementById(“”;
query+=author.ClientID;
查询+=@“)。选项[IndexValue]。值;
document.getElementById('”;
query+=TextField3.ClientID;
查询+=@“)。值=SelectedVal;
}";
返回查询;
}
}
您还需要包含父控件的客户端ID
// Replace:
query += author.ClientID;
// With:
query += base.ClientID + "_" + author.ClientID;
(请注意,我只对web部件执行过此操作,因此您可能需要对其进行一些调整,以使其在页面布局中工作。)
另一个选择是解决这个客户端问题。有关大多数信息,请参见。在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 "";
}
请记住,这是特定于我的应用程序的,您的里程数和我的不同