Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 281143
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T05:10:42+00:00 2026-05-12T05:10:42+00:00

I have a HiddenField control that is created within my ASP.NET server control. I

  • 0

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

}

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-12T05:10:42+00:00Added an answer on May 12, 2026 at 5:10 am

    Your server-side code only fires during a call to the page that is rendering the control–either during initial load or during a PostBack.

    If you need the value from the hidden field PRIOR to PostBack, then you need to do your work in client-side JavaScript. For example, you can create a function in JavaScript called “function checkedNodesChanged” and do work in there by registering and event listener in JavaScript. The output would need to look like:

    <input type="hidden" id="fldCheckedNodes" onchange="checkedNodesChanged();" />
    

    There is probably a way to add this in your code-behind for the control. Something like:

    fldCheckedNodes.Attributes.Add("onchange", "checkedNodesChanged();");
    

    You would have to create the meaningful guts of checkedNodesChanged.
    The server-side ValueChanged event only fires during the Page lifecycle on the server during a PostBack and it detects that the value is different than the original value from the original page load or from the last value stored in ViewState when it was Posted Back the last time.

    Perhaps this UpdateCheckedNodes is the “checkedNodesChanged()” function I’m referring to? In that case, just call that instead of what I have.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

We have an asp.net web page with a Hidden control on it: <asp:HiddenField runat=server
I have created a custom Server control that processes some data, stores that data
I've created a custom server control in ASP.NET to render a standard checkbox and
i have a <asp:HiddenField runat=server ID=ListTwoHiddenField /> i have some programming aspect how can
I have a user control I'm building (ASP.NET 3.5, using C#). This control is
I have a created a user control that is hopefully going to be used
I have a page that contains a HiddenField control, I register some javascript on
I have a PlaceHolder control inside of a ListView that I am using to
I have a partial view (user control) that is shared by my Create and
I have a set of accordion panes(dynamically created) contained in an Accordion control. Basically,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.