Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8660695
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T16:19:10+00:00 2026-06-12T16:19:10+00:00

I have a visual web-part for sharepoint with code below, but code of click-handler

  • 0

I have a visual web-part for sharepoint with code below, but code of click-handler is never firing. What’s wrong with my code?:

protected Button btApply;
protected void Page_Load(object sender, EventArgs e)
{
    ... some code ...
    btApply = new Button();
    btApply.Text = "Apply";
    btApply.CssClass += "InputControl";
    btApply.Click += new EventHandler(btApplyClick);
    this.Controls.Add(btApply);
    ... some code ...
}

protected void btApplyClick(object sender, EventArgs e)
{
    ... some code ...
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T16:19:11+00:00Added an answer on June 12, 2026 at 4:19 pm

    If you are to create controls dynamically, you have to to it in Page_PreInit. A quote from MCTS Self-Paced Training Kit (Exam 70-515): Web Applications Development with Microsoft .NET Framework 4, page 106:

    You can add controls to a form at run time by handling the
    Page.PreInit event (if you are not using master pages) or the
    Page.Init event (if you are using master pages and you are adding the
    control to a content page)

    The page (as well as web-part, user control, etc.) lifecycle is explained in MSDN “ASP.NET Page Life Cycle Overview” article.

    Also, see the quite similar question Event for Dynamically created Controls in ASP.Net

    So, just move your control creation and event wiring-up into Page_PreInit and you should be fine.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In SharePoint 2010 I have a visual web part that uses SP.UI.ModalDialog.showModalDialog The dialog
I am working on sharepoint 2010. I have created a simple visual web part
I have a visual web part (a simple form) with RequiredFieldValidators. But a problem
I have created a Hello World web part. When I pressed F5 in Visual
I have a web app that runs fine in Visual web developer. But when
This is my first Visual Studio 2010 / SharePoint 2010 Visual Web Part, and
I have a SharePoint WebPart project in visual studio. As part of the project
I'm writing a visual web part in visual studio for sharepoint 2010. The whole
I have created a visual web part that combines two other previously created web
I am creating a custom view in a SharePoint visual Web Part using ASP.NET

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.