简体   繁体   中英

SharePoint 2013 web part button click event not firing

I am building my first Hello World Web part using the Cloud 9 videos as tutorial.

I am using an Azure VM with SharePoint 2013, SQL Server 2012 and VS Premium 2013 Update 4.

The Web part is a simple textbox with a button and a label. The click event triggers a method which sets the text of the label to "Hello, " + the text from the textbox.

    protected void btnSubmitThis_Click(object sender, EventArgs e)
    {
        lblOutput.Text = "Hello, " + txtTextBox.Text;
    }

I have been able to push my Web part and insert it into my SharePoint 2013 home page.

The click even does nothing at all. Actually, when run in debug, I can see that the constructor, OnInit() and Page_Load() all execute but btnSubmitThis_Click() does not.

The Web part does have an ascx.g.cs file attached.

I can see that this file also includes the code below, showing that the event does handle the method properly:

        [global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Never)]
    [GeneratedCodeAttribute("Microsoft.VisualStudio.SharePoint.ProjectExtensions.CodeGenerators.SharePointWebP" +
        "artCodeGenerator", "12.0.0.0")]
    private global::System.Web.UI.WebControls.Button @__BuildControlbtnSubmitThis() {
        global::System.Web.UI.WebControls.Button @__ctrl;
        @__ctrl = new global::System.Web.UI.WebControls.Button();
        this.btnSubmitThis = @__ctrl;
        @__ctrl.ApplyStyleSheetSkin(this.Page);
        @__ctrl.ID = "btnSubmitThis";
        @__ctrl.Text = "Submit";
        @__ctrl.Click -= new System.EventHandler(this.btnSubmitThis_Click);
        @__ctrl.Click += new System.EventHandler(this.btnSubmitThis_Click);
        return @__ctrl;
    }

There are no errors generated that I can see, the click event simply does not call the method.

What could I be missing here?

I am ready to answer any question, as I really would love to figure this one out and I have spent a few days trying on my own, already.

Thanks a lot in advance for your help.

Code added by request:

ascx:

<%@ Assembly Name="$SharePoint.Project.AssemblyFullName$" %>
<%@ Assembly Name="Microsoft.Web.CommandUI, Version=15.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %> 
<%@ Register Tagprefix="SharePoint" Namespace="Microsoft.SharePoint.WebControls" Assembly="Microsoft.SharePoint, Version=15.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %> 
<%@ Register Tagprefix="Utilities" Namespace="Microsoft.SharePoint.Utilities" Assembly="Microsoft.SharePoint, Version=15.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<%@ Register Tagprefix="asp" Namespace="System.Web.UI" Assembly="System.Web.Extensions, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" %>
<%@ Import Namespace="Microsoft.SharePoint" %> 
<%@ Register Tagprefix="WebPartPages" Namespace="Microsoft.SharePoint.WebPartPages" Assembly="Microsoft.SharePoint, Version=15.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="HelloWorldWebPart.ascx.cs" Inherits="HelloWorld.HelloWorldWebPart.HelloWorldWebPart" %>
<p>
    Name:
    <asp:TextBox ID="txtTextBox" runat="server"></asp:TextBox>
&nbsp;<asp:Button ID="btnSubmitThis" runat="server" OnClick="btnSubmitThis_Click" Text="Submit" />
</p>
<asp:Label ID="lblOutput" runat="server"></asp:Label>

ascx.cs:

using System;
using System.ComponentModel;
using System.Web.UI.WebControls.WebParts;

namespace HelloWorld.HelloWorldWebPart
{
[ToolboxItemAttribute(false)]
public partial class HelloWorldWebPart : WebPart
{
    // Uncomment the following SecurityPermission attribute only when doing Performance Profiling on a farm solution
    // using the Instrumentation method, and then remove the SecurityPermission attribute when the code is ready
    // for production. Because the SecurityPermission attribute bypasses the security check for callers of
    // your constructor, it's not recommended for production purposes.
    // [System.Security.Permissions.SecurityPermission(System.Security.Permissions.SecurityAction.Assert, UnmanagedCode = true)]
    public HelloWorldWebPart()
    {
    }

    protected override void OnInit(EventArgs e)
    {
        base.OnInit(e);
        InitializeControl();
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        int Test = 0;
    }

    protected void btnSubmitThis_Click(object sender, EventArgs e)
    {
        lblOutput.Text = "Hello, " + txtTextBox.Text;
    }
}
}

It did not want to work as a sandboxed solution. Although debugging is harder in a farm solution, it seems to be more robust.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM