Adding control & Event Handler at run-time

Discussion in 'ASP .Net Web Controls' started by matt del vecchio, Jan 23, 2004.

  1. hello,

    For some reason I am having difficulty doing what i thought would be
    simple.

    i want to loop thru a short list of categories, and for each one
    programmatically add new LinkButton control. i think want to be able
    to detect which new LinkButton was clicked. so like so:


    LinkButton myLKB;

    while (myDR.Read()) {
    myLKB = new LinkButton();
    myLKB.Text = myDR.GetString(1); //category description

    myLKB.CommandName = "CategoryID";
    myLKB.CommandArgument = myDR.GetString(0); //category id code
    myLKB.Command += new CommandEventHandler(this.myLKB_Command);
    }

    ....

    private void myLKB_Command(object sender, System.EventArgs e){
    Response.Write("in handler!!<br>");
    Response.Write(CommandName + ": " + e.CommandArgument);
    }


    ....yet the handler never seems to get called (no response.write is
    ever done). i have also tried this w/ a .Click event, since im not
    really sure if i should use a .Click or .Command.

    any suggestions?


    thanks!
    matt
     
    matt del vecchio, Jan 23, 2004
    #1
    1. Advertising

  2. Hi Matt,

    At which point is the code that creates the linkbuttons being executed? This
    sounds like you're creating the child controls too late. Please take a look
    at the docs about "Control Execution Lifecycle" to learn more about it.
    Here[1] you will find some links to the docs plus some more extra info I've
    written about the topic.

    [1] http://weblogs.asp.net/vga/archive/2003/08/11/23498.aspx

    --
    Victor Garcia Aprea
    Microsoft MVP | ASP.NET
    Looking for insights on ASP.NET? Read my blog:
    http://obies.com/vga/blog.aspx
    To contact me remove 'NOSPAM'. Please post all questions to the newsgroup

    "matt del vecchio" <> wrote in message
    news:...
    > hello,
    >
    > For some reason I am having difficulty doing what i thought would be
    > simple.
    >
    > i want to loop thru a short list of categories, and for each one
    > programmatically add new LinkButton control. i think want to be able
    > to detect which new LinkButton was clicked. so like so:
    >
    >
    > LinkButton myLKB;
    >
    > while (myDR.Read()) {
    > myLKB = new LinkButton();
    > myLKB.Text = myDR.GetString(1); //category description
    >
    > myLKB.CommandName = "CategoryID";
    > myLKB.CommandArgument = myDR.GetString(0); //category id code
    > myLKB.Command += new CommandEventHandler(this.myLKB_Command);
    > }
    >
    > ...
    >
    > private void myLKB_Command(object sender, System.EventArgs e){
    > Response.Write("in handler!!<br>");
    > Response.Write(CommandName + ": " + e.CommandArgument);
    > }
    >
    >
    > ...yet the handler never seems to get called (no response.write is
    > ever done). i have also tried this w/ a .Click event, since im not
    > really sure if i should use a .Click or .Command.
    >
    > any suggestions?
    >
    >
    > thanks!
    > matt
     
    Victor Garcia Aprea [MVP], Jan 23, 2004
    #2
    1. Advertising

  3. matt del vecchio

    CMA Guest

    here u have to follow these steps...
    set the ID for each link button while creating it.
    myLKB.ID = myDR.GetString(0); // or u can set an integer value THIS SHOULD
    BE UNIQUE

    and then in the event method (myLKB_Command)
    u can access the ID like this...
    LinkButton lbtnLink = (LinkButton)sender;
    now, "lbtnLink.ID" is the ID of the button u clicked...

    hope this helps.
    regards,
    CMA





    "matt del vecchio" <> wrote in message
    news:...
    > hello,
    >
    > For some reason I am having difficulty doing what i thought would be
    > simple.
    >
    > i want to loop thru a short list of categories, and for each one
    > programmatically add new LinkButton control. i think want to be able
    > to detect which new LinkButton was clicked. so like so:
    >
    >
    > LinkButton myLKB;
    >
    > while (myDR.Read()) {
    > myLKB = new LinkButton();
    > myLKB.Text = myDR.GetString(1); //category description
    >
    > myLKB.CommandName = "CategoryID";
    > myLKB.CommandArgument = myDR.GetString(0); //category id code
    > myLKB.Command += new CommandEventHandler(this.myLKB_Command);
    > }
    >
    > ...
    >
    > private void myLKB_Command(object sender, System.EventArgs e){
    > Response.Write("in handler!!<br>");
    > Response.Write(CommandName + ": " + e.CommandArgument);
    > }
    >
    >
    > ...yet the handler never seems to get called (no response.write is
    > ever done). i have also tried this w/ a .Click event, since im not
    > really sure if i should use a .Click or .Command.
    >
    > any suggestions?
    >
    >
    > thanks!
    > matt
     
    CMA, Jan 23, 2004
    #3
  4. hi Victor,

    i have seen that lifecyle chart many times, but it hasnt made anything
    clearer. my app goes like this:

    - the first time its called, theres a static dropdownlist and a static
    button, both design-time. the user thus makes a department selection and
    clicks "GO".

    - the "GO" button event handler turns the dropdownlist & button
    invisible. it then calls another routine, GenerateCategoryButtons(),
    passing in the department that was selected from the dropdownlist.

    - this button-generating routine loops thru a datareader and thus
    dynamically spits out the appropiate buttons, adding them to a
    previously-empty placeholder (set on the page ala design-time).

    ...and that pretty much seems to work: a user loads the app, makes a
    selection, hits GO and sees the appropiate buttons.

    i just cant seem to handle those buttons' clicks.

    does that help any? i beleive i forgot to show the line where the
    controls get added to the placeholder, but it's there.


    thanks,
    matt

    *** Sent via Developersdex http://www.developersdex.com ***
    Don't just participate in USENET...get rewarded for it!
     
    Matt Del Vecchio, Jan 23, 2004
    #4
  5. ...well, i actually am setting the .ID, i just cut it out because it
    didnt see to make a diff--if you dont give a control a name asp.net
    finds one to use.

    also, i dont think casing a type to the sender will do anything because
    the entire event handler routine isnt firing--the Response.Write is
    never hit. stepping thru the code it never reaches the handler.


    matt


    *** Sent via Developersdex http://www.developersdex.com ***
    Don't just participate in USENET...get rewarded for it!
     
    Matt Del Vecchio, Jan 23, 2004
    #5
  6. btw, i am indeed adding these to the form (forgot to include). looks like this:

    LinkButton myLKB;
    while (myDR.Read()) {
    int itemID = myDR.GetInt16(0);

    myLKB = new LinkButton();
    myLKB.ID = "lkb" + itemID.ToString();
    myLKB.Text = myDR.GetString(1);
    myLKB.CssClass = "lk02";


    //TRY: set up an eventhandler for the linkbutton's click..?
    //myLKB.Click += new System.EventHandler(this.myLKB_Click);

    //TRY: set up an eventhandler for the linkbutton's command-
    //name/argument pair?
    myLKB.CommandName = "NewAreaID";
    myLKB.CommandArgument = itemID.ToString();
    myLKB.Command += new CommandEventHandler(this.myLKB_Command);

    ph1.Controls.Add(myLKB);
    ph1.Controls.Add(new LiteralControl(" ยท "));
    }
     
    matt del vecchio, Jan 23, 2004
    #6
  7. ok, typos aside, i found my problem.

    it was in fact w/ the control lifecycle, as Victor suspected.

    i was building my dynamic linkbuttons on a static button's click
    event. while this did generate them and allow me to add them to the
    form, it does not allow me to create new event handlers. to do this
    properly, i must create the controls & their events/handlers from
    (atleast) Page_Load().

    some c++ devs told me that makes sense. but coming from ASP and not a
    true OO background, the "why" is still voodoo to me.

    anyone care to take a stab at explaining? and not in
    one-experienced-OO-developer-to-another fashion?


    thanks!
    matt
     
    matt del vecchio, Jan 23, 2004
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Jose Suero
    Replies:
    2
    Views:
    9,401
    Jose Suero
    Dec 26, 2003
  2. Pavils Jurjans

    Assign session onEnd event handler at run-time

    Pavils Jurjans, Apr 21, 2004, in forum: ASP .Net
    Replies:
    3
    Views:
    2,545
    Pavils Jurjans
    Apr 21, 2004
  3. =?Utf-8?B?bWc=?=

    Generating event handler at run time.

    =?Utf-8?B?bWc=?=, Nov 13, 2004, in forum: ASP .Net
    Replies:
    1
    Views:
    336
    =?Utf-8?B?bWc=?=
    Nov 13, 2004
  4. Replies:
    1
    Views:
    706
    Damien
    Feb 22, 2007
  5. Slavcho
    Replies:
    1
    Views:
    130
    Slavcho
    Apr 9, 2008
Loading...

Share This Page