Reputation: 347
I'm dynamically adding an event handler on a link button, first when the page is load o create a link button with product category. On clicking the product category it fires an event and passes the product-id IN sender object.
Here is my problem: when i readd the event handler on page load it's giving an error cannot cast sender object
code:
sub page int
Me.Page.EnableViewState = True
end sub
page load
CatDisplay()
If ViewState("GetProductClicked") = True Then
GetProduct(Me, New EventArgs)
End If
Function CatDisplay() As Boolean
Dim arr As New ArrayList
Dim objTesting As New Bo_Dall_Layer.BoCategory
arr = objTesting.GetAllCategory()
Dim objExtra As Bo_Dall_Layer.BoCategory
Dim Cat_hyperLink As LinkButton
For Each objExtra In arr
Cat_hyperLink = New LinkButton
Cat_hyperLink.Text = objExtra.CategoryName
Cat_hyperLink.CommandArgument = objExtra.CategoryId
AddHandler Cat_hyperLink.Click, AddressOf GetProduct
CatPanel.Controls.Add(New LiteralControl("<br />"))
CatPanel.Controls.Add(Cat_hyperLink)
Next
ViewState.Item("CategoryLoaded") = False.ToString
End Function
Private Sub GetProduct(ByVal sender As Object, ByVal e As EventArgs)
Dim arrCollectList As New ArrayList
Dim getLinkDetail As LinkButton = CType(sender, LinkButton)
Dim getCatID As String = getLinkDetail.CommandArgument
Dim SendCatID As Integer
SendCatID = CInt(getCatID)
Dim objGetProduct As New Bo_Dall_Layer.BoProduct
arrCollectList = objGetProduct.getSelectProduct(SendCatID)
Dim objTemCollectPrd As Bo_Dall_Layer.BoProduct
Dim lblPrdName As Label
Dim lblPrdDes As Label
Dim lblPrdPrice As Label
Dim addCart As LinkButton
For Each objTemCollectPrd In arrCollectList
lblPrdName = New Label
lblPrdDes = New Label
lblPrdPrice = New Label
addCart = New LinkButton
getCatID = objTemCollectPrd.ProductId
PrdName.Controls.Add(New LiteralControl("<br />"))
lblPrdName.Text = objTemCollectPrd.ProductName
PrdName.Controls.Add(New LiteralControl("<br />"))
PrdDesc.Controls.Add(New LiteralControl("<br />"))
lblPrdDes.Text = objTemCollectPrd.ProductDescription
PrdDesc.Controls.Add(New LiteralControl("<br />"))
lblPrdPrice.Text = objTemCollectPrd.ProductPrice
PrdPrice.Controls.Add(New LiteralControl("<br />"))
lblPrdPrice.Text = objTemCollectPrd.ProductPrice
PrdPrice.Controls.Add(New LiteralControl("<br />"))
PrdCart.Controls.Add(New LiteralControl("<br />"))
addCart.Text = "Add to Cart"
addCart.CommandArgument = objTemCollectPrd.ProductId
PrdCart.Controls.Add(New LiteralControl("<br />"))
PrdName.Controls.Add(lblPrdName)
PrdDesc.Controls.Add(lblPrdDes)
PrdPrice.Controls.Add(lblPrdPrice)
PrdCart.Controls.Add(addCart)
Next
ViewState.Item("GetProductClicked") = True
End Sub
when the page is reload it giving me an error
Unable to cast object of type 'ASP.products_aspx' to type 'System.Web.UI.WebControls.LinkButton' Dim getLinkDetail As LinkButton = CType(sender, LinkButton)
Upvotes: 1
Views: 766
Reputation: 21881
in the line GetProduct(Me, New EventArgs)
you are passing in a reference to the page (Me) as the sender parameter. In the GetProduct
method with this line Dim getLinkDetail As LinkButton = CType(sender, LinkButton)
you are trying to cast the page as a link button. You are not assigning an event handler in the page load method at all, you are just calling the method.
Upvotes: 2