Wrox Programmer Forums
Go Back   Wrox Programmer Forums > C# and C > C# 1.0 > C#
C# Programming questions specific to the Microsoft C# language. See also the forum Beginning Visual C# to discuss that specific Wrox book and code.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the C# 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 November 23rd, 2005, 06:18 AM
Registered User
Join Date: Nov 2005
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
Default Viewstate & FindControl problems in class


I created a working function wich i want to convert to a class. After some adjustments things work fine, except for two: ViewState and FindControl. The class needs to save a string to ViewState and retrieve it again when a button is clicked. FindControl is used in the class to retrieve a control from a Form (by its ID: the string from ViewState)in which the class is initiated. Here is my class so far:
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

namespace Navigatie
    public class BtnImageChange : ImageButton
        public string strPreviousBtn = " ";
        private const string ACTIVE_IMG_SUPL = "_a";
        ImageButton btnPreviousButton = new ImageButton();

        public BtnImageChange()            
            System.Diagnostics.Trace.WriteLine(" ");
            System.Diagnostics.Trace.WriteLine("----------- BtnImageChange Opnieuw Geinitieerd -----------");
            //this.EnableViewState = true;
            if (this.EnableViewState)
                System.Diagnostics.Trace.WriteLine("ViewState on startup contains: " + (string)ViewState["strActiveBtnID"]);
            this.EnableViewState = true;
        public void ChangeImage(object sender, ImageClickEventArgs e)
            //set image of current active button to active
            ImageButton btnToActive = (ImageButton)sender;

            string strInActiveUrl = btnToActive.ImageUrl; 
            string [] arrSplitInActiveString = strInActiveUrl.Split(new Char [] {'.'});
            strInActiveUrl = arrSplitInActiveString[0] + ACTIVE_IMG_SUPL + "." + arrSplitInActiveString[1];
            btnToActive.ImageUrl = strInActiveUrl;

            strPreviousBtn = (string)this.ViewState["strActiveBtnID"];
            System.Diagnostics.Trace.WriteLine("viewstate after postback : " + strPreviousBtn + " button ID = "  + btnToActive.ID);

            if(strPreviousBtn != null)
                //Change image of previous button to inactive
                System.Diagnostics.Trace.WriteLine("viewstate after postback and inside if : " + strPreviousBtn);
                btnPreviousButton = FindControl(strPreviousBtn) as ImageButton;

                string strActiveUrl = btnPreviousButton.ImageUrl; 
                string strNewInActiveUrl = strActiveUrl.Remove((strActiveUrl.Length-6),ACTIVE_IMG_SUPL.Length); 

                btnPreviousButton.ImageUrl = strNewInActiveUrl;
            this.ViewState["strActiveBtnID"] = btnToActive.ID;
            strPreviousBtn = (string)ViewState["strActiveBtnID"];
            System.Diagnostics.Trace.WriteLine("viewstate after setting : " + strPreviousBtn);
Now the viewstate saves fine, but is gone after the postback and the findcontrol returns null. My guess is that I should be pointing to the ViewState and the buttons in the form. If so, how do I do that and if not does anyone have another idea?

Old November 25th, 2005, 04:54 AM
Registered User
Join Date: Nov 2005
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts

Ok, never mind. I got things working by creating a custom control and adding a couple of functions to it, which i think is a cleaner solution too.

Thanks anyway.

Similar Threads
Thread Thread Starter Forum Replies Last Post
MFC Class wizard & vc++ 6 & message mapping C@uark Visual C++ 0 February 20th, 2007 06:47 PM
SimpleMail class problems derrida BOOK: Beginning PHP5, Apache, and MySQL Web Development ISBN: 978-0-7645-7966-0 0 July 6th, 2006 05:52 AM
urgent doubt between class & structure lgeetha C# 5 February 24th, 2005 09:27 AM
Chapter 5, Exercise 2, Class Problems nmstudent BOOK: Beginning Java 2 1 December 12th, 2003 07:08 PM
Window Class problems drb2k2 C++ Programming 1 October 1st, 2003 06:10 PM

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