Home All Groups Group Topic Archive Search About

Why does OnClick event not work in 2.0 for DataGrid

Author
23 Jan 2007 8:16 PM
Harry Keck
I have a DataGrid in a web application that I upgraded from 1.1 to 2.0.  This
DataGrid has a TemplateColumn with a Button control in it.  The Button has
the OnClick event declared for it in the aspx.  When I click the Button, the
page posts back, but the OnClick event is not raised.  When I declare a
Button outside of the DataGrid on the same page with the same OnClick event
handler, that one works, but not the one in the DataGrid.  This all works
fine in the 1.1 version of my code.  Does anyone have an explanation or
workaround for this in 2.0?  Thank.

Author
24 Jan 2007 6:04 AM
bhavesh
hello harry,

u r nor required to define onclick event of a button inside a grid.

just assign a command name property of a button to any string name.

and then try to check e.commandname in grid's item command.

by this way u can get the onclick of a button inside a grid.

hope this will help

On Jan 24, 1:16 am, Harry Keck <HarryK***@discussions.microsoft.com>
wrote:
Show quoteHide quote
> I have a DataGrid in a web application that I upgraded from 1.1 to 2.0.  This
> DataGrid has a TemplateColumn with a Button control in it.  The Button has
> the OnClick event declared for it in the aspx.  When I click the Button, the
> page posts back, but the OnClick event is not raised.  When I declare a
> Button outside of the DataGrid on the same page with the same OnClick event
> handler, that one works, but not the one in the DataGrid.  This all works
> fine in the 1.1 version of my code.  Does anyone have an explanation or
> workaround for this in 2.0?  Thank.
Author
24 Jan 2007 3:51 PM
Harry Keck
Thank you for the suggestion, but that did not help.  I even added
ButtonColumns to the DataGrid, but nothing would fire the OnClick or
OnItemCommand events on the server side.  Then, I started commenting out code
and found the one line of code that is apparently causing the problem.

Part of my code catches the DataGrid's OnItemCreated event to manipulate the
controls rendered in the DataGrid.  Here is a part of that code.

Button objViewButton = (Button)e.Item.FindControl("ViewButton");
string stringViewButtonID = objViewButton.ClientID;

If I replace the line

string stringViewButtonID = objViewButton.ClientID;

with

string stringViewButtonID = "ViewButton";

everything all of a sudden works.  This makes no sense to me.  Simply
referencing the ClientID property of a control is preventing all OnClick
commands from processing.  This works fine in my 1.1 version of code, but in
2.0, it does not.  This has to be a bug of some kind in the framework.  Can
anyone provide me with any information as to what is causing this?

Show quoteHide quote
"bhavesh" wrote:

> hello harry,
>
> u r nor required to define onclick event of a button inside a grid.
>
> just assign a command name property of a button to any string name.
>
> and then try to check e.commandname in grid's item command.
>
> by this way u can get the onclick of a button inside a grid.
>
> hope this will help
>
> On Jan 24, 1:16 am, Harry Keck <HarryK***@discussions.microsoft.com>
> wrote:
> > I have a DataGrid in a web application that I upgraded from 1.1 to 2.0.  This
> > DataGrid has a TemplateColumn with a Button control in it.  The Button has
> > the OnClick event declared for it in the aspx.  When I click the Button, the
> > page posts back, but the OnClick event is not raised.  When I declare a
> > Button outside of the DataGrid on the same page with the same OnClick event
> > handler, that one works, but not the one in the DataGrid.  This all works
> > fine in the 1.1 version of my code.  Does anyone have an explanation or
> > workaround for this in 2.0?  Thank.
>
>
Author
28 Jan 2007 12:02 PM
Teemu Keiski
Hi,

sounds like that when accessing ClientID it gets cached, which mixs things,
since in ItemCreated the Item itself isn't yet added to grid's Controls
collection, so the client id will be fixed to a wrong value, which impacts
also on the unique ID. And unique ID is used with postback data routing

ClientID should be accessed when the entire control hierarchy is "done".

--
Teemu Keiski
AspInsider, ASP.NET MVP
http://blogs.aspadvice.com/joteke
http://teemukeiski.net


Show quoteHide quote
"Harry Keck" <HarryK***@discussions.microsoft.com> wrote in message
news:4E447C38-EF2B-4BB6-B517-C838CB3415AD@microsoft.com...
> Thank you for the suggestion, but that did not help.  I even added
> ButtonColumns to the DataGrid, but nothing would fire the OnClick or
> OnItemCommand events on the server side.  Then, I started commenting out
> code
> and found the one line of code that is apparently causing the problem.
>
> Part of my code catches the DataGrid's OnItemCreated event to manipulate
> the
> controls rendered in the DataGrid.  Here is a part of that code.
>
> Button objViewButton = (Button)e.Item.FindControl("ViewButton");
> string stringViewButtonID = objViewButton.ClientID;
>
> If I replace the line
>
> string stringViewButtonID = objViewButton.ClientID;
>
> with
>
> string stringViewButtonID = "ViewButton";
>
> everything all of a sudden works.  This makes no sense to me.  Simply
> referencing the ClientID property of a control is preventing all OnClick
> commands from processing.  This works fine in my 1.1 version of code, but
> in
> 2.0, it does not.  This has to be a bug of some kind in the framework.
> Can
> anyone provide me with any information as to what is causing this?
>
> "bhavesh" wrote:
>
>> hello harry,
>>
>> u r nor required to define onclick event of a button inside a grid.
>>
>> just assign a command name property of a button to any string name.
>>
>> and then try to check e.commandname in grid's item command.
>>
>> by this way u can get the onclick of a button inside a grid.
>>
>> hope this will help
>>
>> On Jan 24, 1:16 am, Harry Keck <HarryK***@discussions.microsoft.com>
>> wrote:
>> > I have a DataGrid in a web application that I upgraded from 1.1 to 2.0.
>> > This
>> > DataGrid has a TemplateColumn with a Button control in it.  The Button
>> > has
>> > the OnClick event declared for it in the aspx.  When I click the
>> > Button, the
>> > page posts back, but the OnClick event is not raised.  When I declare a
>> > Button outside of the DataGrid on the same page with the same OnClick
>> > event
>> > handler, that one works, but not the one in the DataGrid.  This all
>> > works
>> > fine in the 1.1 version of my code.  Does anyone have an explanation or
>> > workaround for this in 2.0?  Thank.
>>
>>
Author
28 Jan 2007 4:32 PM
Teemu Keiski
I blogged about the issue:
http://aspadvice.com/blogs/joteke/archive/2007/01/28/Accessing-ClientID-or-UniqueID-too-early-can-cause-issues.aspx

--
Teemu Keiski
AspInsider, ASP.NET MVP
http://blogs.aspadvice.com/joteke
http://teemukeiski.net



Show quoteHide quote
"Teemu Keiski" <jot***@aspalliance.com> wrote in message
news:8EDA76A3-CEB3-4A44-A9C1-0BF585E619ED@microsoft.com...
> Hi,
>
> sounds like that when accessing ClientID it gets cached, which mixs
> things, since in ItemCreated the Item itself isn't yet added to grid's
> Controls collection, so the client id will be fixed to a wrong value,
> which impacts also on the unique ID. And unique ID is used with postback
> data routing
>
> ClientID should be accessed when the entire control hierarchy is "done".
>
> --
> Teemu Keiski
> AspInsider, ASP.NET MVP
> http://blogs.aspadvice.com/joteke
> http://teemukeiski.net
>
>
> "Harry Keck" <HarryK***@discussions.microsoft.com> wrote in message
> news:4E447C38-EF2B-4BB6-B517-C838CB3415AD@microsoft.com...
>> Thank you for the suggestion, but that did not help.  I even added
>> ButtonColumns to the DataGrid, but nothing would fire the OnClick or
>> OnItemCommand events on the server side.  Then, I started commenting out
>> code
>> and found the one line of code that is apparently causing the problem.
>>
>> Part of my code catches the DataGrid's OnItemCreated event to manipulate
>> the
>> controls rendered in the DataGrid.  Here is a part of that code.
>>
>> Button objViewButton = (Button)e.Item.FindControl("ViewButton");
>> string stringViewButtonID = objViewButton.ClientID;
>>
>> If I replace the line
>>
>> string stringViewButtonID = objViewButton.ClientID;
>>
>> with
>>
>> string stringViewButtonID = "ViewButton";
>>
>> everything all of a sudden works.  This makes no sense to me.  Simply
>> referencing the ClientID property of a control is preventing all OnClick
>> commands from processing.  This works fine in my 1.1 version of code, but
>> in
>> 2.0, it does not.  This has to be a bug of some kind in the framework.
>> Can
>> anyone provide me with any information as to what is causing this?
>>
>> "bhavesh" wrote:
>>
>>> hello harry,
>>>
>>> u r nor required to define onclick event of a button inside a grid.
>>>
>>> just assign a command name property of a button to any string name.
>>>
>>> and then try to check e.commandname in grid's item command.
>>>
>>> by this way u can get the onclick of a button inside a grid.
>>>
>>> hope this will help
>>>
>>> On Jan 24, 1:16 am, Harry Keck <HarryK***@discussions.microsoft.com>
>>> wrote:
>>> > I have a DataGrid in a web application that I upgraded from 1.1 to
>>> > 2.0. This
>>> > DataGrid has a TemplateColumn with a Button control in it.  The Button
>>> > has
>>> > the OnClick event declared for it in the aspx.  When I click the
>>> > Button, the
>>> > page posts back, but the OnClick event is not raised.  When I declare
>>> > a
>>> > Button outside of the DataGrid on the same page with the same OnClick
>>> > event
>>> > handler, that one works, but not the one in the DataGrid.  This all
>>> > works
>>> > fine in the 1.1 version of my code.  Does anyone have an explanation
>>> > or
>>> > workaround for this in 2.0?  Thank.
>>>
>>>
>