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
662 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

How our DevOps Teams Maximize Uptime

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us. Read the use case whitepaper.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A quick way to get a menu to work on our website, is using the Menu control and assign it to a web.sitemap using SiteMapDataSource. Example of web.sitemap file: (CODE) Sample code to add to the page menu: (CODE) Running the application, we wi…
IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

856 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