Saturday, March 24, 2012

Setting the CommandArgument property in the ItemDataBound event

I am trying to set the CommandArgument property of a Button control from a
template in a DataList of mine using code in the ItemDataBound event.
However, it does not want to set the property (it is acting as if I did not
set it). Here is my code that sets the CommandArgument property in the
ItemDataBound event:
CType(e.Item.FindControl("btnMainMoveDown"), Button).CommandArgument =
CStr(CType(Me.datMain.DataSource, DataTable).Rows(e.Item.ItemIndex +
1)("id"))
I have tried using a System.Diagnostics.Debug.Write() statement to make sure
the value I am assigning to the CommandArgument is returning the expected
value, and it is. However, when I try to use the CommandArgument property
through EventBubbling in the ItemCommand event, the e.CommandArgument
property has no value. If I set the CommandArgument attribute in the *.aspx
file as an attribute, it has a value, but I need to set it in the
ItemDataBound event. Am I doing something wrong when setting the property?
Why is the property not getting set? Thanks.
--
Nathan Sokalski
njsokalski@dotnet.itags.org.hotmail.com
http://www.nathansokalski.com/On May 27, 6:00 am, "Nathan Sokalski" <njsokal...@.hotmail.com> wrote:
> I am trying to set the CommandArgument property of a Button control from a
> template in a DataList of mine using code in the ItemDataBound event.
> However, it does not want to set the property (it is acting as if I did no
t
> set it). Here is my code that sets the CommandArgument property in the
> ItemDataBound event:
> CType(e.Item.FindControl("btnMainMoveDown"), Button).CommandArgument =
> CStr(CType(Me.datMain.DataSource, DataTable).Rows(e.Item.ItemIndex +
> 1)("id"))
> I have tried using a System.Diagnostics.Debug.Write() statement to make su
re
> the value I am assigning to the CommandArgument is returning the expected
> value, and it is. However, when I try to use the CommandArgument property
> through EventBubbling in the ItemCommand event, the e.CommandArgument
> property has no value. If I set the CommandArgument attribute in the *.asp
x
> file as an attribute, it has a value, but I need to set it in the
> ItemDataBound event. Am I doing something wrong when setting the property?
> Why is the property not getting set? Thanks.
> --
> Nathan Sokalski
> njsokal...@.hotmail.comhttp://www.nathansokalski.com/
Hi Nathan
try to see if the control was properly found and a value assigned
CType(e.Item.FindControl("btnMainMoveDown"), Button).CommandArgument =
CStr(CType(Me.datMain.DataSource, DataTable).Rows(e.Item.ItemIndex + 1)
("id"))
Response.Write(CType(e.Item.FindControl("btnMainMoveDown"),
Button).CommandArgument)
Another thought: are you sure about e.Item.ItemIndex + 1? I think, the
last row will not have a value.
Anyway, try to Response.Write at first

0 comments:

Post a Comment