Home All Groups Group Topic Archive Search About

Why usercontrol won't allow Request.Cookies

Author
2 Dec 2005 2:55 PM
Andy Sutorius via DotNetMonster.com
I'm getting an error, Request is not available in this context, when I run a
usercontrol and instantiate a class and call a property in that class which
calls Request.Cookies. The instantiated class inherits the Page class. Why
won't this work?

namespace ui
{
    public partial class topNav : System.Web.UI.UserControl
    {
        BaseUpdatePage SharedBaseMethods = new BaseUpdatePage();

        protected void Page_Load(object sender, System.EventArgs e)
        {
            lblUserAndDatabase.Text = "User: " + SharedBaseMethods.UserID + "
:: " + DateTime.Now.ToLongDateString() + " :: Database: " + SharedBaseMethods.
Database;
        }
}
}


Author
2 Dec 2005 4:36 PM
Stephen Woolhead
Just because you instantiate a page class does not mean it has a
HttpContext.  There is no point instantiating a Page class like that, put
your shared methods in a normal class and use HttpContext.Current.Request to
get the threads active context.

Stephen


"Andy Sutorius via DotNetMonster.com" <u9025@uwe> wrote in message
news:583cab0b00fb7@uwe...
Show quoteHide quote
> I'm getting an error, Request is not available in this context, when I run
> a
> usercontrol and instantiate a class and call a property in that class
> which
> calls Request.Cookies. The instantiated class inherits the Page class. Why
> won't this work?
>
> namespace ui
> {
>    public partial class topNav : System.Web.UI.UserControl
>    {
>        BaseUpdatePage SharedBaseMethods = new BaseUpdatePage();
>
>        protected void Page_Load(object sender, System.EventArgs e)
>        {
>            lblUserAndDatabase.Text = "User: " + SharedBaseMethods.UserID +
> "
> :: " + DateTime.Now.ToLongDateString() + " :: Database: " +
> SharedBaseMethods.
> Database;
>        }
> }
> }
>
> --
> Message posted via DotNetMonster.com
> http://www.dotnetmonster.com/Uwe/Forums.aspx/asp-net-web-controls/200512/1