Custom Web Part in Portal Engine using Kentico 10

Bhadresh Panchal asked on July 3, 2018 09:17

How to use custom web part in portal engine kentico 10?

I created web part From Kentico 10 Documentation Custom development > Developing web parts. (https://docs.kentico.com/k10/custom-development/developing-web-parts/creating-new-web-parts)

When I integrate the web part with the Page, Button Click event is not trigger.

Correct Answer

Bhadresh Panchal answered on July 5, 2018 13:45

HI All, Thanks for your suggestion and comments. In my custom Master Template design

0 votesVote for this answer Unmark Correct answer

Recent Answers


David te Kloese answered on July 3, 2018 10:38

Hi,

Do you see any (script) errors on the page or in the Event log module? Could you share the code before and code behind of your Web Part?

You can also look at the default Kentico Web Parts, the code can be found in [your installation Path]\CMS\CMSWebParts\ Although they might be a bit more advanced.

0 votesVote for this answer Mark as a Correct answer

Dragoljub Ilic answered on July 3, 2018 10:38

Hi Bhadresh,

Do you have code like this in .ascx file:

<asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" />

And in .ascx.cs:

protected void Button1_Click(object sender, EventArgs e)
{
    //do stuff
}

You can check event log also, maybe button click is triggered but something else cause error.

Best regards, Dragoljub

0 votesVote for this answer Mark as a Correct answer

Bhadresh Panchal answered on July 3, 2018 11:36 (last edited on July 3, 2018 11:39)

@David, @Dragoljub Ilic I check with the code and event log, But I can't find any error. Using the ASPX Blank web template this custom webpart working fine. But when I checked with my portal engine webtemplate site button click event not trigger.

Please review below my code Helloworld.ascx

<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="HelloWorld.ascx.cs" CodeFile="~/CMSWebParts/MyWebParts/HelloWorld.ascx.cs" Inherits="CMSApp.CMSWebParts.MyWebParts.CMSWebParts_MyWebParts_HelloWorld" %> <asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" />
<asp:Label ID="Label1" runat="server" Text="Label" />

Helloworld.ascx.cs

public partial class CMSWebParts_MyWebParts_HelloWorld : CMSAbstractWebPart {

    protected void Page_Load(object sender, EventArgs e)
    {
        Button1.Text = DataHelper.GetNotEmpty(GetValue("ButtonText"), "Show time");
    }

    protected void Button1_Click(object sender, EventArgs e)
    {
        Label1.Text = DateTime.Now.ToString();
        Label1.Visible = true;
    }
}
0 votesVote for this answer Mark as a Correct answer

Dave Wanta answered on July 3, 2018 16:55

Since you are using the codebehind, make sure you compile your entire application. If you are would be developing a website (vs application), you could change codebehind to "codefile".

0 votesVote for this answer Mark as a Correct answer

vasu yerramsetti answered on July 4, 2018 07:09 (last edited on July 4, 2018 07:11)

@Bhadresh: Please update your code with below code snippet

public partial class CMSWebParts_aspx_DemoASPXWebPart : CMSAbstractWebPart
{
    #region "Properties"



    #endregion


    #region "Methods"

    /// <summary>
    /// Content loaded event handler.
    /// </summary>
    public override void OnContentLoaded()
    {
        base.OnContentLoaded();
        SetupControl();
    }
    /// <summary>
    /// Initializes the control properties.
    /// </summary>
    protected void SetupControl()
    {
        if (this.StopProcessing)
        {
            // Do not process
        }
        else
        {
            Label1.Visible = false;
        }
    }


    /// <summary>
    /// Reloads the control data.
    /// </summary>
    public override void ReloadData()
    {
        base.ReloadData();

        SetupControl();
    }

    #endregion

    protected void Button1_Click(object sender, EventArgs e)
    {
        Label1.Text = DateTime.Now.ToString();
        Label1.Visible = true;
    }
}

I hope this will help you.

0 votesVote for this answer Mark as a Correct answer

Bhadresh Panchal answered on July 4, 2018 07:22

Thank you for the comments @Vasu : I tried with the Same but seems Click event is not trigger. page load event is executed but button click event is not trigger.

0 votesVote for this answer Mark as a Correct answer

David te Kloese answered on July 4, 2018 09:24

Can you check if a default Kentico Web Part works?

0 votesVote for this answer Mark as a Correct answer

Bhadresh Panchal answered on July 4, 2018 09:31

@David

Yes other inbuild web parts working fine.

Even created Custom web part working fine in ASPX Blank Web template.

0 votesVote for this answer Mark as a Correct answer

vasu yerramsetti answered on July 4, 2018 09:58 (last edited on July 4, 2018 10:02)

@Bhadresh

I have done sample in my local the following steps and its working fine.

  1. Created custom web part from Application->Web Parts
  2. Added above code snippet from Visual studio
  3. Drag and Drop this web part into root.aspx design mode. Template location is CMSTemplates->BlankSiteASPX
  4. Tested from CMSDesk and its triggered the button click event.

Note: You can also debug your code to enable debug=true in web.config

Please let me know, where actually this code is been used?

0 votesVote for this answer Mark as a Correct answer

David te Kloese answered on July 4, 2018 09:59

Did the comment from Dave help or make sense to you?

Dave Wanta answered on July 3, 2018 16:55

Since you are using the codebehind, make sure you compile your entire application. If you are would be developing a website (vs application), you could change codebehind to "codefile".

Did you setup a website or webapplication project? (devnet.kentico.com/...web-app-vs-website-which-one-is-best)

0 votesVote for this answer Mark as a Correct answer

Bhadresh Panchal answered on July 4, 2018 10:33

Yes David, That I did but not work.

I created the Site with Sample blank web site in Web Application project. I did with all above comments but not work.

0 votesVote for this answer Mark as a Correct answer

David te Kloese answered on July 4, 2018 10:49

Can you just export the Web Part from Kentico:

https://content.screencast.com/users/DavidTK/folders/Jing/media/18fccb07-44db-4699-aac4-7cd2aa117203/2018-07-04_1045.png

This will create a .zip which also includes the code files.

1 votesVote for this answer Mark as a Correct answer

Bhadresh Panchal answered on July 4, 2018 11:55

@All

I found the cause that with Master Template. When I remove the Master Template From root level, itw working fine. with Master Template button trigger not executed. Is any idea what the cause in Master Template.

0 votesVote for this answer Mark as a Correct answer

David te Kloese answered on July 4, 2018 13:34

Is it a custom master template? Any other Web Parts or scripts on it?

0 votesVote for this answer Mark as a Correct answer

Bhadresh Panchal answered on July 4, 2018 13:48 (last edited on July 4, 2018 13:55)

@David,

Yes, It is the custom Master Template and used the Web Part zones. I define the links foe the css and scripts.

0 votesVote for this answer Mark as a Correct answer

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