Home All Groups Group Topic Archive Search About

Label text from template column is blank in ItemCommand event

Author
28 Mar 2005 10:47 PM
Kim Quigley
I have a datagrid with a template column and in that column I have some
label controls and an image button. The image button causes the
Products_ItemCommand event to fire; however, the value of ItemID is "".
Can anyone explain why this is so and how to get around it? Thanks!

HTML:
<asp:Label id="ItemIDLabel" runat="server" text='<%#
Container.DataItem("ItemID")%>'></asp:Label>

CODE BEHIND:
    Private Sub Products_ItemCommand(ByVal sender As Object, ByVal e As
System.Web.UI.WebControls.DataGridCommandEventArgs) Handles
Products.ItemCommand
        If (e.CommandName = "AddToCart") Then
            'Add to cookie
            Dim ItemID As Label
            ItemID = CType(e.Item.Cells(0).FindControl("ItemIDLabel"),
Label)
            Response.Write(ItemID)
        End If
    End Sub

Author
28 Mar 2005 10:58 PM
Kim Quigley
I figured out the solution within 5 minutes of posting this question :)


I encapsulated the databinding into a method called BindData and then
called BindData from inside the ItemCommand method:

CODE BEHIND:
    Private Sub Products_ItemCommand(ByVal sender As Object, ByVal e As

System.Web.UI.WebControls.Data­GridCommandEventArgs) Handles
Products.ItemCommand
        If (e.CommandName = "AddToCart") Then

            BindData()
            'Add to cookie
            Dim ItemID As Label
            ItemID =
CType(e.Item.Cells(0).FindCont­rol("ItemIDLabel"),
Label)
            Response.Write(ItemID)
        End If
    End Sub
Author
29 Mar 2005 3:04 AM
Elton Wang
Hi Kim,

Unless the datagrid's viewstate is disabled, you can
directly get data from the datagrid without re-binding
datagrid's data source.

HTH

Elton Wang
elton_w***@hotmail.com


>-----Original Message-----
>I figured out the solution within 5 minutes of posting
this question :)
>
>
>I encapsulated the databinding into a method called
BindData and then
>called BindData from inside the ItemCommand method:
>
>CODE BEHIND:
>    Private Sub Products_ItemCommand(ByVal sender As
Object, ByVal e As
Show quoteHide quote
>
>System.Web.UI.WebControls.Data­GridCommandEventArgs)
Handles
>Products.ItemCommand
>        If (e.CommandName = "AddToCart") Then
>
>            BindData()
>            'Add to cookie
>            Dim ItemID As Label
>            ItemID =
>CType(e.Item.Cells(0).FindCont­rol("ItemIDLabel"),
>Label)
>            Response.Write(ItemID)
>        End If
>    End Sub
>
>.
>