Tim
Tim

Reputation: 4101

Dynamically adding a commandbutton in ASP.NET

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;

    }

`

Upvotes: 2

Views: 2199

Answers (2)

Jonas H&#248;gh
Jonas H&#248;gh

Reputation: 10874

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.

Upvotes: 1

Chandu
Chandu

Reputation: 82913

You can still access the CommandArgument using the the first argument passed to the click handler.

Something like:

protected void btnTrigger_Click(object sender, EventArgs e)
{
  Button btnTrigger = sender as Button;
  String commandArgument = btnTrigger.CommandArgument;
  .
  .
  .
  .
  //Other code....

}

Upvotes: 2

Related Questions