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 January 11th, 2011, 04:54 PM
Authorized User
 
Join Date: Jan 2011
Posts: 14
Thanks: 1
Thanked 0 Times in 0 Posts
Default Saving control values

Hi Imar,

First, great book. I'm three quarters through reading it. Maybe because it's a beginner's book, it doesn't touch on saving control values. Anyway, my question is that I've an ASP.Net web app where I'd want to save the initial values of the controls after the page loads and save their values again when the user clicks the Accept button, so that I can compare if any of their values changed before I hit the server to do database updates. I'm using a method that recursively finds the controls on the form and saves their values into one string. My method goes something like this:

Code:
private void SaveCtrlValues(System.Web.UI.Control Page, ref String ctrlValues)
        {
            foreach (System.Web.UI.Control ctrl in Page.Controls)
            {

                if (ctrl is TextBox)
                    {
                        if (((TextBox)(ctrl)).Text.Trim().Length > 0)
                        {
                            ctrlValues += ((TextBox)(ctrl)).Text.Trim();
                        }
                    }

                else if (ctrl is DropDownList)
                    {
                        if (((DropDownList)(ctrl)).SelectedIndex > -1)
                        {
                            ctrlValues += ((DropDownList)(ctrl)).SelectedValue;
                        }
                    }
                else
                {
                    SaveCtrlValues(ctrl, ref ctrlValues);
                }
            }
My question is would this method always find the controls in the same order? If not, is there a better way to store and compare initial and updated values of the controls? Thanks for the help.
 
Old January 11th, 2011, 05:56 PM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

Hi there,

Yes, the order will be the same (the order is defined by the order of the controls in the mark up).

Are you just interested in knowing something has changed, or do you need to know what has changed? In the former case, are you familiar with the TextChanged event of the TextBox and the SelectedIndexChanged of the DropDownList controls?

BTW: questions not directly related to the book are better off in a more general ASP.NET category: http://p2p.wrox.com/asp-net-3-5-436/ That way, it's easier for others to join and learn from the discussion.

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!
 
Old January 14th, 2011, 02:26 AM
Authorized User
 
Join Date: Jan 2011
Posts: 14
Thanks: 1
Thanked 0 Times in 0 Posts
Default

Thanx, Imar. Sorry for posting my question in the wrong forum. I'm a newbie.

Well, I just needed to know if there has been a change in the data, not specifically which one, even though that might be nice, but then I'd imagine I'd have to put code in every control to capture the change.
 
Old January 14th, 2011, 03:31 AM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

You could hook up the TextChanged and other handlers dynamically in Page_Init in the same way as you're processing the controls now. That way, you can have a single method that keeps track of the changes.

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
Saving control state to persistent medium nmahesh567 .NET Framework 2.0 0 June 17th, 2007 02:28 PM
Saving Data to DB in dynamically created control ashokkumar ASP.NET 1.0 and 1.1 Basics 2 June 23rd, 2006 01:18 PM
how can i check for duplicate values before saving noor ASP.NET 1.0 and 1.1 Basics 3 June 10th, 2005 09:28 AM





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