Wrox Programmer Forums
|
BOOK: Beginning ASP.NET 3.5 : in C# and VB BOOK ISBN: 978-0-470-18759-3
This is the forum to discuss the Wrox book Beginning ASP.NET 3.5: In C# and VB by Imar Spaanjaars; ISBN: 9780470187593
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: Beginning ASP.NET 3.5 : in C# and VB BOOK ISBN: 978-0-470-18759-3 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old July 27th, 2012, 03:56 PM
Authorized User
 
Join Date: Jan 2011
Posts: 14
Thanks: 1
Thanked 0 Times in 0 Posts
Default HiddenField losing value on postback

Hi Imar,

I've a hidden field control on my page where its value gets lost on a postback. Is this normal? What I've is this:

In my html page, I've something like this:

<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">
<asp:HiddenField ID="Hidden1" runat="server" />
.
.
.
<asp:Button ID="btnCancel" Text="Cancel" runat="server" />
</asp:Content>

In my code behind, I've something like this:

protected void Page_Init(object sender, EventArgs e)
{
if (Hidden1.Value == null || Hidden1.Value == "")
{
.
.
.

Hidden1.Value = "0";
}
}

protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
.
.
.

Hidden1.Value = "1";
}
}

The problem is when I click the Cancel button, which invokes the Postback, thus the Page_Init event gets called but by that time the content of Hidden1 has been reset to empty/null. And there is no code in my html page or code behind where I clear the value of Hidden1. Am I missing something, or is this the normal behavior? I wound up having to use a session variable (which I don't really like using) instead of Hidden1 for the if condition in Page_Init.

Thanks for your time and help.

Ed
 
Old July 28th, 2012, 04:27 AM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

Hi Ed,

This is by design. During Init control values haven't been loaded from form and view state yet. Your code then overwrites the value and sets it to 0 again. However, if you'd debug and look at the control again in Page_Load, you'll see it still has a value of 1.

BTW: since this is not directly related to my book, you're better off posting questions like these in a more general ASP.NET category: http://p2p.wrox.com/asp-net-3-5-436/

Cheers,

Imar
__________________
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Follow me on Twitter

Author of Beginning ASP.NET 4.5 : in C# and VB, Beginning ASP.NET Web Pages with WebMatrix
and Beginning ASP.NET 4 : in C# and VB.
Did this post help you? Click the button below this post to show your appreciation!





Similar Threads
Thread Thread Starter Forum Replies Last Post
Forms losing focus Apocolypse2005 Visual Basic 2005 Basics 8 August 20th, 2009 08:36 AM
Variables losing their values jrensink78 ASP.NET 1.0 and 1.1 Basics 3 April 13th, 2006 01:09 PM
Losing variables bluemat XSLT 2 October 16th, 2004 01:59 AM
Losing style property stephanel VS.NET 2002/2003 10 August 12th, 2004 07:50 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.