Search code examples
javascriptasp.netcode-behind

populating hidden field from codebehind, then, passing it back to codebehind as QueryString ASP/javascript


I have this code in page_load method:

string orgId = Session["Lineage"].ToString().Split(';')[depth];
hidOrg.Value = orgId;

This in the aspx page:

<input type="hidden" id="hidOrg" runat="server" />

and

function doRetrieveData(objVal) {

    var org = document.getElementById("hidOrg").value;
    //do stuff
    window.location.href = "summary.aspx?multiple=" + org
}

The problem is, I'm getting an object expected error when assigning var org the value of the hidden "hidOrg" field. Thanks for any help in advance.


Solution

  • ASP.NET generates server-control's ClientIDs according to the control's NamingContainer. So you need to pass the ClientID to your javascript function. Try this:

    var org = document.getElementById('<%=hidOrg.ClientID%>').value;
    

    Inline ASP.NET tags

    Other approaches:

    1. Pass the hiddenfield's value to the javascript function as well. You need to put the hiddenfield directly behind the DropDownList/Select and use nextSibling to get a reference to it.

      onchange="doRetrieveData(this.value,this.nextSibling.value)
      
    2. Set the Hidden-Field's ClientIdMode to Static (if using .NET 4.0)