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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T22:57:11+00:00 2026-05-20T22:57:11+00:00

I have a page with a dynamically created command button. I need to dynamically

  • 0

I have a page with a dynamically created command button.

I need to dynamically wire up a click event so I can grab the CommandArgument of the clicked button.

Button b = new Button();
b.ID = "btnTrigger2";
b.CssClass = "hiddenBtn";
b.CommandName = "lbl3";
b.Click += new EventHandler(btnTrigger_Click);

The problem is the last line – I can’t wire up an EventHandler this way – I need to use the standard EventArgs instead of CommandEventArgs.

Anyone have any suggestions on getting this to work? There’s got to be a way…

EDIT

Figured I’d post the code that finally worked in case anyone else tries to do the same thing.

`string tabLoadedScript = string.Empty;
string postBackScript = string.Empty;

string script = " <script language='javascript' type='text/javascript'>" + System.Environment.NewLine;
script += "function clientActiveTabChanged(sender, args) {" + System.Environment.NewLine;


int i = 0;

foreach(TabPanel tp in tc1.Tabs)
    {
    Button b = new Button();
    b.ID = "btn" + tp.ClientID;
    b.CssClass = "hiddenBtn";
    b.CommandName = tp.ID;
    b.Command += btnTrigger_Click;
    this.form1.Controls.Add(b);
    AsyncPostBackTrigger trg = new AsyncPostBackTrigger();
    trg.ControlID = "btn" + tp.ClientID;

    tabLoadedScript += "var isTab" + tp.ClientID + "loaded=$get('" + tp.Controls[0].ClientID + "');" + System.Environment.NewLine;
    postBackScript += "if(!isTab" + tp.ClientID + "loaded && sender.get_activeTabIndex() == " + i + ") {" + System.Environment.NewLine;
    postBackScript += "__doPostBack('" + b.ClientID + "','');}" + System.Environment.NewLine;
    i++;
    }
script += tabLoadedScript;
script += postBackScript;
script += "}" + System.Environment.NewLine;
script += "</script>";

this.ClientScript.RegisterClientScriptBlock(this.GetType(), “cs”, script, false);

protected void btnTrigger_Click(object sender, CommandEventArgs e)
{
System.Threading.Thread.Sleep(2500);
Panel ctrl = (Panel) FindControlRecursive(this, “pnl” + e.CommandName);
ctrl.Visible = true;
}

public static Control FindControlRecursive(Control Root, string Id)
    {

    if(Root.ID == Id)
        return Root;

    foreach(Control Ctl in Root.Controls)
        {

        Control FoundCtl = FindControlRecursive(Ctl, Id);
        if(FoundCtl != null)
        return FoundCtl;

        }
    return null;

    }

`

  • 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-20T22:57:12+00:00Added an answer on May 20, 2026 at 10:57 pm

    You need to use the Command event instead of the Click event. Also, assuming you’re using a recent version of Visual Studio and .Net, you can simply change the event registration from

    b.Click += new EventHandler(btnTrigger_Click);
    

    to

    b.Command += btnTrigger_Click
    

    The explicit typing of the delegate is redundant and will be inferred by the compiler. You can now change the signature of your event handler to:

    protected void btnTrigger_Click(object sender, CommandEventArgs e)
    

    And the code should work as desired.

    Unfortunately, the default code snippets in Visual Studio still generate this old-style event listener code.

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

Sidebar

Related Questions

I have a page with a UserControl (in which linkbuttons are dynamically created). On
I have a page containing a control called PhoneInfo.ascx. PhoneInfo is dynamically created using
I have a page where the user can dynamically add file upload boxes. Adding
I have a page which contains a dynamically generated grid, where a user can
I have a page where the background colour can be set dynamically. In some
I have a page that is currently generating dynamically created textboxes in a table
I have several dynamically created links and forms on one page named with IDs
I currently have a page being dynamically created like below: <h2>a Heading</h2> <p>a paragraph</p>
I have a jsp page in which rows are created dynamically to a table
i have dynamically created table with string builder.. when i run this page background

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.