private static String en(Control p, JsonObjectMode mode) { string retVal = ""; foreach (Control c in p.Controls) { if (!String.IsNullOrEmpty(c.ID)) { retVal += (counter > 0 ? "," : "") + c.ID + ":\"" + (mode == JsonObjectMode.ById ? "#" : "") + c.ClientID + "\""; counter++; switch (c.GetType().ToString()) { case "System.Web.UI.WebControls.CheckBoxList": case "System.Web.UI.WebControls.RadioButtonList": case "System.Web.UI.WebControls.ListBox": case "System.Web.UI.WebControls.DropDownList": case "System.Web.UI.HtmlControls.HtmlSelect": ListControl cc = (ListControl)c; if (cc.Items.Count > 0) { for (Int32 index = 0; index < cc.Items.Count; index++) { retVal += (counter > 0 ? "," : "") + c.ID + "_" + index + ":\"" + (mode == JsonObjectMode.ById ? "#" : "") + c.ClientID + "_" + index + "\""; counter++; } } break; } } retVal += en(c, mode); } return (retVal); }
/// <summary> /// Insert a script block which creates a JSON object with an array of /// client control ID names for all server-side controls. This allows access /// to the objects using javascript. It is best used in the Page_PreRender block. /// <example> /// <code> /// using Argentini.Halide; /// ... /// H3ClientSide.CreateJsonIdArray(MyPanel, "en", H3ClientSide.JsonObjectMode.ByID); /// </code> /// Assuming that there is a server-side control on the page, with the ID /// "Control01", its run-time ID is something cryptic, like "ctl00_Body_Control01". /// After the code above is executed, however, Javascript code on the page itself /// can refer to all server-side controls by their server-side ID's, as: /// <code> /// var obj = document.getElementById(en.Control01); /// $(en.Control01).value = "test"; /// </code> /// </example> /// </summary> /// <param name="pageObject">The Page object.</param> /// <param name="containerControl">The container for which all children should be used.</param> /// <param name="objectName">Name for the object with the array of names.</param> /// <param name="mode">JsonObjectMode enumeration which specifies how page /// objects are to be identified; by ID or CSS class name.</param> public static void CreateJsonIdArray(Page pageObject, Control containerControl, string objectName, JsonObjectMode mode) { if (objectName.Contains(" ")) { throw new Exception("CreateJsonIdArray objectName cannot contain spaces"); } else { counter = 0; string script = en(containerControl, mode); pageObject.ClientScript.RegisterClientScriptBlock(typeof(Page), objectName, objectName + "={" + script + "};", true); } }