I have created visual webpart which as .net validation control. When i deploy part and try to insert into page i get error

This page contains content or formatting that is not valid. You can find more information in the affected sections. + webpart

Then i found code which i put in my webpart

private const string _ascxPath = @"~/MyProject/Webparts/Mywebpart.ascx";

    protected override void CreateChildControls()

        WebPartManager wp = WebPartManager.GetCurrentWebPartManager(this.Page);

        if (wp.DisplayMode == WebPartManager.BrowseDisplayMode)


            Control control = Page.LoadControl(_ascxPath);



But when i debug this code it is not going inside if condition if (wp.DisplayMode == WebPartManager.BrowseDisplayMode)

What could be reason?

Also if i forcefully take debugger inside if condition then i get error path is not valid which i have put in variable _ascxPath What path should i put there?


Now i am trying to disable validation control as below but i get error

object reference not set to instance of object

Here is code

    protected override void CreateChildControls()

        bool pageIsEditMode = SPContext.Current.FormContext.FormMode == SPControlMode.Edit ? true : false;

        if (pageIsEditMode)

            RequiredName.Enabled = false;

            RequiredName.Enabled = true;


Getting error on line RequiredName.Enabled = false;

RequiredName is asp validation control

1 Answer 1


The user control which you deploy will be saved inside hive folder inside control templates.

If your path is folder path in solution is


then the path which you should use will be like this


If you are checking whether page is in edit mode or display mode then you can use bellow code.

bool pageIsEditMode = SPContext.Current.FormContext.FormMode == SPControlMode.Edit ? true : false

Here pageIsEditMode variable will be true if page is in edit mode else it will be false for design mode

Update 1

If you go the properties window (F4) of Mywebpart.ascx file in your solution it will be having the property called Deployment Location here you will be having value similar to this format


here you have to replace the

\ with /

and {SharePointRoot}\Template\CONTROLTEMPLATES with ~/_CONTROLTEMPLATES/15/

and in last add the user control file name in this case Mywebpart.ascx

filnal string will be the path of the user control you want to load.

As per my knowledge the object reference not found will come if your control is not initialized or not loaded


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.