0

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)
        {

            base.CreateChildControls();

            Control control = Page.LoadControl(_ascxPath);

            Controls.Add(control);

        }
    }

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?

**Update1**

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;

        }
        else
        {
            RequiredName.Enabled = true;


        }
    }

Getting error on line RequiredName.Enabled = false;

RequiredName is asp validation control
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

"MyProject/Webparts/Mywebpart.ascx"

then the path which you should use will be like this

"~/_controltemplates/15/MyProject/Webparts/Mywebpart.ascx".

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

{SharePointRoot}\Template\CONTROLTEMPLATES\MyProject.Webparts\Webparts\

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, privacy policy and cookie policy

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