Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
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 tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developersí questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Thread Tools Display Modes
  #1 (permalink)  
Old July 27th, 2012, 03:56 PM
Authorized User
Points: 64, Level: 1
Points: 64, Level: 1 Points: 64, Level: 1 Points: 64, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
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" />

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.

Reply With Quote
  #2 (permalink)  
Old July 28th, 2012, 04:27 AM
Imar's Avatar
Wrox Author
Points: 71,768, Level: 100
Points: 71,768, Level: 100 Points: 71,768, Level: 100 Points: 71,768, Level: 100
Activity: 100%
Activity: 100% Activity: 100% Activity: 100%
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 17,033
Thanks: 80
Thanked 1,580 Times in 1,557 Posts

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/


Imar Spaanjaars
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!
Reply With Quote

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off

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

All times are GMT -4. The time now is 01:24 PM.

Powered by vBulletin®
Copyright ©2000 - 2018, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.