无需脚本即可在 ASP.NET 中使用 HiddenField 调用事件

发布于 2024-09-09 01:13:18 字数 154 浏览 8 评论 0原文

看起来“asp:HiddenField”没有“AutoPostBack”属性,并且我在“OnValueChanged”事件方面遇到问题...当我填充隐藏字段时,我需要调用一个函数(C#),但是就我而言,什么也没有发生。而且我无法使用任何脚本。

可能是什么? =( 谢谢您的回复!

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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

送舟行 2024-09-16 01:13:18

根据您想要执行的操作,您可以向代码隐藏添加一个属性来设置 HiddenField 控件的值,然后使用该属性。然后,在设置器中,做任何你想做的事。

IE..

public string MyHiddenValue
{
    get { return hiddenField.Value; }
    set 
    {
        hiddenField.Value = value;
        if(MyHiddenValueChanged != null)
            MyHiddenValueChanged(this, new EventArgs());
    }
}
public event EventHandler MyHiddenValueChanged;

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;
意中人 2024-09-16 01:13:18

我正在更改我的完整答案,因为我完全错了。该死。

在此之前,我对 HiddenField 的 OnValueChanged 事件了解不多(好吧,我实际上对此一无所知,哈哈),但是在查看了有关该主题的 MSDN 后,似乎 OnValueChanged 事件是为了您可以检测该字段的值在回发之间是否已更改(即,自您上次更新该值以来,用户在他或她的浏览器中更改了该值)。如果您在发布到页面时更改 HiddenField 的值,则不会触发 OnValueChanged 事件。另一方面,如果页面上的脚本在下一页回发之前更改了 HiddenField 的值,那么它将触发该事件。所以在你的情况下它对你来说没有用。我之前建议使用不可见的 TextBox 并处理 TextChanged 事件,但同样毫无价值,因为 TextChanged 事件仅在用户更改时才会触发。

所以,这并不能回答你的问题,对此感到抱歉。

哦,是的,这是 MSDN 链接: HiddenField Web 服务器控制

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文