在回发后js中分配的隐藏值丢失

时间:2021-11-25 16:53:28

Here's my problem. I have a hidden field whose value I change through a javascript method. The problem is after postback the value is lost.

这是我的问题。我有一个隐藏的字段,其值通过javascript方法更改。问题是在回发后值丢失了。

How can I persist the value after postback?

如何在回发后保留该值?

Thanks!

谢谢!

.aspx File

.aspx文件

<asp:HiddenField ID="HiddenField1" runat="server" />
<asp:Button ID="BtnGuardar" runat="server" OnClick="BtnGuardar_Click" OnClientClick="return GridUpdateInfoOK()" />

.js file

.js文件

document.getElementById('<%= HiddenField1.ClientID %>').value = 'TEST';

.aspx.cs file

.aspx.cs文件

protected void BtnGuardar_Click(object sender, EventArgs e)
{
    String test = HiddenField1.Value;
}

3 个解决方案

#1


13  

You don't need to have the hidden input run at server. You can do:

您不需要在服务器上运行隐藏的输入。你可以做:

<input type="hidden" id="HiddenInput" name="HiddenInput" value="" />

Then when you post back you can access it like that:

然后,当您回发时,您可以像这样访问它:

protected void BtnGuardar_Click(object sender, EventArgs e)
{
    String test = Request.Form["HiddenInput"];
}

#2


5  

That doesn't work like that. The value is not present since the PageLoad, so won't be postbacked. Try using a TextBox with style="display:none".

这不是那样的。自PageLoad以来,该值不存在,因此不会回发。尝试使用带有style =“display:none”的TextBox。

#3


0  

Please use

请用

<asp:HiddenField ID="HiddenField1" runat="server" EnableViewState="true"/>

Then we will get the value after postback.

然后我们将在回发后获得该值。

All the properties of HiddenField are as bellow:

HiddenField的所有属性如下:

<asp:HiddenField
    EnableTheming="True|False"
    EnableViewState="True|False"
    ID="string"
    OnDataBinding="DataBinding event handler"
    OnDisposed="Disposed event handler"
    OnInit="Init event handler"
    OnLoad="Load event handler"
    OnPreRender="PreRender event handler"
    OnUnload="Unload event handler"
    OnValueChanged="ValueChanged event handler"
    runat="server"
    SkinID="string"
    Value="string"
    Visible="True|False"
/>

#1


13  

You don't need to have the hidden input run at server. You can do:

您不需要在服务器上运行隐藏的输入。你可以做:

<input type="hidden" id="HiddenInput" name="HiddenInput" value="" />

Then when you post back you can access it like that:

然后,当您回发时,您可以像这样访问它:

protected void BtnGuardar_Click(object sender, EventArgs e)
{
    String test = Request.Form["HiddenInput"];
}

#2


5  

That doesn't work like that. The value is not present since the PageLoad, so won't be postbacked. Try using a TextBox with style="display:none".

这不是那样的。自PageLoad以来,该值不存在,因此不会回发。尝试使用带有style =“display:none”的TextBox。

#3


0  

Please use

请用

<asp:HiddenField ID="HiddenField1" runat="server" EnableViewState="true"/>

Then we will get the value after postback.

然后我们将在回发后获得该值。

All the properties of HiddenField are as bellow:

HiddenField的所有属性如下:

<asp:HiddenField
    EnableTheming="True|False"
    EnableViewState="True|False"
    ID="string"
    OnDataBinding="DataBinding event handler"
    OnDisposed="Disposed event handler"
    OnInit="Init event handler"
    OnLoad="Load event handler"
    OnPreRender="PreRender event handler"
    OnUnload="Unload event handler"
    OnValueChanged="ValueChanged event handler"
    runat="server"
    SkinID="string"
    Value="string"
    Visible="True|False"
/>