Question

My understanding is that viewstate controls that hiddenfields or any other read only control have not been interfered with between postback on the client side.

But How do I come about checking this is actually working. How can i simulate a postback with changed value in a hidden field to see what actually happen. I have implemented a sub:

Protected Sub HF1_ValueChanged(ByVal sender As Object, ByVal e As System.EventArgs)
       Handles HF1.ValueChanged

How can i test this? I tried inspect element in firefox but I cannot even find the hidden fields. I can see then on the page source but I cannot edit this.

Was it helpful?

Solution

I'm sure you're aware that changing the HiddenField will not post back automatically. Instead, it calls ValueChanged method when one of the server control post back.

Here is how you test - you can change the hidden field value at client side. Then click PostBack button to post back to server. HF1_ValueChanged will fire if hidden field is changed.

ASPX

<asp:HiddenField runat="server" ID="HF1" Value="1" 
   OnValueChanged="HF1_ValueChanged" />
<div id="button">Click this text to change Hidden Field</div><br/>
<asp:Button runat="server" ID="Button1" OnClick="Button1_Click" 
    Text="PostBack"/><br/>
<script src="/Scripts/jquery-1.8.2.js"></script>
<script>
    $(document).ready(function() {
        $("#button").click(function () {
            $("#<%= HF1.ClientID %>").val("2");
            alert("HF has new value: " + $("#<%= HF1.ClientID %>").val());
        });
    });
</script>

Code Behind

protected void HF1_ValueChanged(object sender, EventArgs e)
{
    // This method should be called 
    // only if hidden field is changed at client side.
}

protected void Button1_Click(object sender, EventArgs e)
{

}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top