Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
474 views
in Technique[技术] by (71.8m points)

javascript - Hidden value assigned in js lost after postback

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.

How can I persist the value after postback?

Thanks!

.aspx File

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

.js file

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

.aspx.cs file

protected void BtnGuardar_Click(object sender, EventArgs e)
{
    String test = HiddenField1.Value;
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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"];
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...