2009-08-25 43 views
2

我想在頁面佈局上使用一些JavaScript,並遇到一個奇怪的問題,其中Sharepoint.WebControls.TextField的ClientID似乎在OnLoad和正在顯示的頁面之間發生更改。SharePoint TextField ClientID更改?

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

回答

1

您需要包括父控件的客戶端ID爲好。

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

(請注意,我只曾經與一個Web部件做到了這一點所以可能會有一些調整,你需要做的是在一個頁面佈局工作。)

另一種選擇是解決這個客戶端。大多數信息請參見Eric Shupp's blog

0

的幫助形式亞歷安加斯,這裏是我的發現: 的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 ""; 
    } 

請記住,這是具體到我的應用程序,你的里程我的有所不同。