Problem handling event in a button inside a Datalist

Discussion in 'ASP .Net Web Controls' started by ed, Jul 11, 2006.

  1. ed

    ed Guest

    Hello All.

    Scenario: I have a web form with a textbox, a button and a datalist (lets
    call it datalist1), when the user clicks in the button I add items to the
    datalist1 as much as the textbox says. Each item in datalist1 has a label, a
    link button and other datalist (datalist2), the label shows the item index,
    and above the datalist2 I have a link button intended to add items to the
    datalist2. I successfully can add items to the datalist1 when clicking the
    button, but when clicking the Add linkbutton nothing happens. I subscribed
    the onclick event by getting a reference to the button in the ItemDataBound
    event in datalist1.

    private void datalist1_ItemDataBound(object sender, DataListItemEventArgs e)
    {
    LinkButton lnkAdd = (LinkButton)e.Item.FindControl("lnkAdd ");
    lnkAdd.Click += new EventHandler(lnk_Click);
    }

    private void lnkAdd _Click(object sender, EventArgs e)
    {
    // This never executes…
    }

    Is this way right to subscribe the event to the "add" button? What can be
    the cause of this?

    Thank you very much
     
    ed, Jul 11, 2006
    #1
    1. Advertising

  2. hi Ed, you can respond to the event in ItemCommand event exposed by the
    datalist. All child controls that fire events bubbleup and fired here.

    http://msdn2.microsoft.com/en-us/library/y200hyx2.aspx

    Or you can keep your current code and instead of wiring the event in
    ItemDataBound you can wire it declaratively in your linkbutton itself
    <asp:linkButton .... onclick="lnkAdd" />

    Regards,
    Alessandro Zifiglio
    http://www.AsyncUI.net

    "ed" <> ha scritto nel messaggio
    news:D...
    > Hello All.
    >
    > Scenario: I have a web form with a textbox, a button and a datalist (lets
    > call it datalist1), when the user clicks in the button I add items to the
    > datalist1 as much as the textbox says. Each item in datalist1 has a
    > label, a
    > link button and other datalist (datalist2), the label shows the item
    > index,
    > and above the datalist2 I have a link button intended to add items to the
    > datalist2. I successfully can add items to the datalist1 when clicking the
    > button, but when clicking the Add linkbutton nothing happens. I subscribed
    > the onclick event by getting a reference to the button in the
    > ItemDataBound
    > event in datalist1.
    >
    > private void datalist1_ItemDataBound(object sender, DataListItemEventArgs
    > e)
    > {
    > LinkButton lnkAdd = (LinkButton)e.Item.FindControl("lnkAdd ");
    > lnkAdd.Click += new EventHandler(lnk_Click);
    > }
    >
    > private void lnkAdd _Click(object sender, EventArgs e)
    > {
    > // This never executes.
    > }
    >
    > Is this way right to subscribe the event to the "add" button? What can be
    > the cause of this?
    >
    > Thank you very much
    >
     
    Alessandro Zifiglio, Jul 12, 2006
    #2
    1. Advertising

  3. ed

    ed Guest

    Thank you very much :)

    It worked this way: <asp:linkButton .... onclick="lnkAdd" />

    "Alessandro Zifiglio" wrote:

    > hi Ed, you can respond to the event in ItemCommand event exposed by the
    > datalist. All child controls that fire events bubbleup and fired here.
    >
    > http://msdn2.microsoft.com/en-us/library/y200hyx2.aspx
    >
    > Or you can keep your current code and instead of wiring the event in
    > ItemDataBound you can wire it declaratively in your linkbutton itself
    > <asp:linkButton .... onclick="lnkAdd" />
    >
    > Regards,
    > Alessandro Zifiglio
    > http://www.AsyncUI.net
    >
    > "ed" <> ha scritto nel messaggio
    > news:D...
    > > Hello All.
    > >
    > > Scenario: I have a web form with a textbox, a button and a datalist (lets
    > > call it datalist1), when the user clicks in the button I add items to the
    > > datalist1 as much as the textbox says. Each item in datalist1 has a
    > > label, a
    > > link button and other datalist (datalist2), the label shows the item
    > > index,
    > > and above the datalist2 I have a link button intended to add items to the
    > > datalist2. I successfully can add items to the datalist1 when clicking the
    > > button, but when clicking the Add linkbutton nothing happens. I subscribed
    > > the onclick event by getting a reference to the button in the
    > > ItemDataBound
    > > event in datalist1.
    > >
    > > private void datalist1_ItemDataBound(object sender, DataListItemEventArgs
    > > e)
    > > {
    > > LinkButton lnkAdd = (LinkButton)e.Item.FindControl("lnkAdd ");
    > > lnkAdd.Click += new EventHandler(lnk_Click);
    > > }
    > >
    > > private void lnkAdd _Click(object sender, EventArgs e)
    > > {
    > > // This never executes.
    > > }
    > >
    > > Is this way right to subscribe the event to the "add" button? What can be
    > > the cause of this?
    > >
    > > Thank you very much
    > >

    >
    >
    >
     
    ed, Jul 12, 2006
    #3
  4. you are welcome, ed :)

    Regards,
    Alessandro Zifiglio
    http://www.AsyncUI.net


    "ed" <> ha scritto nel messaggio
    news:...
    > Thank you very much :)
    >
    > It worked this way: <asp:linkButton .... onclick="lnkAdd" />
    >
    > "Alessandro Zifiglio" wrote:
    >
    >> hi Ed, you can respond to the event in ItemCommand event exposed by the
    >> datalist. All child controls that fire events bubbleup and fired here.
    >>
    >> http://msdn2.microsoft.com/en-us/library/y200hyx2.aspx
    >>
    >> Or you can keep your current code and instead of wiring the event in
    >> ItemDataBound you can wire it declaratively in your linkbutton itself
    >> <asp:linkButton .... onclick="lnkAdd" />
    >>
    >> Regards,
    >> Alessandro Zifiglio
    >> http://www.AsyncUI.net
    >>
    >> "ed" <> ha scritto nel messaggio
    >> news:D...
    >> > Hello All.
    >> >
    >> > Scenario: I have a web form with a textbox, a button and a datalist
    >> > (lets
    >> > call it datalist1), when the user clicks in the button I add items to
    >> > the
    >> > datalist1 as much as the textbox says. Each item in datalist1 has a
    >> > label, a
    >> > link button and other datalist (datalist2), the label shows the item
    >> > index,
    >> > and above the datalist2 I have a link button intended to add items to
    >> > the
    >> > datalist2. I successfully can add items to the datalist1 when clicking
    >> > the
    >> > button, but when clicking the Add linkbutton nothing happens. I
    >> > subscribed
    >> > the onclick event by getting a reference to the button in the
    >> > ItemDataBound
    >> > event in datalist1.
    >> >
    >> > private void datalist1_ItemDataBound(object sender,
    >> > DataListItemEventArgs
    >> > e)
    >> > {
    >> > LinkButton lnkAdd = (LinkButton)e.Item.FindControl("lnkAdd ");
    >> > lnkAdd.Click += new EventHandler(lnk_Click);
    >> > }
    >> >
    >> > private void lnkAdd _Click(object sender, EventArgs e)
    >> > {
    >> > // This never executes.
    >> > }
    >> >
    >> > Is this way right to subscribe the event to the "add" button? What can
    >> > be
    >> > the cause of this?
    >> >
    >> > Thank you very much
    >> >

    >>
    >>
    >>
     
    Alessandro Zifiglio, Jul 13, 2006
    #4
    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. Nevyn Twyll
    Replies:
    8
    Views:
    1,916
  2. manu_srinivasa
    Replies:
    0
    Views:
    394
    manu_srinivasa
    Nov 24, 2004
  3. Steve
    Replies:
    0
    Views:
    3,069
    Steve
    Feb 1, 2005
  4. Les Caudle
    Replies:
    0
    Views:
    2,021
    Les Caudle
    May 2, 2006
  5. KJ
    Replies:
    3
    Views:
    407
Loading...

Share This Page