Home All Groups Group Topic Archive Search About

Problem handling event in a button inside a Datalist

Author
11 Jul 2006 4:25 AM
ed
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

Author
12 Jul 2006 10:13 AM
Alessandro Zifiglio
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

Show quoteHide quote
"ed" <e*@discussions.microsoft.com> ha scritto nel messaggio
news:D57BA7D8-C78B-485B-AB03-CBB3FA67B671@microsoft.com...
> 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
>
Author
12 Jul 2006 10:06 PM
ed
Thank you very much :)

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

Show quoteHide quote
"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" <e*@discussions.microsoft.com> ha scritto nel messaggio
> news:D57BA7D8-C78B-485B-AB03-CBB3FA67B671@microsoft.com...
> > 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
> >
>
>
>
Author
13 Jul 2006 6:23 AM
Alessandro Zifiglio
you are welcome, ed :-)

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


Show quoteHide quote
"ed" <e*@discussions.microsoft.com> ha scritto nel messaggio
news:0812310B-B188-48A0-899C-A444150D578A@microsoft.com...
> 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" <e*@discussions.microsoft.com> ha scritto nel messaggio
>> news:D57BA7D8-C78B-485B-AB03-CBB3FA67B671@microsoft.com...
>> > 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
>> >
>>
>>
>>