Accessing hidden-input values edited by JavaScript

ErnieStings picture ErnieStings · Jul 21, 2009 · Viewed 9.4k times · Source

I have dynamically created hidden input controls in a C# code-behind file, and then populated their values with JavaScript. I now want to access these variables in C#.

Firebug shows that the values do change with JavaScript, but I'm getting the original values back in the code behind. Any insight would be much appreciated.

JavaScript:

function injectVariables(){
    var hidden = document.getElementById("point1");
    hidden.value = 55;
    var hidden2 = document.getElementById("point2");
    hidden2.value = 55;
    var hidden3 = document.getElementById("point3");
    hidden3.value = 55;
    alert("values changed");
}

ASPX:

<asp:Button OnClick="Update_PlanRisks" OnClientClick="injectVariables()" Text="updatePlanRisks" runat="server" />

C#:

protected override void CreateChildControls()
{
    base.CreateChildControls();
    int planId = Convert.ToInt32(Request.QueryString.Get("plan"));
    planRisks = wsAccess.GetPlanRiskByPlanId(planId);

    foreach (PlanRisk pr in planRisks)
    {
        HtmlInputHidden hiddenField = new HtmlInputHidden();
        hiddenField.ID= "point" + pr.Id;
        hiddenField.Name = "point" + pr.Id;
        hiddenField.Value = Convert.ToString(pr.Severity);
        this.Controls.Add(hiddenField);
    }   
}

protected void Update_PlanRisks(object sender, EventArgs e)
{
    foreach (PlanRisk pr in planRisks)
    {
        int planRiskId = pr.Id;
        string planRiskName = "point" + pr.Id;
        HtmlInputHidden hiddenControl = (HtmlInputHidden) FindControl(planRiskName);
        string val = hiddenControl.Value;
    }
}

Answer

Josh Stodola picture Josh Stodola · Jul 21, 2009

This is one way to get the value from the request...

string point1 = Request.Form["point1"];