This is a migrated thread and some comments may be shown as answers.

[Solved] ItemCommand event not firing

4 Answers 337 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Kevin
Top achievements
Rank 2
Kevin asked on 02 May 2008, 03:49 PM
This is my first try at using the RadGrid so I'm probably way off.  Troubleshooting declarative code is so difficult.  I'm creating an Add to Cart page with a grid that displays products and has unbound fields that the user can enter quantity and request date and a button to add the item to the shopping cart.  There are a lot of dependencies (MasterPages, WebControls, complex objects that are being bound, etc.) that prohibit me from easily duplicating the problem outside of the project.  The grid is running inside of an ascx and renders properly when not posting back.

I've tried with both the GridButtonColumn and a GridTemplateColumn.  With the GridTemplate column I can get the Add2Cart_ItemCommand to fire but the grid does not show itself again when told to DataBind or Rebind.

    protected void Add2Cart_ItemCommand(object sender, EventArgs e)
{
// Add it to the cart
// ...
ProductListGrid.Rebind();
// or
ProductListGrid.DataBind();
// neither shows the grid again
}

protected void ProductListGrid_ItemCreated(object sender, GridItemEventArgs e)
{
if (e.Item is GridDataItem)
{
GridDataItem item = (GridDataItem) e.Item;
Button btn = (Button) item["Add2Cart"].FindControl("Add2CartButton");
btn.CommandArgument = e.Item.ItemIndex.ToString();
btn.Click += new EventHandler(Add2Cart_ItemCommand);
}
}

protected void ProductListGrid_ItemCommand(object source, GridCommandEventArgs e)
{
// This never fires
}
</script>

<telerik:RadGrid ID="ProductListGrid" runat="server" AllowSorting="True"
GridLines="None" Width="800px" OnItemCommand="ProductListGrid_ItemCommand" onitemcreated="ProductListGrid_ItemCreated"
EnableViewState="false" >
<MasterTableView AutoGenerateColumns="false">
<NoRecordsTemplate>
There are no available products in this category.
</NoRecordsTemplate>
<RowIndicatorColumn Visible="False">
<HeaderStyle Width="20px"></HeaderStyle>
</RowIndicatorColumn>

<ExpandCollapseColumn Visible="False" Resizable="False">
<HeaderStyle Width="20px"></HeaderStyle>
</ExpandCollapseColumn>
<Columns>
<telerik:GridBoundColumn HeaderText="ID" DataField='ID' Visible="false"></telerik:GridBoundColumn>
<telerik:GridTemplateColumn HeaderText="Qty">
<ItemTemplate>
<telerik:RadNumericTextBox ID="Qty" Runat="server" MinValue="0"
ShowSpinButtons="True" Width="65px">
<NumberFormat AllowRounding="True" DecimalDigits="0" GroupSeparator="" />
</telerik:RadNumericTextBox>
</ItemTemplate>
</telerik:GridTemplateColumn>
<telerik:GridTemplateColumn HeaderText=""><%-- Unit of measure --%>
<ItemTemplate>
<asp:Label ID="UnitOfMeasureLabel" runat="server" Text='<%# GetMetaField("UofM", (Item)Container.DataItem) %>' />
</ItemTemplate>
</telerik:GridTemplateColumn>

<telerik:GridTemplateColumn HeaderText="Part Number">
<ItemTemplate>
<asp:HyperLink ID="ProductHyperLink" runat="server" NavigateUrl='<%# ClientHelper.FormatUrl("product", GetMetaField("ProductId", (Item)Container.DataItem), DataBinder.Eval(Category, "SiteNodeId"), GetMetaField("Name", (Item)Container.DataItem)) %>'><h3><%# GetMetaField("Name", (Item)Container.DataItem)%></h3></asp:HyperLink>
</ItemTemplate>
</telerik:GridTemplateColumn>


<telerik:GridTemplateColumn HeaderText="Description">
<ItemTemplate>
<asp:Label ID="DescriptionLabel" runat="server" Text='<%# GetMetaField("Description", (Item)Container.DataItem) %>' />
</ItemTemplate>
</telerik:GridTemplateColumn>
<telerik:GridTemplateColumn HeaderText="Code" Visible="false">
<ItemTemplate>
<asp:Label ID="CodeLabel" runat="server" Text='<%# GetMetaField("Code", (Item)Container.DataItem) %>' />
</ItemTemplate>
</telerik:GridTemplateColumn>
<telerik:GridTemplateColumn HeaderText="Product Id" Visible="false">
<ItemTemplate>
<asp:Label ID="ProductIdLabel" runat="server" Text='<%# GetMetaField("ProductId", (Item)Container.DataItem) %>' />
</ItemTemplate>
</telerik:GridTemplateColumn>
<telerik:GridTemplateColumn HeaderText="Availability" Visible="false">
<ItemTemplate>
<asp:Label ID="AvailabilityLabel" runat="server" Text='<%# GetMetaField("Availability", (Item)Container.DataItem) %>' />
</ItemTemplate>
</telerik:GridTemplateColumn>
<telerik:GridTemplateColumn HeaderText="Request Date">
<ItemTemplate>
<telerik:RadDatePicker ID="RequestDatePicker" Runat="server" Width="80px">
<DateInput runat="server" InvalidStyleDuration="100" MinDate="<%#DateTime.Now %>">
</DateInput>
</telerik:RadDatePicker>
</ItemTemplate>
</telerik:GridTemplateColumn>
<telerik:GridTemplateColumn HeaderText="Retail Price">
<ItemTemplate>
<asp:Label ID="ListPriceLabel" runat="server" Text='<%# ((Item) Container.DataItem).Offers.Offer[0].OfferListing[0].Price.FormattedPrice %>' />
</ItemTemplate>
</telerik:GridTemplateColumn>
<telerik:GridTemplateColumn HeaderText="Your Price">
<ItemTemplate>
<asp:Label ID="YourPriceLabel" runat="server" />
</ItemTemplate>
</telerik:GridTemplateColumn>
<%-- This isn't causing it to fire either

telerik:GridButtonColumn ButtonType="PushButton"
UniqueName="Add2Cart"
CommandName="Add2Cart"
Text="Add to Cart">
</telerik:GridButtonColumn --%>
<telerik:GridTemplateColumn UniqueName="Add2Cart">
<ItemTemplate>
<asp:Button CommandName="Add2Cart" Text="Add to Cart" ID="Add2CartButton" runat="server" />
</ItemTemplate>
</telerik:GridTemplateColumn>
</Columns>

<EditFormSettings>
<PopUpSettings ScrollBars="None"></PopUpSettings>
</EditFormSettings>
</MasterTableView>
</telerik:RadGrid>

4 Answers, 1 is accepted

Sort by
0
Nikita Gourme
Top achievements
Rank 1
answered on 04 May 2008, 01:08 PM
I took a look at your code and noticed that you set EnableViewState = false for your grid. Can this be the cause for the ItemCommand event raising failure? Set this property to true to see how it goes. Also consider using NeedDataSource binding as this will handle automatically sorting/paging/grouping/other operations when enabled:

http://www.telerik.com/DEMOS/ASPNET/Prometheus/Grid/Examples/Programming/NeedDataSource/DefaultCS.aspx

Happy coding

Nikita
0
Kevin
Top achievements
Rank 2
answered on 04 May 2008, 03:59 PM
Thank you very much for your reply, on a Sunday no less!!!

Unfortunately, setting the EnableViewState property to true does not allow the ItemCommand event to fire. I've also implemented the NeedDataSource databinding since I'm going to use paging but that's not helping with the ItemCommand event or the postback databind.  Hmm.  I wonder it they are somehow related.
0
Accepted
Nikita Gourme
Top achievements
Rank 1
answered on 05 May 2008, 08:25 AM
Have you removed all DataBind() calls from your code as suggested in the online demo? If you need to refresh the state of the grid call its Rebind() method at a given phase of the lifecycle.
Furthermore, I think you do not need to attach the OnClick event of the button since if you set CommandName and CommandArgument for it you can hook the ItemCommand event of the grid, thus putting the event bubbling of the control into practice.

Nikita
0
Kevin
Top achievements
Rank 2
answered on 06 May 2008, 03:34 PM
Yep that did the trick for both the postback and the ItemCommand event.
Tags
Grid
Asked by
Kevin
Top achievements
Rank 2
Answers by
Nikita Gourme
Top achievements
Rank 1
Kevin
Top achievements
Rank 2
Share this question
or