Changed SharePoint TextField tenant ID?

I'm trying to use some javascript on a page layout and I'm running into some weird problem where the ClientID from Sharepoint.WebControls.TextField seems to change between OnLoad and the page being rendered.

In the OnLoad event, TextField3.ClientID resolves to "ctl00_PlaceHolderMain_TextField3", but looking at why my js is not working, the page source shows that the control id is "ctl00_PlaceHolderMain_TextField3_ctl00_TextField".

Any ideas what's going on?

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

      

+2


source to share


2 answers


You must also provide the client ID of the parent control.

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

      



(Note that I've only ever done this with a web part, so there might be some tweak you need to do to get the page layout working.)

Another option is to allow this client side. For more information see Eric Schupp's blog .

+1


source


With the Alex Angas form, here's what I discovered: TexField highlights some literals that end up surrounding the textbox, and that's really the textbox you're interested in. Here's a 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 that this is for my application, your mileage varies.

0


source







All Articles