Alexander Orlovskiy
Alexander Orlovskiy

Reputation: 21

How to change the value of input hidden on server-side

I have a form with hidden fields:

<form id="Form1" runat="server" style="width: 100%; height: 100%; overflow: hidden"       onsubmit="return false;">
<div>    
<input type="hidden" runat="server" id="TrackColors" value=""/>            
<input type="hidden" runat="server" id="Relogin" value=""/>
</div>
</form>

After Page_Load() on the server-side is called function:

protected void SomeFunction()
    {
        Dictionary<int, int> trackColors = new Dictionary<int, int>();
        if (!String.IsNullOrEmpty(TrackColors.Value))
            trackColors = ReadValues(TrackColors.Value);

    //if value is null or empty it's assigned to a different


        TrackColors.Attributes["value"] = FormValues(trackColors); //FormValues() return string

    //change is visible        
    }


    string FormValues(Dictionary<int, int> values)
    {
    string result = "";
    if (values == null || values.Count == 0)
        return result;

    foreach (KeyValuePair<int, int> p in values)
        result += p.Key + "@@" + p.Value + "^^";
    result = result.TrimEnd('^');

    return result;
}

If I change the selected field of ComboBox, the function is called:

<dx:ASPxTextBox ID="ColorTrackCarID" Visible="false" Text='<%# Eval("CarId") %>' />
                    <dx:ASPxComboBox ID="ASPxComboBox1" runat="server" SelectedIndex='<%# Eval("TrackColor") %>' 
                                    ValueType="System.String" Width="30" ShowImageInEditBox="true" 
                        ondatabinding="ASPxComboBox1_DataBinding">
                                    <ClientSideEvents SelectedIndexChanged="function (s,e) { 
                                   if (window.TrackColorChanged != null)TrackColorChanged(s,e); }" />
</dx:ASPxComboBox>



function TrackColorChanged(s, e) {
            var TrackColors = document.getElementById('TrackColors');
            if (TrackColors == null || TrackColors.value == "")
                return values;      
            //values is always emply    
        }

I understand the value of the form fields are not passed back to the client-side. The question is: How to pass these values ​​back?

And if I change the value on the server-side in Page_Load (), then the client can see everything, that is,

protected void Page_Load(object sender, EventArgs e)
{
TrackColors.Attributes["value"] = "bla-bla-bla";
//All changes are visible on the client-side
}

Thank you for your attention.

Upvotes: 1

Views: 3565

Answers (1)

Brian Mains
Brian Mains

Reputation: 50728

To make it even easier, replace your hidden fields with the control:

<asp:HiddenField id="X" runat="server" />

Which you can set the value on it directly:

X.Value = "XYZ";

This value can be passed from client to server, and vice versa, and works very easily. Not that you can't use a server-side input, but HiddenField handles a lot of that for you.

EDIT: Also, are you sure you're not overwriting the value? If you are doing this:

protected void Page_Load(object sender, EventArgs e)
{
TrackColors.Attributes["value"] = "bla-bla-bla";
//All changes are visible on the client-side
}

This will always change the value to "bla-bla-bla". You would want to wrap it in if (!Page.IsPostback) if you initialize it on page load.

Upvotes: 3

Related Questions