Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Dynamically creating controls and adding CommandEventHandler

Status
Not open for further replies.

iaresean

Programmer
Mar 24, 2003
570
ZA
Hi All;

I have created a few dynamic controls within the Page_Load event, like so:

Code:
LinkButton lnkSet = new LinkButton();
lnkSet.ID = "lnkSet" + i.ToString();
lnkSet.Text = "Lot " + iStart + " - " + (iStart + iIncrement).ToString();
lnkSet.Command += new CommandEventHandler(this.LinkButton1_Command);
lnkSet.CommandName = "Prev";
lnkSet.CommandArgument = i.ToString();

phLotLinks.Controls.Add(lnkSet);

The command event handler code I am trying to add to these links is below:
Code:
protected void LinkButton1_Command(object sender, CommandEventArgs e)
{
    if (e.CommandName == "Prev")
        LoadPrev();
    else if (e.CommandName == "Next")
        LoadNext();

    SetupLinks(int.Parse(e.CommandArgument.ToString()), int.Parse(hdnCount.Value));
}

It compiles and runs fine, but when I click on one of my dynamically added links the LinkButton1_Command isn't being called.

Is there something I am doing wrong?

Thanks for any and all help!


Sean. [peace]
 
I believe in C# you have to use System.EventHandler rather than CommandEventHandler e.g.
Code:
lnkSet.Click += new System.EventHandler(LinkButton1_Command);
I haven't tried this though so you will need to test if this does work.


____________________________________________________________

Need help finding an answer?

Try the Search Facility or read FAQ222-2244 on how to get better results.

 
Hi ca8msm;

But I need to use the CommandEventHandler so that I can access the CommandEventArgs. :-(

Sean. [peace]
 
Ah sorry, I glossed over what you were actually trying to do!

Anyway, I don't think there is anything wrong with the actual code you've written, as stripping it down to this works perfectly fine
Code:
public partial class Default2 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        LinkButton lnkSet = new LinkButton();
        lnkSet.ID = "lnkSet";
        lnkSet.Text = "Lot ";
        lnkSet.Command += new CommandEventHandler(this.LinkButton1_Command);
        lnkSet.CommandName = "Prev";
        lnkSet.CommandArgument = "1";

        phLotLinks.Controls.Add(lnkSet);
    }

    protected void LinkButton1_Command(object sender, CommandEventArgs e)
    {
        Response.Write("I fired");
    }
}
So, maybe it is something to do with the loop you have to create the controls? Try seeing if the example I posted works for you, and if it does you might have to post your full page load event here.




____________________________________________________________

Need help finding an answer?

Try the Search Facility or read FAQ222-2244 on how to get better results.

 
Hi ca8msm;

Thank you for taking the time to test! Appreciated!

I am swamped with work at the moment, so haven't been able to check my code out or do any testing of late. Will get back to you guys with results ASAP.

Regards

Sean. [peace]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top