Link to home
Start Free TrialLog in
Avatar of svcoder
svcoderFlag for United States of America

asked on

How can I extract a web.config section in a user control?

I have  a File Upload user control and I want to read the MaxRequestLength from the httpRuntime section of the web.config. My code works fine from a class that's derived from the Page class, but blows up when I try to compile the same code from a class derived from UserControl. The exact error I get is "An object reference is required for the nonstatic field, method, or property 'System.Web.UI.UserControl.Request.get " 

This is the error I get on the Request object, my config member and my section member. Any ideas on how to modify my code to work from a user control? I'm thinking it has something to do with the usercontrol existing within the parent page's control collection.  But I'm grasping at straws.
// Set the file size limit from the Web.Config
        System.Configuration.Configuration config = WebConfigurationManager.OpenWebConfiguration(Request.ApplicationPath);
        HttpRuntimeSection section = config.GetSection("system.web/httpRuntime") as HttpRuntimeSection;
        double maxFileSize = Math.Round(section.MaxRequestLength / 1024.0, 1);

Open in new window

Avatar of Martman100

You need to access the request object from the parent page.
Surely you can just use the static methods in System.Configuration.ConfigurationManager as that runs in the current application context and will grab the config from the web app using the control...
Avatar of Martman100

Link to home
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
which method of user control you have this code in. what is the sequence of the execution....

i think by the time execution reaches you code, the request object is not yet available. can you give more details

Did you ever find a solution to this?
Avatar of svcoder


Thanks, you set me on the right path.  I had to access the Page's request object.