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
Reply
 
Thread Tools Display Modes
  #1 (permalink)  
Old July 27th, 2012, 04: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" />
</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
Reply With Quote
  #2 (permalink)  
Old July 28th, 2012, 05:27 AM
Imar's Avatar
Wrox Author
Points: 71,164, Level: 100
Points: 71,164, Level: 100 Points: 71,164, Level: 100 Points: 71,164, Level: 100
Activity: 100%
Activity: 100% Activity: 100% Activity: 100%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 16,967
Thanks: 79
Thanked 1,560 Times in 1,537 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!
Reply With Quote
Reply


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 09:36 AM
Variables losing their values jrensink78 ASP.NET 1.0 and 1.1 Basics 3 April 13th, 2006 02:09 PM
Losing variables bluemat XSLT 2 October 16th, 2004 02:59 AM
Losing style property stephanel VS.NET 2002/2003 10 August 12th, 2004 08:50 PM



All times are GMT -4. The time now is 11:20 PM.


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