Error loading custom webpart

Jeff Klunder asked on April 5, 2017 12:10

Hi there,

I'm creating a custom webpart using the Kentico documentation. Added a new folder inside CMSWebParts and added both .ascx and .ascx.cs files there. Also registered the webpart to Kentico and I can add it to a webpart zone. Only weird thing is I'm getting this message: **[Error loading the WebPart 'ElkeDagGeluk' of type 'ElkeDagGeluk'] **

Not really sure where to start debugging. Any tips would be appreciated.

Recent Answers


Mariia Hrytsai answered on April 5, 2017 12:17 (last edited on April 5, 2017 12:17)

Does this web part inherit CMSAbstractWebPart? Have you checked Event log to see what error is logged for this web part?

3 votesVote for this answer Mark as a Correct answer

Jeff Klunder answered on April 5, 2017 12:42

Hi Mariia,

Yes, the webpart does inherit CMSAbstractWebPart. I'm fairly new to Kentico; where do I find this Event log you speak of? Perhaps there I can find a more concrete message of whats wrong...

0 votesVote for this answer Mark as a Correct answer

Jeff Klunder answered on April 5, 2017 12:44

Already found the event log...

0 votesVote for this answer Mark as a Correct answer

Jeff Klunder answered on April 5, 2017 12:46

Ah, I think I found the reason: 'Too many characters in character literal'. Now the next obstacle: how to fix this...

0 votesVote for this answer Mark as a Correct answer

Jeff Klunder answered on April 5, 2017 13:02

So, with some trial and error, I found out that on the one hand there are 'too many characters in character literal'. On the other hand, I get this message...

error ASPNET: Make sure that the class defined in this code file matches the 'inherits' attribute, and that it extends the correct base class

I've double checked the Inherits part in the .ascx file and what it says in the .ascx.cs file; their both the same.

0 votesVote for this answer Mark as a Correct answer

Jeff Klunder answered on April 5, 2017 13:55

The 'too many chars' message was a quick fix. I used single quotes instead of double. Only message left is the inherit one. Not sure how to fix this, since these are equal amongst both files.

0 votesVote for this answer Mark as a Correct answer

Mariia Hrytsai answered on April 5, 2017 14:01

Does this web control inherit CMSAbstractWebPart?

0 votesVote for this answer Mark as a Correct answer

Jeff Klunder answered on April 5, 2017 14:03

Yes, it does

0 votesVote for this answer Mark as a Correct answer

Jeff Klunder answered on April 5, 2017 14:14

Should be fine, right?

// inside .ascx file
<%@ Control Language="C#" AutoEventWireup="true" Inherits="CMSWebParts_LuckyDay_ElkeDagGeluk" CodeFile="~/CMSWebParts/LuckyDay/ElkeDagGeluk.ascx.cs" %>

// inside .ascx.cs file
public partial class CMSWebParts_LuckyDay_ElkeDagGeluk : CMSAbstractWebPart
0 votesVote for this answer Mark as a Correct answer

Mariia Hrytsai answered on April 5, 2017 14:32

Yes

0 votesVote for this answer Mark as a Correct answer

Kenny Deblaere answered on April 5, 2017 16:43

Hi Jeff

Does your CS file have a namespace? Because, looking from the CodeFile, your inherits needs the namespace.

This is how I think it should look:

<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="CMSWebParts_LuckyDay_ElkeDagGeluk.ascx.cs" Inherits="CMSApp.CMSWebParts.CMSWebParts_LuckyDay_ElkeDagGeluk" %>

I'm not sure it's the solution, but I'm guessing it has something to do with it.

0 votesVote for this answer Mark as a Correct answer

Jeff Klunder answered on April 5, 2017 16:48

Hi Kenny,

No, it hasn't a namespace. I've did find a couple of articles about that though. I'll edit the line per your answer to see what it does..

0 votesVote for this answer Mark as a Correct answer

Trevor Fayas answered on April 5, 2017 16:52

Would you be able to post the actual code behind so we can analyze?

0 votesVote for this answer Mark as a Correct answer

Jeff Klunder answered on April 5, 2017 16:54

Now this appears in the event log: Message: Could not load type 'CMS.CMSWebParts.CMSWebParts_LuckyDay_ElkeDagGeluk'. At least the error changed (=

0 votesVote for this answer Mark as a Correct answer

Trevor Fayas answered on April 5, 2017 16:58

If you can get us the aspx and aspx.cs file, we can help figure out the error for ya, hard to guess at the cause of the errors without the code to look at!

0 votesVote for this answer Mark as a Correct answer

Jeff Klunder answered on April 5, 2017 17:00

The CMS bit I changed on purpose, because I don't have a CMSApp folder.. both CMSApp as CMS gives back the same error though..

0 votesVote for this answer Mark as a Correct answer

Jeff Klunder answered on April 5, 2017 17:18

Hi Trevor,

Sure I can, here they are...

.ascx file

 <%@ Control Language="C#" AutoEventWireup="true" CodeBehind="CMSWebParts_LuckyDay_ElkeDagGeluk.ascx.cs" Inherits="CMS.CMSWebParts.CMSWebParts_LuckyDay_ElkeDagGeluk" %>

<div class="usp usp--sec usp--with-visual visual--left" data-scroll-target="true" data-scroll-offset="450">

Afbeelding van een radio

    <div class="usp__content ml-negative pt-0 pb-0">
        <div class="usp__content__inner">
            <h2 class="usp__title usp__title--large mb">
                <asp:Literal runat="server" ID="SectionTitle"/>
            </h2>

            <div class="cta">
                <a href="#" class="btn btn--large btn--pri"><span>Primaire button</span></a>
            </div>

            <div class="usp__visual is-mobile">
                <div class="image" style="background-image: url();"></div>
            </div>
        </div>
    </div>
</div>

.ascx.cs file using System; using System.Data; using System.Collections; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; //using System.Web.UI.HtmlControls;

//using CMS.UIControls; using CMS.PortalControls; using CMS.helpers;

public partial class CMSWebParts_LuckyDay_ElkeDagGeluk : CMSAbstractWebPart {

public override void OnContentLoaded()
{
    base.OnContentLoaded();
    SetupControl();
}

protected void SetupControl()
{
    if(this.StopProcessing)
    {
        // Do not process
    }
    else
    {

    }
}

public override void ReloadData()
{
    base.ReloadData();
    SetupControl();
}

public string ElementTitle {
    get {
        var elementValue = this.GetValue("Title");

        if(elementValue != null) {
            return elementValue.ToString();
        } else {
            return null;
        }
    }
}

protected void Page_PreRender(object sender, EventArgs e)
{
    SectionTitle.Text = ElementTitle;
}

}

0 votesVote for this answer Mark as a Correct answer

Trevor Fayas answered on April 5, 2017 17:26 (last edited on April 5, 2017 17:28)

Thanks, try this for the ASCX portion's header

If Portal Method (non compiled site) <%@ Control Language="C#" AutoEventWireup="true" CodeFile="CMSWebParts_LuckyDay_ElkeDagGeluk.ascx.cs" Inherits="CMSWebParts_LuckyDay_ElkeDagGeluk" %>

If compiled site: <%@ Control Language="C#" AutoEventWireup="true" CodeBehind="CMSWebParts_LuckyDay_ElkeDagGeluk.ascx.cs" Inherits="CMSWebParts_LuckyDay_ElkeDagGeluk" %>

And for the ascx.cs, it's using CMS.Helpers; not using CMS.helpers; , this is probably the main issue is you need the capital H in Helpers.

Try that and it should compile!

0 votesVote for this answer Mark as a Correct answer

Jeff Klunder answered on April 6, 2017 09:20

Changed .helpers in .Helpers.

Using the first option for the .ascx header gives me Message: The file '/luckyday-kentico/CMSWebParts/LuckyDay/CMSWebParts_LuckyDay_ElkeDagGeluk.ascx.cs' does not exist.

Trying the second option gives back Message: Could not load type 'CMSWebParts_LuckyDay_ElkeDagGeluk'.

0 votesVote for this answer Mark as a Correct answer

Kenny Deblaere answered on April 6, 2017 09:24

Hi Jeff

Can you add the location in your project where the webparts are stored? Or maybe a screenshot of your project structure?

Kind regards

Kenny

0 votesVote for this answer Mark as a Correct answer

Jeff Klunder answered on April 6, 2017 09:29

Hi Kenny,

Not really sure what you mean by the location. Both .ascx and .ascx.cs files are located inside the CMS/CMSWebParts/LuckyDay folder.

Does that give you enough information?

0 votesVote for this answer Mark as a Correct answer

Kenny Deblaere answered on April 6, 2017 09:34

Hi Jeff

I guess, by renaming your solution, the paths used by Kentico have changed and that's the reason the file isn't found. Maybe changing your CMSApp project to it's default name will fix it?

0 votesVote for this answer Mark as a Correct answer

Jeff Klunder answered on April 6, 2017 09:51 (last edited on April 6, 2017 10:00)

Hi Kenny,

I'm afraid renaming isn't an option, since I'm not the only developer working on the project.

Just curious, what makes you think something is renamed? I don't think we renamed anything; we did add a new site within Kentico and working on that one, not the default Blank Site or anything like that.

0 votesVote for this answer Mark as a Correct answer

Jeff Klunder answered on April 6, 2017 09:56 (last edited on April 6, 2017 10:01)

I haven't tried it yet but couldn't it be the filename that's wrong? The message says Message: The file '/luckyday-kentico/CMSWebParts/LuckyDay/CMSWebParts_LuckyDay_ElkeDagGeluk.ascx.cs' does not exist., but the filename is only ElkeDagGeluk.ascx.cs.

When the filename is changed this message appears: Message: D:\Repositories\luckyday-cms\CMS\CMSWebParts\LuckyDay\ElkeDagGeluk.ascx.cs(13): error ASPNET: Make sure that the class defined in this code file matches the 'inherits' attribute, and that it extends the correct base class (e.g. Page or UserControl).

0 votesVote for this answer Mark as a Correct answer

Kenny Deblaere answered on April 6, 2017 10:00

It looks like your filepath is wrong, but as I said, I do not have any view on your project structure.

Maybe you try to just to set

<%@ Control Language="C#" AutoEventWireup="true" CodeFile="ElkeDagGeluk.ascx.cs" Inherits="ElkeDagGeluk" %>

This could maybe help. Otherwise, can you upload both of the files?

0 votesVote for this answer Mark as a Correct answer

Jeff Klunder answered on April 6, 2017 10:20

These are screenshots from the two files and my folder-structure. Hopefully that helps you a bit more...

Image Text

Image Text

0 votesVote for this answer Mark as a Correct answer

Kenny Deblaere answered on April 6, 2017 10:27

I see you don't have a designer file?

Perhaps this could fix the error you have by recreating your file in VS?

0 votesVote for this answer Mark as a Correct answer

Jeff Klunder answered on April 6, 2017 10:41

A designer file? I'll google that one, not sure what that is. I'm not using visual studio for my front-ending stuff (as of yet).. All could be done in another ide right? Or is vs required?

0 votesVote for this answer Mark as a Correct answer

Kenny Deblaere answered on April 6, 2017 11:00

When you create a custom webpart in VS everything is done for you. I'm not sure what other IDE's are doing for you.

I only know I'm having a Web Forms User Control adds the correct things

0 votesVote for this answer Mark as a Correct answer

Jeff Klunder answered on April 6, 2017 11:04 (last edited on April 6, 2017 11:45)

Ah okay. I'll check in VS then, perhaps it will work.

[edit] Actually I would like to use my trusted ide Sublime for my coding. Learning to use a whole different program just to create a webpart seems a bit much to be honest. Per earlier replies from Mariia and Trevor it looks to me that all code are correct, so it seems to me there's something else wrong or missing.

0 votesVote for this answer Mark as a Correct answer

Jeff Klunder answered on April 6, 2017 12:09

Not sure if it's the correct way, but tried creating a webpart within visual studio. It got me a <%@ rule which differs a little bit from my original one. Changed the line inside the webpart, but to no avail. So the quest continues (=

0 votesVote for this answer Mark as a Correct answer

Kenny Deblaere answered on April 6, 2017 12:12

Jeff

You can create the webpart into your VS and edit it in Sublime?

Just copy the content from your webpart into de frontend, press F7 and copy the partial class into the backend code?

That should normally work

0 votesVote for this answer Mark as a Correct answer

Suneel Jhangiani answered on April 6, 2017 12:51

I think if you force a namespace it would be the quickest fix. You can do this by setting the ASCX file as:

<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="CMSWebParts_LuckyDay_ElkeDagGeluk.ascx.cs" Inherits="LuckyDay.ElkeDagGeluk" %>

Then in the .ASCX.CS codebehind file wrap the Class in a Namespace:

using ....

namespace LuckyDay
{
    public partial class ElkeDagGeluk
    {
        // class code...
    }
}
0 votesVote for this answer Mark as a Correct answer

Jeff Klunder answered on April 6, 2017 13:13 (last edited on April 6, 2017 13:16)

@Kenny: I tried the approach of creating the webpart in VS and then transferring it to Sublime. Didn't make a difference.

@Suneel: Tried your approach. Message: Could not load type 'CMSWebPart.LuckyDay.ElkeDagGeluk'. comes back now..

Isn't there a tutorial of sorts I could try? I've googled for it but every time I get referred to the Kentico site. Watched a youtube video of someone creating a custom webpart (the famous Hello World), but the steps the video-maker does, I did also..

0 votesVote for this answer Mark as a Correct answer

Trevor Fayas answered on April 6, 2017 15:27

Jeff, the "Documentation" link in this site has Kentico's thorough documentation, including how to create custom web parts.

https://docs.kentico.com/k10/custom-development/developing-web-parts/creating-new-web-parts

When i provided the headers for you, i could not see the file name and assumed what you had in there was the file name. Try ElkeDagGeluk.aspx.cs as the file name in the aspx top.

0 votesVote for this answer Mark as a Correct answer

Suneel Jhangiani answered on April 6, 2017 15:33

I think we are getting a bit nearer if you did use the code I pasted earlier. All you need to do is change the Codebehind Namespace to match the type name that could not be loaded:

using ....

namespace CMSWebPart.LuckyDay
{
    public partial class ElkeDagGeluk
    {
        // class code...
    }
}
0 votesVote for this answer Mark as a Correct answer

Suneel Jhangiani answered on April 6, 2017 15:44

Trevor did also mention Portal mode in one of his posts, hence I think we need to see if your site is setup as a Website or a Web Application. The easy way to do this is to open and working ASPX or ASCX file and see if it uses CodeBehind or CodeFile to indicate where the C# code is being stored.

If it is using CodeBehind you are developing as a Web Application and hence would need to compile the application so that a new CMS.DLL file is generated in the bin folder.

0 votesVote for this answer Mark as a Correct answer

Jeff Klunder answered on April 6, 2017 15:46

I'm a little confused now, sorry...

This is the first line in the .ascx file

<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="CMSWebParts_LuckyDay_ElkeDagGeluk.ascx.cs" Inherits="CMSWebPart.LuckyDay.ElkeDagGeluk" %>

And this is whats inside the .ascx.cs file

using System;

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

using CMS.PortalControls; using CMS.Helpers;

namespace CMSWebPart.LuckyDay { public partial class ElkeDagGeluk : CMSAbstractWebPart {

    public override void OnContentLoaded()
    {
        base.OnContentLoaded();
        SetupControl();
    }

    protected void SetupControl()
    {
        if(this.StopProcessing)
        {
            // Do not process
        }
        else
        {

        }
    }

    public override void ReloadData()
    {
        base.ReloadData();
        SetupControl();
    }

    public string ElementTitle {
        get {
            var elementValue = this.GetValue("Title");

            if(elementValue != null) {
                return elementValue.ToString();
            } else {
                return null;
            }
        }
    }

    protected void Page_PreRender(object sender, EventArgs e)
    {
        SectionTitle.Text = ElementTitle;
    }

}

}

0 votesVote for this answer Mark as a Correct answer

Jeff Klunder answered on April 6, 2017 15:48

@Suneel Looking in a existing webpart folder, the .ascx file uses CodeBehind

0 votesVote for this answer Mark as a Correct answer

Jeff Klunder answered on April 7, 2017 13:46

If it is using CodeBehind you are developing as a Web Application and hence would need to compile the application so that a new CMS.DLL file is generated in the bin folder.

Thinking this is the case, should I (re)build the solution so that the mentioned .dll file gets generated?

0 votesVote for this answer Mark as a Correct answer

Suneel Jhangiani answered on April 7, 2017 14:45

Yes, you SHOULD rebuild (compile) the solution.

You could just change CodeBehind to CodeFile in your ascx page. However, mixing modes like that would certainly not be recommended (http://stackoverflow.com/questions/10110587/using-codefile-in-web-application-project)

0 votesVote for this answer Mark as a Correct answer

Jeff Klunder answered on April 7, 2017 15:11

Hmz, a (re)build doesn't seem to fix the issue either.

I'm beginning to doubt if what I want is actually possible. I'm just trying to create a webpart, nothing fancy...

0 votesVote for this answer Mark as a Correct answer

Trevor Fayas answered on April 7, 2017 15:30

Jeff, email me, tfayas@hbs.net

If you're free today, i'll do a webex (screen share) and get ya going, shouldn't be that hard :P

0 votesVote for this answer Mark as a Correct answer

Jeff Klunder answered on April 21, 2017 08:47

@Trevor, I totally missed your last comment. For the time being, the project doesn't require custom webparts, so for now I won't continue developing. Perhaps in the (near) future I'll come back on this.

Thanks to all for the quick replies.

0 votesVote for this answer Mark as a Correct answer

Jeff Klunder answered on April 25, 2017 10:19

Hi all,

So after some more playing around, together with another developer, we discovered the following...

Creating a webpart through Kentico admin adds the needed files automatically in the folder you designate. I got three files; a .ascx one, a .ascx.cs one and a .ascx.designer.cs one. Still got the initial error.

We've looked at both first lines inside the .ascx file and found out that there was one difference. My one had 'CodeBehind'; the one from the other developer had 'CodeFile'. I changed my line to CodeFile and all looks fine now. I can add the webpart (this worked before) and I also see the content.

So finally my custom made webpart seems to be working.

One thing that still bugs me is the fact that both myself and the other developer are using Kentico 10 and created the webpart the same way. Why does the difference in files occur? Also I got the .ascx.designer.cs file which the other developer did not get.

The initial problem of creating and using custom webparts is now solved, but still feels buggy...

1 votesVote for this answer Mark as a Correct answer

Janiek Colpaert answered on January 23, 2018 12:33

Hey Jef,

Thanks for your feedback and the fix. Actually had the same error for some unknown reason. All my other custom webparts work fine with "CodeBehind", but my last added one I had to change the code to "CodeFile".

0 votesVote for this answer Mark as a Correct answer

   Please, sign in to be able to submit a new answer.