Hidden Field altered through javascript not persisting on postback

Last post 03-28-2009 6:16 PM by Danny117. 3 replies.

Sort Posts:

  • Hidden Field altered through javascript not persisting on postback

    03-26-2009, 3:12 PM
    • Member
      162 point Member
    • Loki
    • Member since 09-05-2005, 8:14 PM
    • Posts 69

    I have a web user control with a hidden field on it.  When a javascript even ocurrs, I am trying to set a value in the hidden field, so that the value can be retained on postback.  The control is a collapsible panel extender that does not cause postback, uses jquery, and if postback ocurrs elsewhere on the page, it remembers if it is expanded or collapsed.

     The problem is that the javascript executes, but does not actually change the value in the hidden field.  If I use the dom explorer, the hidden fiend is still set to the default, and then when I debug, the next postback is still set to the default as well.

     

    ASCX code:

     

    <%@ Control Language="C#" AutoEventWireup="true" CodeFile="CollapsiblePanelExtender.ascx.cs" Inherits="Controls_SubControls_CollapsiblePanelExtender" %>
    
    <input id="hiddenCurrentState" type="hidden" runat="server" /> 

     

    Codebehind:

    1    using System;
    2    using System.Web;
    3    using System.Web.UI;
    4    using System.Web.UI.WebControls;
    5    using System.Text;
    6    
    7    public partial class Controls_SubControls_CollapsiblePanelExtender : System.Web.UI.UserControl
    8    {
    9        public string HeaderControlId { get; set; }
    10   
    11       public string BodyControlId { get; set; }
    12   
    13       public string CollapseAllControlId { get; set; }
    14   
    15       public string ShowAllControlId { get; set; }
    16   
    17       public CollapsedState DefaultState { get; set; }
    18   
    19       protected void Page_Load(object sender, EventArgs e)
    20       {
    21           if (!IsPostBack)
    22           {
    23               hiddenCurrentState.Value = DefaultState.ToString();
    24           }
    25       }
    26   
    27       protected override void OnPreRender(EventArgs e)
    28       {
    29           BuildJQueryScript();
    30           base.OnPreRender(e);
    31       }
    32   
    33       private void BuildJQueryScript()
    34       {
    35           StringBuilder script = new StringBuilder();
    36   
    37           script.Append("$(document).ready(function(){\n");
    38   
    39           //toggle based on current state
    40           script.Append("if ($(\"#" + hiddenCurrentState.ClientID + "\").attr(\"value\")==\"Expanded\")\n");
    41           script.Append("{\n");
    42           script.Append("$(\"#" + BodyControlId + "\").show();\n");
    43           script.Append("$(\"#" + hiddenCurrentState.ClientID + "\").val(\"Expanded\");\n");
    44           script.Append("}\n");
    45           script.Append("else\n");
    46           script.Append("{\n");
    47           script.Append("$(\"#" + BodyControlId + "\").hide();\n");
    48           script.Append("$(\"#" + hiddenCurrentState.ClientID + "\").val(\"Collapsed\");\n");
    49           script.Append("}\n");
    50   
    51   
    52           //toggle on click
    53           script.Append("$(\"#" + HeaderControlId + "\").click(function(){\n");
    54           script.Append("  $(this).next(\"#" + BodyControlId + "\").slideToggle(500)\n");
    55           script.Append("  return false;\n");
    56           script.Append("});\n");
    57   
    58           //collapse all
    59           if (!String.IsNullOrEmpty(CollapseAllControlId))
    60           {
    61               script.Append("$(\"#" + CollapseAllControlId + "\").click(function(){\n");
    62               script.Append("  $(\"#" + BodyControlId + "\").slideUp(500)\n");
    63               script.Append("  return false;\n");
    64               script.Append("});\n");
    65           }
    66   
    67           //show all
    68           if (!String.IsNullOrEmpty(ShowAllControlId))
    69           {
    70               script.Append("$(\"#" + ShowAllControlId + "\").click(function(){\n");
    71               script.Append("  $(this).hide()\n");
    72               script.Append("  $(\"#" + BodyControlId + "\").slideDown()\n");
    73               //script.Append("  $(\".message_list li:gt(4)\").slideDown()\n");
    74               script.Append("  return false;\n");
    75               script.Append("});\n");
    76           }
    77   
    78           script.Append("});\n");
    79   
    80           Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "CollapsiblePanelScript", script.ToString(), true);
    81       }
    82   }
    83   
    84   public enum CollapsedState
    85   {
    86       Expanded = 0,
    87       Collapsed = 1
    88   }
    
     
    Loki

    Broken Bokken Productions
  • Re: Hidden Field altered through javascript not persisting on postback

    03-27-2009, 6:29 AM
    • Star
      10,558 point Star
    • Danny117
    • Member since 12-16-2008, 9:30 AM
    • Royal Oak Michigan USA
    • Posts 1,837

    Can you show us the rendered javascript

    Good Luck



  • Re: Hidden Field altered through javascript not persisting on postback

    03-28-2009, 4:53 PM
    Answer

    Just guessing here (providing the generated JS code as Danny suggested would help more): but maybe Expanded & Collapsed should be replaced with 0 & 1 in the jQuery code as the generated JS has no notion of the server side enum, and so you'd be storing the value "Expanded" instead of zero which you server side code can interpret as CollapsedState.Expanded when cast to the enum.  Just a thought...

  • Re: Hidden Field altered through javascript not persisting on postback

    03-28-2009, 6:16 PM
    Answer
    • Star
      10,558 point Star
    • Danny117
    • Member since 12-16-2008, 9:30 AM
    • Royal Oak Michigan USA
    • Posts 1,837

    I just can't read script embedded into string.  Probably could help you easy.  All you need is a hidden asp:textbox on the client to get a value to postback.  Even query string.

     

    Good Luck



Page 1 of 1 (4 items)