control loses state when page renders to browser

Posted on 2007-10-07
Last Modified: 2010-04-15
Control inherits from DataGrid
in the ItemCreated event this code generate a table containing buttons and adds them to the table

if (this.ExtraButtonCount > 0)
                              System.Web.UI.WebControls.TableRow extraButtonRow = new TableRow();
                              System.Web.UI.WebControls.TableCell buttonCell = new TableCell();
                              buttonCell.HorizontalAlign = HorizontalAlign.Right;
                              buttonCell.ColumnSpan = 3;
                              extraButtonRow.CssClass = this.CssPrefix + "btnCSS";
When the application in debug mode I can see all buttons in correct state the ones are set to enabled=false are disabled but when the application renders to the browser all buttons are enabled again
the prerender event does not have any code related to any of the buttons
it seems like I am missing something simple here but I can not seem to find it
Question by:Sammy
    LVL 22

    Expert Comment

    make sure you are making use of ispostback

    If Not IsPostBack Then
    ' not post back, set stuff up
    end if

    otherwise you may be resetting values unintentionally.

    without all the code, it is really hard to determine the problem

    LVL 26

    Expert Comment

    >>When the application in debug mode I can see all buttons in correct state the ones are set to enabled=false are disabled
    where are setting them to enabled = false in your code above .. is it set within the cssclass?

    LVL 27

    Author Comment

    the controls enabled state is set to false in a another method call
    LVL 10

    Expert Comment

    I think Kelvin is right. You might wanna check the use of IsPostBack. Since your buttons are generated dynamically. After post back, they aren't included in Controls hierarhical anymore.

    Make sure the adding procedure is invoked in the PageLoad method.

    LVL 27

    Accepted Solution

    Found out what was the problem
     CreateChildControls() was overwritten and the method which creates the buttons was Not added to the call which causes the control to keeps on clearing the control collections whenever any of the control is bound to a datafield
    solution was to add the method call as follow

    protected override void CreateChildControls()
                      base.CreateChildControls ();
                         //Ommited code
    public override void DataBind()
    //Ommitted code      
                   base.DataBind ();
    LVL 10

    Expert Comment

    Well done :)
    LVL 1

    Expert Comment

    Closed, 500 points refunded.
    Community Support Moderator

    Featured Post

    Highfive Gives IT Their Time Back

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    Join & Write a Comment

    Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
    International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
    In this sixth video of the Xpdf series, we discuss and demonstrate the PDFtoPNG utility, which converts a multi-page PDF file to separate color, grayscale, or monochrome PNG files, creating one PNG file for each page in the PDF. It does this via a c…
    In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor ( If you're interested in additional methods for monitoring bandwidt…

    755 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    20 Experts available now in Live!

    Get 1:1 Help Now