I have a HiddenField control that is created within my ASP.NET server control. I added a new EventHandler for the ValueChanged event.
Will this event be fired when the value of my HiddenField changes from within a javascript function?
The main problem I am having is trying to retrieve the value of my HiddenField server-side when the value has changed or at least before postback. I have a server control within a page where that page contains a button that causes a postback and I initially attempted using SaveControlState/LoadControlState but for some reason SaveControlState is never called before the postback occurs. I need to be able to either capture the value from within the ValueChanged event or before postback.
Here's the code:
public class ObjectTree : WebControl {
private CA.TreeView _treeView;
private HiddenField fldCheckedNodes = new HiddenField();
public CA.TreeView TreeView {
get { return _treeView; }
set { _treeView = value; }
}
public ObjectTree() {
_treeView = new CA.TreeView();
_treeView.ID = "objectTree";
fldCheckedNodes.ID = "fldCheckedNodes";
}
protected void fldCheckedNodes_ValueChanged(Object sender, EventArgs e) {
string test = fldCheckedNodes.Value;
}
protected override void OnPreRender(EventArgs e) {
TreeView.ClientSideOnNodeCheckChanged = TreeView.ClientID + "NodeChecked";
fldCheckedNodes.ValueChanged += new EventHandler(fldCheckedNodes_ValueChanged);
if (!Page.ClientScript.IsClientScriptBlockRegistered("jscript")) {
StringBuilder jscript = new StringBuilder();
jscript.AppendLine(" function " + TreeView.ClientID + "UpdateCheckedNodes() {");
jscript.AppendLine(" var x = 0;");
jscript.AppendLine(" var nodeArray = " + TreeView.ClientID + ".get_nodes().get_nodeArray();");
jscript.AppendLine(" var nodeLength = nodeArray.length;");
jscript.AppendLine(" document.getElementById('" + fldCheckedNodes.ClientID + "').value = \"\";");
jscript.AppendLine(" for (x=0; x < nodeLength; x++) {");
jscript.AppendLine(" examineNode(nodeArray[x]);");
jscript.AppendLine(" }");
jscript.AppendLine(" var result = document.getElementById('" + fldCheckedNodes.ClientID + "').value;");
jscript.AppendLine(" return true;");
jscript.AppendLine(" }");
jscript.AppendLine(" function examineNode(node) {");
jscript.AppendLine(" var y = 0;");
jscript.AppendLine(" var childNodes = node.get_nodes().get_nodeArray();");
jscript.AppendLine(" var childNodeLength = childNodes.length;");
jscript.AppendLine(" if (node.get_checked()) {");
jscript.AppendLine(" if (document.getElementById('" + fldCheckedNodes.ClientID + "').value.length > 0) {");
jscript.AppendLine(" document.getElementById('" + fldCheckedNodes.ClientID + "').value += \",\"; ");
jscript.AppendLine(" document.getElementById('" + fldCheckedNodes.ClientID + "').value += node.get_id();");
jscript.AppendLine(" } else {");
jscript.AppendLine(" document.getElementById('" + fldCheckedNodes.ClientID + "').value += node.get_id();");
jscript.AppendLine(" }");
jscript.AppendLine(" }");
jscript.AppendLine(" if (childNodeLength >= 1) {");
jscript.AppendLine(" for (y=0; y < childNodeLength; y++) {");
jscript.AppendLine(" examineNode(childNodes[y]);");
jscript.AppendLine(" }");
jscript.AppendLine(" } ");
jscript.AppendLine(" }");
jscript.AppendLine("</script>");
Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "jscript", jscript.ToString());
}
base.OnPreRender(e);
}
protected override void CreateChildControls() {
Controls.Add(TreeView);
Controls.Add(fldCheckedNodes);
}
protected override void RenderContents(HtmlTextWriter output) {
TreeView.RenderControl(output);
fldCheckedNodes.RenderControl(output);
}
}