Victor
Victor

Reputation: 1108

How to repair "Error Creating Control Request is not available in this context" in ASP.NET C#?

I have a solution where I can simply add webforms and elements to it (labels, textboxes, etc.), and everything is shown properly. However there are some webforms that show this error on the design view on every element on the form.

I noticed that the webforms that show this error are webforms that inherit from BasePage instead of System.Web.UI.Page

Example.

using System;
using System.Data;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Collections.Generic;

namespace Station.Payments
{
    public partial class detailsPayment : BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }
    }
}

here's my class BasePage.cs

using System;
using System.Text;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Collections.Generic;
using Core;//Custom Library

namespace Station
{
    public class BasePage : Page
    {
        protected override void OnInit(EventArgs e)
        {
            if (Request.IsAuthenticated)
            {
                base.OnInit(e);
            }

            else
            {
                Session.Abandon();
                Response.Redirect("~/Login.aspx");
            }
        }

        public UserLogin CurrentUser
        {
            get
            {
                //UserLogin is a class from Core
                return (UserLogin)Session["User"];
            }

            set
            {
                Session["User"] = value;
            }
        }

        protected StringBuilder DisableButton(object sender, bool isImage, bool blockAll)
        {
            StringBuilder disableButton = new StringBuilder();
            disableButton.Append("if (typeof(Page_ClientValidate) == 'function') { ");
            disableButton.Append("if (Page_ClientValidate() == false) { return false; }} ");
            disableButton.Append("this.disabled = true;");

            if (isImage)
            {
                disableButton.Append("this.value = 'PROCESSING...';");
                disableButton.Append("document.getElementById('" + ((ImageButton)sender).ID + "').disabled = true;");
                disableButton.Append("document.getElementById('" + ((ImageButton)sender).ID + "').src = '../images/saveInactive.png';");
                disableButton.Append(this.Page.GetPostBackEventReference((ImageButton)sender));
            }

            else
            {
                disableButton.Append("document.getElementById('" + ((Button)sender).ID + "').disabled = true;");
                disableButton.Append("document.getElementById('" + ((Button)sender).ID + "').value = 'CARGANDO...';");
                disableButton.Append(this.Page.GetPostBackEventReference((Button)sender));
            }

            disableButton.Append(";");

            return disableButton;
        }
    }
}

I have tried by getting rid of some of the commands there, however everytime I rebuild the solution I still get the error on any webform that inherits from BasePage.

Is there something there that is affecting the webform?

Upvotes: 0

Views: 837

Answers (1)

mshsayem
mshsayem

Reputation: 18028

I faced this kind of error in the past. Not sure if this helps. We have this in our base page's OnInit method :

if(Context != null && Context.Session != null && !Request.IsAuthenticated)
{
    Request.Redirect("~/Login.aspx");
}

It seems that Context != null && Context.Session != null part fixed the problem. Hope this helps.

Upvotes: 1

Related Questions