Home > ASP.NET, Share Point > SharePoint: Add ‘onchange’-attribute to DropDownChoiceField

SharePoint: Add ‘onchange’-attribute to DropDownChoiceField

December 2nd, 2010 Leave a comment Go to comments

To change the values of controls logically connected to DropDownChoiceField, I decided to add a client-side JavaScript handler that fires when selected value of DropDownChoiceField is changed. Unfortunately, DropDownChoiceField doesn’t have a habitual collection of attributes. But DropDownChoiceField is a template control and contains DropDownList inside, which allows to add attributes. We need to extract DropDownList from DropDownChoiceField and process it. I think the better way to do that is the Page_PreRender or similar methods, because all child controls of DropDownChoiceField are already created by this moment. According to Reflector, the id of DropDownList is “DropDownChoice”. To get control I use recursive FindControl function.

protected DropDownChoiceField choiceField;
protected void Page_PreRender(object sender, EventArgs e)
    DropDownList ddl = FindControlRecursive(choiceField, "DropDownChoice") as DropDownList;
    if (ddl != null)
        ddl.Attributes.Add("onchange", "javascript:alert('Hello')");
public static Control FindControlRecursive(Control root, string id)
    if (root.ID == id)
        return root;

    foreach (Control c in root.Controls)
        Control t = FindControlRecursive(c, id);
        if (t != null)
            return t;

    return null;
Related posts:
Categories: ASP.NET, Share Point Tags: ,
  1. No comments yet.
  1. No trackbacks yet.