Adding EventHandler to LinkButton that I create programatically

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • CodeRazor

    #1

    Adding EventHandler to LinkButton that I create programatically

    Hi,
    I am trying to dynamically create linkbuttons. They need an event handler,
    so i can respond to the user's click.
    I try to add the eventhandler on the fly, but when i click on the link, the
    code does not execute, it just reloads the page.

    Where am i going so wrong? i don't understand what's missing.

    many thanks in advance.

    //Creating the link buttons
    for(int i=0; i<4; i++)
    {
    LinkButton lbtn = new LinkButton();
    lbtn.ID = i;
    lbtn.Text = "Delete";
    btn.Click += new EventHandler(lb tn_Click);
    }

    //Event Handler
    private void lbtn_Click(obje ct sender, System.EventArg s e)
    {
    LinkButton lbtnSender = (LinkButton)sen der;
    Response.Write( lbtnSender.ID.T oString());
    }
  • Dmytro Lapshyn [MVP]

    #2
    Re: Adding EventHandler to LinkButton that I create programatically

    Hi CodeRazor,

    Make sure you create exactly the same set of controls with the same IDs and
    property values upon PostBack. The whole hierarchy of controls must be
    exactly the same as it was when the page was rendered for the first time.

    It also matters *WHEN* you create the dynamic controls. At least they must
    be created before the PreRender phase.
    Also, make sure to add the created controls to the Page's Controls
    collection (not sure about this step, did not touch ASP .NET for more than a
    year).

    --
    Sincerely,
    Dmytro Lapshyn [Visual Developer - Visual C# MVP]


    "CodeRazor" <CodeRazor@disc ussions.microso ft.com> wrote in message
    news:DA57B56F-6655-4218-B1DF-34A0280F65CA@mi crosoft.com...[color=blue]
    > Hi,
    > I am trying to dynamically create linkbuttons. They need an event handler,
    > so i can respond to the user's click.
    > I try to add the eventhandler on the fly, but when i click on the link,
    > the
    > code does not execute, it just reloads the page.
    >
    > Where am i going so wrong? i don't understand what's missing.
    >
    > many thanks in advance.
    >
    > //Creating the link buttons
    > for(int i=0; i<4; i++)
    > {
    > LinkButton lbtn = new LinkButton();
    > lbtn.ID = i;
    > lbtn.Text = "Delete";
    > btn.Click += new EventHandler(lb tn_Click);
    > }
    >
    > //Event Handler
    > private void lbtn_Click(obje ct sender, System.EventArg s e)
    > {
    > LinkButton lbtnSender = (LinkButton)sen der;
    > Response.Write( lbtnSender.ID.T oString());
    > }[/color]

    Comment

    • CodeRazor

      #3
      Re: Adding EventHandler to LinkButton that I create programaticall

      "Make sure you create exactly the same set of controls with the same IDs and
      property values upon PostBack"

      that's the solution.
      thank you dmytro.

      CR.

      Comment

      • JJ

        #4
        Re: Adding EventHandler to LinkButton that I create programaticall

        Hi,

        If I had another set of controls how can I go about doing it.

        My page is a view photo page. Lets say there is 3 pages of 20 photo each and
        a large placeholder to display a selected photo from the sets of photo. When
        I move from page 1 to page 2, i had a new sets of photo display hence from
        the post you were mentioning that this will not work. True enf the click
        event does not fire off after the page change from 1->2.

        Page 2 is reload after a selection, i.e. page 2 controls are exactly the
        same as the previous page 2 components hence the event now is able to fire
        off.

        So how can I overcome this? Force a page reload when page renders from 1->2
        or 2->1? How can i do this?Or Whats the code to write for asp.net to treat it
        as a new posting?

        Thanks!
        JJ

        "Dmytro Lapshyn [MVP]" wrote:
        [color=blue]
        > Hi CodeRazor,
        >
        > Make sure you create exactly the same set of controls with the same IDs and
        > property values upon PostBack. The whole hierarchy of controls must be
        > exactly the same as it was when the page was rendered for the first time.
        >
        > It also matters *WHEN* you create the dynamic controls. At least they must
        > be created before the PreRender phase.
        > Also, make sure to add the created controls to the Page's Controls
        > collection (not sure about this step, did not touch ASP .NET for more than a
        > year).
        >
        > --
        > Sincerely,
        > Dmytro Lapshyn [Visual Developer - Visual C# MVP]
        >
        >
        > "CodeRazor" <CodeRazor@disc ussions.microso ft.com> wrote in message
        > news:DA57B56F-6655-4218-B1DF-34A0280F65CA@mi crosoft.com...[color=green]
        > > Hi,
        > > I am trying to dynamically create linkbuttons. They need an event handler,
        > > so i can respond to the user's click.
        > > I try to add the eventhandler on the fly, but when i click on the link,
        > > the
        > > code does not execute, it just reloads the page.
        > >
        > > Where am i going so wrong? i don't understand what's missing.
        > >
        > > many thanks in advance.
        > >
        > > //Creating the link buttons
        > > for(int i=0; i<4; i++)
        > > {
        > > LinkButton lbtn = new LinkButton();
        > > lbtn.ID = i;
        > > lbtn.Text = "Delete";
        > > btn.Click += new EventHandler(lb tn_Click);
        > > }
        > >
        > > //Event Handler
        > > private void lbtn_Click(obje ct sender, System.EventArg s e)
        > > {
        > > LinkButton lbtnSender = (LinkButton)sen der;
        > > Response.Write( lbtnSender.ID.T oString());
        > > }[/color]
        >
        >[/color]

        Comment

        Working...