Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Hidden field won't turn blank

Status
Not open for further replies.

bitseeker

Programmer
Nov 27, 2005
60
US
I'm using javascript to capture a text field value and put it in a hidden field value, and then submit the associated form. In Page_Load I extract and act on the hidden field value. All this works fine, except...

The hidden field value that is entered persists when the page is refreshed (ie. reloaded) via the browser UI, causing the field value to be recognized and acted on again in Page_Load.

I've tried to blank out the hidden field value in several different client-side and server side events, and can't seem to make it go away.

Any suggestions on how to fix this would be appreciated.

Thanks!

================= page and javascript

<%@ Page Language="VB" AutoEventWireup="false" CodeFile="Default.aspx.vb" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "
<html xmlns=" >
<head runat="server">
<title>Untitled Page</title>
<script src=JScript.js></script>
</head>

<BODY onload="javascript:ResetHiddenFields()"; onunload="javascript:ResetHiddenFields()";>
<form id="form1" method="post" runat="server">
<div>
<input type=hidden id="Hidden1" runat=server value = "" />
<input id="Button1" type="button" style="z-index:103; left: 180px; position: absolute; top: 8px;" onClick="javascript:Button1Click()" value="button" />
<input id="Text1" style="z-index: 104; left: 16px; position: absolute; top: 8px"
type="text" />
</div>

</form>

<script language=javascript>

function GetElem(varString){return document.getElementById(varString) }

function Button1Click() {
GetElem("Hidden1").value = ""
GetElem("Hidden1").value = GetElem("Text1").value
alert("Hidden1 = " + GetElem("Hidden1").value);
document.forms[0].submit();
}

function ResetHiddenFields() {
alert("resetting hidden value")
GetElem("Hidden1").value = ""
}
</script>

</body>
</html>


============= code behind

Partial Class _Default
Inherits System.Web.UI.Page

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

If Not IsPostBack Then
Me.Hidden1.Value = ""
End If

MsgBox("in Page_Load")
If IsPostBack Then
MsgBox("about to check hidden1")
If Trim(Me.Hidden1.Value) <> "" Then ' doesn't help, because not blank at this point
MsgBox("found non-blank hidden value = " & Me.Hidden1.Value)
Me.Hidden1.Value = "" ' doesn't help
End If
End If

End Sub

Protected Sub Page_LoadComplete(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.LoadComplete
Me.Hidden1.Value = "" ' didn't work
End Sub

Protected Sub Page_PreLoad(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.PreLoad
'Me.Hidden1.Value = "" ' this shuts it off before load, so not found, not good
End Sub

Protected Sub Page_PreRender(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.PreRender
'Me.Hidden1.Value = "" ' didn't work
End Sub

Protected Sub form1_Unload(ByVal sender As Object, ByVal e As System.EventArgs) Handles form1.Unload
'Me.Hidden1.Value = "" ' didn't work
End Sub

Protected Sub Page_PreRenderComplete(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.PreRenderComplete
'Me.Hidden1.Value = "" ' didn't work
End Sub

Protected Sub Hidden1_PreRender(ByVal sender As Object, ByVal e As System.EventArgs) Handles Hidden1.PreRender
'Me.Hidden1.Value = "" ' didn't work
End Sub
End Class

===============================================
 
Apparently, I need a bigger post-it on the side of my computer screen, which says "DO SEARCHES BEFORE POSTING HELP REQUESTS".....

This is a known issue, not specific to ASP.NET, and this article (and others) provide lots of information (including the fact that it's non-trivial).

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top