Sign up ×
Stack Overflow is a community of 4.7 million programmers, just like you, helping each other. Join them; it only takes a minute:

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;
    }
}
}
share|improve this question
    
Visual WebPart OR WebPart ? If visual WP post the ascx, otherwise post the constructor of webpart. I suspect that you not have bind the click event on markup. – Max Mar 18 at 9:22
    
Hey, Max. Visual WebPart. I am adding the code to the ascx – NicVerAZ Mar 18 at 16:31
    
I think missing some object from the markup ? Button and Label ? – Max Mar 18 at 18:40
    
Indeed, it was truncated. – NicVerAZ Mar 18 at 20:21
    
Have you added the web part on a test page ? Do not try the click button when display web part from admin panel. – Max Mar 19 at 9:05

1 Answer 1

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

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.