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 Answer 1

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

9
  • it still gives error for the path. When i open control template folder i cant see my webpart Commented Jul 17, 2015 at 12:57
  • As i read in this link sharepoint.stackexchange.com/questions/75542/… that you cannot find ascx in 15 hive in sharepoint 2013 Commented Jul 17, 2015 at 12:59
  • Please see my updated question Commented Jul 19, 2015 at 10:12
  • Please see my updated code and let me know whether it was helpfull Commented Jul 20, 2015 at 6:47
  • Deployment location is blank when i press F4. Remember i am using 2013 and i am using visual webpart Commented Jul 20, 2015 at 7:42

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.