HI,
No matter what I try, it is always null, the only place it seems to have a value that later disappears is in the ItemDataBound handler.
It goes like this:
I make sure that the DataGrid is bound to the source in every postback
If I bind in Page_Load (every postback) ItemCommand is only entered when using LinkButtons for Commands in the DataGrid, while using ImageButtons , I cannot step into ItemCommand
If I bind only on !IsPostBack and then Bind inside ItemCommand to make sure that the grid is bound I can step into ItemCommand in both previous cases
I check the value of e.Item.ItemCommand and I find it is null (we are inside ItemCommand)
Common sense tells me that this property is there for a reason and that it should return the DataSource Item for example DataRowView incase I am binding to a DataTable or DataView (both are eventually DataView)
I read in another post something similar and the answer was that the reason for it being null is that binding is happenning only first time, but here i am binding every time every where so there is no reason for it being nul.
Also, can anyone explain the issue of why I can enter ItemCommand when using LinkButton and not using ImageButton, when i used the first method of binding, It is as if for a certain reason DataBind is making the grid forget the postback event it came there
for...
Thank you...
How is a guy like me, trying to learn, supposed to find this kind of information "it is by design" is there any documentaion on the weird design decisions of Microsoft?
Also I asked about where I bound, I used DataBind in Page_Load and in another senarion I bind inside ItemCommand, and the 2 case produce different symptoms, If I bind in Page_Load at every post back the ItemCommand is not executed if I am using ImageButtons
while it is when I am using LinkButtons, why so?
As for why I am using e.Item.DataItem, I am doing so because for example I don't display the ID in my grid, so I need a way to get the ID without refering to a cell in the grid, how to do so, I tried e.Item.DataItem expecting it to have the data that it
is bound to.
How is a guy like me, trying to learn, supposed to find this kind of information "it is by design" is there any documentaion on the weird design decisions of Microsoft?
Also I asked about where I bound, I used DataBind in Page_Load and in another senarion I bind inside ItemCommand, and the 2 case produce different symptoms, If I bind in Page_Load at every post back the ItemCommand is not executed if I am using ImageButtons
while it is when I am using LinkButtons, why so?
As for why I am using e.Item.DataItem, I am doing so because for example I don't display the ID in my grid, so I need a way to get the ID without refering to a cell in the grid, how to do so, I tried e.Item.DataItem expecting it to have the data that it
is bound to.
Please answer all my questions, thank you...
Well, the best place to find documentations is MSDN, but not every thing is documented, you will also learn from your project/experience, now you know DataItem is not available in ItemCommand[:)]. Not every problem you have is Microsoft's weird design.
According to MSDN:
This property (DataItem) is available only on a request where the list item is data bound; on subsequent postbacks, it is always
null.
You should bind your grid is page_load's !IsPostBack block initially, in ItemCommand, you can also bind grid after you update/delete/insert data to reflesh your changes
Serveral way to access ID of your data during ItemCommand:
This property (DataItem) is available only on a request where the list item is data bound; on subsequent postbacks, it is always
null.
This only makes things more confusing, because I am always binding the grid at every post back, so accordding to MSDN e.Item.DataItem is available "where the list item is data bound"... Can anyone answer the rest of my question, on why I get different behaviour when using a LinkButton and an ImageButton???
U are not making any sense, what MS means is what I can understand from MSDN, and what I can understand is that they mean DataItem i available only when I bind, not just in ItemDataBound, and I am binding every time so it should work.
Read the whole question please guys...
Well, What I said is the fact. You need to talk to Microsoft on how they should phase their documentation. I just want to help you to solve your problem.
I don't think there is any problem with MSDN's documentation, here is how I can prove this to you:
(I assume that you put datagrid.databind() in !IsPostback block, and also after you do Edit/Upodate/Delete)
1. Load you webform first
2. Set breakpoint at ItemDataBound
3. Set breakpoint at ItemCommand
4. Click the button on your datagrid
5. Tell me which breakpoint was hit first!
My experience tell me ItemCommand will be hit first, does that tell you anything?
Richard Xin MCAD(charter member),MCDBA,MCSD
Web Site: www.richardxin.com (For ASP.Net tips and Code snippet)
Your assumption that I am binding in the !IsPostBack section is wrong, I am binding everytime meaning this:
Page_Load(..)
{
dg1.DataSource=myDataSource;
dg1.DataBind();
}
or
like this:
Hi, your assuption that I am using databind in the !IsPostBack block is wrong since I am not. I am binding every time, meaning without any if like this
Infinite_Rec...
Member
37 Points
13 Posts
e.Item.DataItem is null in the DataGrid's ItemCommand
May 05, 2005 11:27 PM|LINK
No matter what I try, it is always null, the only place it seems to have a value that later disappears is in the ItemDataBound handler.
It goes like this:
- I make sure that the DataGrid is bound to the source in every postback
- If I bind in Page_Load (every postback) ItemCommand is only entered when using LinkButtons for Commands in the DataGrid, while using ImageButtons , I cannot step into ItemCommand
- If I bind only on !IsPostBack and then Bind inside ItemCommand to make sure that the grid is bound I can step into ItemCommand in both previous cases
- I check the value of e.Item.ItemCommand and I find it is null (we are inside ItemCommand)
- Common sense tells me that this property is there for a reason and that it should return the DataSource Item for example DataRowView incase I am binding to a DataTable or DataView (both are eventually DataView)
I read in another post something similar and the answer was that the reason for it being null is that binding is happenning only first time, but here i am binding every time every where so there is no reason for it being nul.Also, can anyone explain the issue of why I can enter ItemCommand when using LinkButton and not using ImageButton, when i used the first method of binding, It is as if for a certain reason DataBind is making the grid forget the postback event it came there for...
Thank you...
rx
Contributor
2575 Points
515 Posts
Re: e.Item.DataItem is null in the DataGrid's ItemCommand
May 05, 2005 11:43 PM|LINK
Yes, e.Item.DataItem is not accessible in ItemCommand, its by design.
What's the reason you want to access DataItem, we may be able to find work-around
MCAD(charter member),MCDBA,MCSD
Web Site: www.richardxin.com (For ASP.Net tips and Code snippet)
Infinite_Rec...
Member
37 Points
13 Posts
Re: e.Item.DataItem is null in the DataGrid's ItemCommand
May 06, 2005 07:41 AM|LINK
How is a guy like me, trying to learn, supposed to find this kind of information "it is by design" is there any documentaion on the weird design decisions of Microsoft?
Also I asked about where I bound, I used DataBind in Page_Load and in another senarion I bind inside ItemCommand, and the 2 case produce different symptoms, If I bind in Page_Load at every post back the ItemCommand is not executed if I am using ImageButtons while it is when I am using LinkButtons, why so?
As for why I am using e.Item.DataItem, I am doing so because for example I don't display the ID in my grid, so I need a way to get the ID without refering to a cell in the grid, how to do so, I tried e.Item.DataItem expecting it to have the data that it is bound to.
Please answer all my questions, thank you...
[:)]
rx
Contributor
2575 Points
515 Posts
Re: e.Item.DataItem is null in the DataGrid's ItemCommand
May 06, 2005 04:34 PM|LINK
Well, the best place to find documentations is MSDN, but not every thing is documented, you will also learn from your project/experience, now you know DataItem is not available in ItemCommand[:)]. Not every problem you have is Microsoft's weird design.
According to MSDN:
This property (DataItem) is available only on a request where the list item is data bound; on subsequent postbacks, it is always null.
You should bind your grid is page_load's !IsPostBack block initially, in ItemCommand, you can also bind grid after you update/delete/insert data to reflesh your changes
Serveral way to access ID of your data during ItemCommand:
1. Use DataKeyField, check MSDN for details
2. Add boundColumn, visible=false, readonly=true
id = e.Item.Cell(indexOfThatBoundColumn).Text
MCAD(charter member),MCDBA,MCSD
Web Site: www.richardxin.com (For ASP.Net tips and Code snippet)
Infinite_Rec...
Member
37 Points
13 Posts
Re: e.Item.DataItem is null in the DataGrid's ItemCommand
May 06, 2005 05:49 PM|LINK
This only makes things more confusing, because I am always binding the grid at every post back, so accordding to MSDN e.Item.DataItem is available "where the list item is data bound"...
Can anyone answer the rest of my question, on why I get different behaviour when using a LinkButton and an ImageButton???
rx
Contributor
2575 Points
515 Posts
Re: e.Item.DataItem is null in the DataGrid's ItemCommand
May 06, 2005 08:50 PM|LINK
That means the safe place to get DataItem is in event ItemDataBound.
MCAD(charter member),MCDBA,MCSD
Web Site: www.richardxin.com (For ASP.Net tips and Code snippet)
Infinite_Rec...
Member
37 Points
13 Posts
Re: e.Item.DataItem is null in the DataGrid's ItemCommand
May 07, 2005 12:55 PM|LINK
Read the whole question please guys...
rx
Contributor
2575 Points
515 Posts
Re: e.Item.DataItem is null in the DataGrid's ItemCommand
May 07, 2005 04:47 PM|LINK
Well, What I said is the fact. You need to talk to Microsoft on how they should phase their documentation. I just want to help you to solve your problem.
I don't think there is any problem with MSDN's documentation, here is how I can prove this to you:
(I assume that you put datagrid.databind() in !IsPostback block, and also after you do Edit/Upodate/Delete)
1. Load you webform first
2. Set breakpoint at ItemDataBound
3. Set breakpoint at ItemCommand
4. Click the button on your datagrid
5. Tell me which breakpoint was hit first!
My experience tell me ItemCommand will be hit first, does that tell you anything?
MCAD(charter member),MCDBA,MCSD
Web Site: www.richardxin.com (For ASP.Net tips and Code snippet)
Infinite_Rec...
Member
37 Points
13 Posts
Re: e.Item.DataItem is null in the DataGrid's ItemCommand
May 08, 2005 11:53 AM|LINK
Page_Load(..)
{
dg1.DataSource=myDataSource;
dg1.DataBind();
}
or
like this:
ItemCommand(..)
{
dg1.DataSource=myDataSource;
dg1.DataBind();
...
}
So, I expect my DataItem to be available all the time, since I am binding everytime...
Comprende??
thanks...
Infinite_Rec...
Member
37 Points
13 Posts
Re: e.Item.DataItem is null in the DataGrid's ItemCommand
May 08, 2005 11:54 AM|LINK
Page_Load(...)
{
dg.DataSource=myDataSource;
dg.DataBind();
}
or like this
ItemCommand(...)
{
dg.DataSource=myDataSource;
dg.DataBind();
...
}
so obviously ItemDataBound will execute every time, and that is why I expect DataItem to be available, thank you...