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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T10:04:52+00:00 2026-06-03T10:04:52+00:00

Im creating some buttons based on a value of some runtime variable (i dont

  • 0

Im creating some buttons based on a value of some runtime variable (i dont even know how many).

My code for the button is as follows:

Button cancel = new Button();
            cancel.Text = "Cancel";
            cancel.ID = "cancelEnrollmentForStudent" + zapsanePredmetyList.ElementAt(i).ID.ToString() + "-" + i ;
            cancel.Click += new EventHandler(cancelEnrollment);
            string toCancel = selectedSubject.SelectedValue + ";" + studentToEnroll.SelectedValue;
            cancel.CommandArgument = toCancel;

while the code for the click method is

protected void cancelEnrollment(object sender, EventArgs e)
    {
        //do something when button clicked. 
        Button sourceButton = (Button)sender;
        string[] data = sourceButton.CommandArgument.Split(';');
    }

However, the click method is not firing up when the button is clicked. I suppose this has something to do with the fact, that I build the button at runtime. Cld someone advise on how to get this method fire up?

Thanks,
Ondrej

  • 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-03T10:04:54+00:00Added an answer on June 3, 2026 at 10:04 am

    You have to override OnInit add the code in OnInit method.

    protected override void OnInit(EventArgs e) 
    {
        base.OnInit(e);
        Button btn = new Button();
        btn.ID = "btn1";
        btn.Text = "Button1";   
        btn.Click += new EventHandler(btn_Click);
        this.form1.Controls.Add(btn);
    }
    
    void btn_Click(object sender, EventArgs e)
    {     
        Button bt = (Button)sender;
        bt.Text = "Changed";        
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've written some code for a button action. First I'm creating a button UI
I'm creating some 50 button dynamically. Text is getting set as followed: btn.Text=result.Rows[i]["Col1"].ToString()+"\n"+result.Rows[i]["Col2"].ToString() +"\n"+result.Rows[i]["Col3"].ToString();
I'm creating some custom components and backing code. I've created a Flex library project
I'm new to this iPad business, and I'm creating some buttons inside a scrollview
I am creating buttons dynamically for a user-based app. Now I have to tell
I am creating a panel and then adding some labels/buttons to it to form
I'm creating an app that creates buttons through code when an activity starts (the
I am having a go at creating some custom controls based on specific configurations
I'm creating an ASP.NET control dynamically based on a value selected in a dropdown;
I'm creating a custom PayPal Pay Now button based on the results of the

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.