Label text from template column is blank in ItemCommand event

K

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
 
K

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
 
E

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
(e-mail address removed)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,777
Messages
2,569,604
Members
45,218
Latest member
JolieDenha

Latest Threads

Top