回发后设置隐藏字段值
我有一个组件可以确定回发事件的值。
protected void Button_Click(object s, EventArgs e)
{
HiddenField.Value = 5;
}
当然,还有更多涉及到的值,但 HiddenField 是一个带有 runat=server 设置的 asp:HiddenField 控件。我在我的 javascript 中:
var id = $("#<%= HiddenField.ClientID %>").val();
javascript 中的代码设置为仅在发生回发(不同的客户端单击事件)后运行,以便通过 QueryString 将隐藏字段值传递到另一个 URL(因为我不能这样做)回发时的响应重定向,并且客户端无论如何都希望它位于不同的页面中)。
我尝试添加:
ScriptManager.RegisterHiddenField(HiddenField, "Value", string.Empty);
到代码的 !Page.IsPostback 部分,但运行 javascript 时仍未设置 ID。
I have a component that determines a value on a postback event.
protected void Button_Click(object s, EventArgs e)
{
HiddenField.Value = 5;
}
There's more involved in the value of course, but HiddenField is an asp:HiddenField control with runat=server set. I have in my javascript:
var id = $("#<%= HiddenField.ClientID %>").val();
The code in the javascript is set to be run only after the postback has occured (a different client click event) for the purpose of passing the hidden field value via QueryString to another URL (since i can't do a response redirect on postbacks and the client wants it in a different page anyway).
I tried adding:
ScriptManager.RegisterHiddenField(HiddenField, "Value", string.Empty);
To a !Page.IsPostback section of code, but the ID is still not set when the javascript is run.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以在主页上添加一个全局 js 变量,然后设置该变量而不是隐藏字段。
页面.aspx
页面.aspx.cs
You could add a global js variable on your main page and then set that variable instead of the hidden field.
Page.aspx
Page.aspx.cs
这里是一个类似的问题。也许你可以用它来修改你正在做的事情。
您需要这样访问客户端 ID 吗?只要控件不在中继器或其他东西中,您应该能够毫无问题地设置控件的 ID。这可以简化您想要做的事情。
在示例中,他们使用 getElementById 获取隐藏字段并仅传递 HiddenField 的 ID。
Here is a similar issue. Maybe you can use it to modify what you are doing.
Do you need to access the client ID like that? You should be able to set the ID for the control with little issue as long as it is not in a repeater or something. That could simplify what you are trying to do.
In the example they use getElementById to get the hidden field and just pass the ID of the HiddenField.