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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T12:41:19+00:00 2026-05-25T12:41:19+00:00

I am creating the same button twice: once on Page_Load, and another on: Page_PreRender.

  • 0

I am creating the same button twice: once on Page_Load, and another on: Page_PreRender.
I expected to see the same behavior, however, the buttons created on Page_PreRender doesn’t work the way intended. (i.e. property “causeValidation” remains true).

“I want to disable “causeValidation” for the button in page_preRender, and i want it there in PreRender. Also, I want the Button_Click function to execute for both buttons, currently it does only for button created on “Page_load” “.

I am looking for a solution or an explanation for this behavior.

Thank You.

Take a look at the code:

protected void Page_Load(object sender, EventArgs e)
    {            
        form1.Controls.Add(GetButton("Button1", "Click"));
    }

    protected void page_PreRender(object sender, EventArgs e)
    {
        form1.Controls.Add(GetButton("Button3", "Click"));
    }

    private Button GetButton(string id, string name)
    {
        Button b = new Button();
        b.Text = name;
        b.ID = id;
        b.CausesValidation = false;
        b.Click += new EventHandler(Button_Click);
        b.OnClientClick = "ButtonClick('" + b.ClientID + "')";
        return b;
    }

    protected void Button_Click(object sender, EventArgs e)
    {
        ClientScript.RegisterClientScriptBlock(this.GetType(), ((Button)sender).ID, "<script>alert('Button_Click');</script>");
        Response.Write(DateTime.Now.ToString() + ": " + ((Button)sender).ID + " was clicked");
    }

and here is the javascript:

<script type="text/javascript">
    function ButtonClick(buttonId) {
        alert("Button " + buttonId + " clicked from javascript");
    }
</script>

Update:
let me add this: Both buttons execute the client’s script. This means that buttons are created. “see function GetButton() “.

Also, the reason why i have the button created “on preRender” instead of “on Page_Load” is that: if i have a form used to enter data into a table dynamically, and i add the data on button click, the the page will PostBack first, then execute the event handler, (i.e the data is added to the table but will show on NEXT postBack). So, PreRender is used to show the table AFTER it is updated by the button_click event.

I hope this addition is useful

  • 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-25T12:41:20+00:00Added an answer on May 25, 2026 at 12:41 pm

    enter image description here

    As you see from this image, that represents asp.net page life cycle (copied from MSDN), Event Handling precedes PreRender. So, in order to fire Button_Click the button itself has to be created before the Event Handling. Page PreInit is a preferred place to add dynamic controls.

    you can see it yourself, just add breakpoints at Page_Load, Page_PreRender, Button_Click. notice the order there are triggered?

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

Sidebar

Related Questions

I'm creating a button dynamically using JavaScript and at the same time assigning attributes
I am tired of creating the same image+text button over and over again, and
I have problem with handling dynamically created Buttons on Android. I'm creating N buttons
I am creating a vertical button bar with 3 buttons. How do I force
Beyond creating a dll with all the same functions with the same interface and
Creating a server-side socket will fail if I'm trying to use the same port
I am considering creating some JSP-tags that will always give the same output. For
I always run into the same problem when creating web pages. When I add
Should I use the same controller and view for editing and creating models in
I am creating a portal where many sites will run of the same MVC

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.