Kentico Custom WebPart button on Click event

Roman Tom asked on April 10, 2019 11:39

I've created a custom webpart to register a new user. I added a button to register and on-click event register I write my code . when I add the web part to kentico it gives my error loading the webpart ,when I remove the on-click event it works fine . How I could solve this and I need to write the code inside the event. thanks..

Recent Answers

David te Kloese answered on April 10, 2019 13:01 (last edited on April 10, 2019 14:29)

What is the error? If not on the page try the Kentico Event Log.

Could you share the code you used for the registration and event itself?

Does it error when you add the Web Part to a template? or when you hit the page on the live site?

0 votesVote for this answer Mark as a Correct answer

Brenden Kehren answered on April 10, 2019 15:13

The overall livecycle of Kentico and the webparts is drastically different than standard forms development. My guess is you're trying to do some work in the page load event. This activity needs to happen sooner in the lifecycle, meaning you need to override the OnConentLoaded() event.

public override void OnContentLoaded()

Then in the SetupControl() method you assign your variables/properties. This needs to happen on each page load. Take a look at the BizForm.ascx.cs file in the /CMSWebparts/Bizforms directory for example.

protected void SetupControl()
    if (StopProcessing)
        // Do nothing
        viewBiz.StopProcessing = true;
        // Set BizForm properties
        viewBiz.FormName = BizFormName;
        viewBiz.SiteName = SiteName;
        viewBiz.UseColonBehindLabel = UseColonBehindLabel;
        viewBiz.AlternativeFormFullName = AlternativeFormName;
        viewBiz.ValidationErrorMessage = ValidationErrorMessage;

        // Set the live site context
        if (viewBiz != null)
            viewBiz.ControlContext.ContextName = CMS.Base.Web.UI.ControlContext.LIVE_SITE;

In the SetupControl() method you will want to assign your event handler to the button click event. For example:

btn1.Click += btn1_Click;

Lastly, I'd check the event log for any details of the error as well as debugging through your code locally to see where the error is coming in to play.

0 votesVote for this answer Mark as a Correct answer

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