views:

112

answers:

2

Hello!

Looks like the "asp:HiddenField" doesn't have an "AutoPostBack" property, and I'm having problems with the "OnValueChanged" event... I need to call a function (C#) when I populate the hidden field, but in my case nothing happens. And I cannot use any scripts.

What could it be? =( Thank you by any response!

+1  A: 

I am changing my complete answer because I was completely wrong. Darn.

I didn't know much about the OnValueChanged event for HiddenField before this (well, I actually didn't know anything about it, lol), but having checked out MSDN on the subject, it seems that the OnValueChanged event is there in order for you to detect if the value of the field has changed between postbacks (i.e. the user changed it in his or her browser since you last updated the value). If you change the value of the HiddenField when you post to the page, this doesn't fire the OnValueChanged event. If on the other hand a script on the page changed the HiddenField's value before the next page postback, then it would fire that event. So it's useless to you in your situation. My earlier suggestion to use an invisible TextBox and handle the TextChanged event is just as worthless, because the TextChanged event would fire only if the user changed it.

So, this doesn't answer your question, sorry about that.

Oh, and yes, here's the MSDN link: HiddenField Web Server Control

Cyberherbalist
I've read your previous answer, and I don't think that you are "completely" wrong, at least it make sense for me (then maybe I'm the "completely wrong" one LOL). Actually I too didn't know so much about this control, but your answer (especially the "between postbacks") help me to understand why so many examples uses scripts to populate the HiddenFields. Anyway, I'll be on my way to find a solution, even if have to be some scripting (why I can't use scripts? Well, ask to my boss, lol). Thank you very much! =D
Kira
+2  A: 

depending on what you're trying to do, you could add a property to your code-behind that sets the value of the HiddenField control, and then use that property instead. then, in the setter, do whatever you want.

i.e...

public string MyHiddenValue
{
    get { return hiddenField.Value; }
    set 
    {
        hiddenField.Value = value;
        if(MyHiddenValueChanged != null)
            MyHiddenValueChanged(this, new EventArgs());
    }
}
public event EventHandler MyHiddenValueChanged;
dave thieben
After all the water under the bridge, that's what I started working up to -- that @Kira could use events and event-handling to do what is wanted. Good on you, @dave thieben.
Cyberherbalist