My event processing in the web part does not start in SharePoint

I started coding something complicated, and then realized that my event handlers were not working, so I simplified the button with the event handler. Please see the code below and maybe you can tell me why it doesn't work?

using System;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using System.Web.UI;
using System.Web.UI.WebControls.WebParts;
using Microsoft.SharePoint;
using System.Web.UI.WebControls;
namespace PrinterSolution
{
    [Guid("60e54fde-01bd-482e-9e3b-85e0e73ae33d")]
    public class ManageUsers : Microsoft.SharePoint.WebPartPages.WebPart
    {
        Button btnNew;


        protected override void CreateChildControls()
        {
            btnNew = new Button();
            btnNew.CommandName = "New";
            btnNew.CommandArgument = "Argument";
            btnNew.Command += new CommandEventHandler(btnNew_Command);
            this.Controls.Add(btnNew);
        }

        void btnNew_Command(object sender, CommandEventArgs e)
        {
            ViewState["state"] = "newstate";
        }



        //protected override void OnLoad(EventArgs e)
        //{
        //    this.EnsureChildControls();
        //}

    }
}
+3
source share
1 answer

I had a similar problem. In my case, the button was contained in the panel, and although the buttons on the parent control worked correctly, the button on the control panel of the child panel did not execute.

, EnsureChildControls OnLoad , , CreateChildControls , . , .

, :

    protected override void OnLoad(EventArgs e)
    {
        EnsureChildControls();
        base.OnLoad(e);
    }

, , , , , . . , Survey list creating child controls PreRender Load.

:

Before making the change to call EnsureChildControls in the OnLoad override

:

After making the change to call EnsureChildCOntrols in the OnLoad override which shows the child controls being created in the correct place in the page life cycle

+2

Source: https://habr.com/ru/post/1748363/


All Articles