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 6475601
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T06:43:26+00:00 2026-05-25T06:43:26+00:00

I am trying to dynamically create controls and give them properties during run time.

  • 0

I am trying to dynamically create controls and give them properties during run time.

I have put my code inside the Page_Init event, when I run my website I can see my controls but when I click on the submit button an error occurrs saying “Object reference not set to an instance of an object”.

Here is the code I have used:

    //Creates instances of the Control    
    Label FeedbackLabel = new Label();
    TextBox InputTextBox = new TextBox();
    Button SubmitButton = new Button();
    // Assign the control properties

    FeedbackLabel.ID = "FeedbackLabel";
    FeedbackLabel.Text = "Please type your name: ";
    SubmitButton.ID = "SubmitButton";
    SubmitButton.Text = "Submit";
    InputTextBox.ID = "InputTextBox";
    // Create event handlers
    SubmitButton.Click += new System.EventHandler(SubmitButton_Click);

    // Add the controls to a Panel
    Panel1.Controls.Add(FeedbackLabel);
    Panel1.Controls.Add(InputTextBox);
    Panel1.Controls.Add(SubmitButton);
}

protected void SubmitButton_Click(object sender, EventArgs e)
{
    // Create an instance of Button for the existing control
    Button SubmitButton = (Button)sender;
    // Update the text on the Button
    SubmitButton.Text = "Submit again!";

    // Create the Label and TextBox controls
    Label FeedbackLabel = (Label)FindControl("FeedbackLabel");
    TextBox InputTextBox = (TextBox)FindControl("InputTextBox");
    // Update the controls
    FeedbackLabel.Text = string.Format("Hi, {0}", InputTextBox.Text);

How can I fix this error?

This Is the Stack Trace

[NullReferenceException: Object reference not set to an instance of an object.]
_Default.Page_PreInit(Object sender, EventArgs e) in c:\Users\bilalq\Documents\Visual Studio 2010\WebSites\WebSite3\Default.aspx.cs:31
System.Web.Util.CalliHelper.EventArgFunctionCaller(IntPtr fp, Object o, Object t, EventArgs e) +14
System.Web.Util.CalliEventHandlerDelegateProxy.Callback(Object sender, EventArgs e) +35
System.Web.UI.Page.OnPreInit(EventArgs e) +8876158
System.Web.UI.Page.PerformPreInit() +31
System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +328

  • 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-05-25T06:43:26+00:00Added an answer on May 25, 2026 at 6:43 am

    I suggest that you declare your controls outside the page_int and do your initialization in the init then use them with their name instead of find control.

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

Sidebar

Related Questions

I'm trying to dynamically create a connection string during compile time: private static string
I am trying to reuse shape data to dynamically create new shapes in code.
I have an application whereby I dynamically create controls on a form from a
I am trying to create a table dynamically in my code behind. My problem
I am trying to dynamically create a Bitmap Image using byte array using following
I'm trying to dynamically create a set of labels in my XUL Runner application.
I'm trying to dynamically create buttons at runtime with PyQT4.7 However, this being my
I've been trying to create a dynamically named JSON property but I keep hitting
Hi i am trying to create dropdownlists dynamically and populating the contents via an
I am trying to create a checkbox dynamically using following HTML/JavaScript. Any ideas why

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.