Click to See Complete Forum and Search --> : button in webcontrol added to datagrid event not firing


flynny1st
November 24th, 2008, 09:56 AM
hi all,

hopefully you can help.

i have created a web control for my add to cart button. this is working fine in my static pages. however, i have a search page which hold a data grid. when the users searches it i want to add an instance of this add to cart button to the datagrid. however when i click the added button its not triggering the click event in the control. can someone explain why.

I've tried the following way

<asp:DataGrid
OnEditCommand="ItemsGrid_Edit"
CssClass="SearchBody"
AlternatingItemStyle-BackColor="GhostWhite"
id="search_datagrid"
AutoGenerateColumns=False
AllowPaging="True"
PageSize="20"
OnItemCreated="DataGrid_ItemCreated"
OnPageIndexChanged="PageIndexChanged_Click"
PagerStyle-Mode="NumericPages"
runat="server">

with my page_load event being

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

'If Not Page.IsPostBack Then

oDataSet = CreateDataSource()
Session("searchDataSet") = oDataSet

If oDataSet.Tables(0).Rows.Count = 0 Then
gridEmptyLabel.Text = "The search returned back no results"
gridEmptyLabel.Visible = True
Else
search_datagrid.DataSource = oDataSet
search_datagrid.DataBind()

'add the addcart buttons here
addCartButtonsToDataGrid(search_datagrid)
End If

'End If

End Sub

and

Sub addCartButtonsToDataGrid(ByVal grid As DataGrid)

For i = 0 To grid.Items.Count - 1

Dim item As DataGridItem = grid.Items(i)
Dim bPlaceHolder As PlaceHolder = item.FindControl("addToCartPlaceholder")

Dim productID As Label = item.FindControl("productID")
Dim productName As HyperLink = item.FindControl("productName")
Dim sizeID As Label = item.FindControl("sizeID")
Dim size As Label = item.FindControl("size")
Dim flavours As DropDownList = item.FindControl("FlavourDropDown")
Dim price As Label = item.FindControl("productPrice")
Dim quantity As TextBox = item.FindControl("quantity")

If item.ItemType = ListItemType.Item Or item.ItemType = ListItemType.AlternatingItem Then
'finally set the add to cart button
addButton = DirectCast(LoadControl("~/cart/add_to_cart_control/add_to_cart.ascx"), add_to_cart)
addButton.productID = productID.Text
addButton.product = productName.Text
addButton.sizeID = sizeID.Text
addButton.size = size.Text
addButton.flavourID = flavours.SelectedIndex
addButton.flavour = flavours.SelectedValue.ToString
addButton.price = price.Text
addButton.Quantity = 1 'quantity.Text
addButton.Attributes.Add("oncommand", "AddProductToCart")

bPlaceHolder.Controls.Add(addButton)
End If

Next

End Sub

i have also tried adding the button in the onitemcreated event

'now create the add button and add

'If e.Item.ItemType = ListItemType.Item Or e.Item.ItemType = ListItemType.AlternatingItem Then
''finally set the add to cart button
'addButton = DirectCast(LoadControl("~/cart/add_to_cart_control/add_to_cart.ascx"), add_to_cart)
'addButton.productID = productID.Text
'addButton.product = productName.Text
'addButton.sizeID = sizeID.Text
'addButton.size = size.Text
'addButton.flavourID = flavours.SelectedIndex
'addButton.flavour = flavours.SelectedValue.ToString
'addButton.price = price.Text
'addButton.Quantity = quantity.Text

'placeHolder.Controls.Add(addButton)
'End If


now both seem to add the button but its losing the click event the ascx is simply

----------------------------ascx-------------------------------

<%@ Control Language="VB" AutoEventWireup="true" CodeFile="add_to_cart.ascx.vb" Inherits="add_to_cart" %>
<%@ Reference Control="~/main/left_side_frame/basket/side_cart.ascx" %>
<asp:button id="add_to_cart_button" text="Add to Cart" OnCommand="AddProductToCart" runat="server"/>

--------------------------ascx.vb-----------------------------

Sub AddProductToCart(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.CommandEventArgs) Handles add_to_cart_button.Click

...

End Sub

any ideas why?

many thanks in advance,

Matt.