Solved

Viewstate is not maintained for the inner controls if i used Enabled/disabled property in my server control

Posted on 2007-11-27
2
659 Views
Last Modified: 2013-12-16
Gurus,

I am developing a server control which is used as a custom tag in asp.net pages. Basically this server control will be acting as a container (but not actuallly) control to render other controls within this. For ex. If the logged in user has appropriate rights, this server control allows the child controls to render as visible or enabled based on the logged in user rights.

i have written a logic in this server control's Render event procedure. here is the sample code snippet

if(!isuserinrole())
{
foreach (Control ctl in this.Controls)
                {
                    ctl.EnableViewState = true;
                    DisableControl(ctl);
                  ctl.RenderControl(writer);                    
                }
}
private void DisableControl(Control control)
        {
            if (control is WebControl)
            {
                ((WebControl)control).EnableViewState = true; //((WebControl)control).Enabled = false;
                ((WebControl)control).Attributes.Add("disabled", "disabled");
            }
            else if (control is HtmlControl)
                ((HtmlControl)control).Attributes.Add("Disabled", "True");
        }

this is working fine for first time. if i do postback and come back to the page, i am loosing all data in textboxes. what is the problem? am i missing anything in viewstate maintanance? please advise me. Thanks for your help.

Ram.
if(!isuserinrole())

{

foreach (Control ctl in this.Controls)

                {

                    ctl.EnableViewState = true;

                    DisableControl(ctl);

                  ctl.RenderControl(writer);                    

                }

}

private void DisableControl(Control control)

        {

            if (control is WebControl)

            {

                ((WebControl)control).EnableViewState = true; //((WebControl)control).Enabled = false;

                ((WebControl)control).Attributes.Add("disabled", "disabled");

            }

            else if (control is HtmlControl)

                ((HtmlControl)control).Attributes.Add("Disabled", "True");

        }

Open in new window

0
Comment
Question by:rampravee
2 Comments
 

Author Comment

by:rampravee
ID: 20399204
I found the solution for this. Instead of checking this logic in Render method, i checked it in oninit event procedure to achieve the same.

it works fine.  it may be helpful for other users when they really need this functionality.
0
 

Accepted Solution

by:
EE_AutoDeleter earned 0 total points
ID: 20968370
Cruthin,
Because you have presented a solution to your own problem which may be helpful to future searches, this question is now PAQed and your points have been refunded.

EE_AutoDeleter
0

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 asp.net 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…
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

747 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

14 Experts available now in Live!

Get 1:1 Help Now