I have a need to create a pdf file for a selected row in a grid which I then download to the client pc. I have the pdf creation working fine in code behind. I've got it triggered from a button on the command row. The button click downloads a pdf file based on the selected row in the grid. The grid is ajaxified using a RadAjaxPanel and, in order to perform the pdf file download, I disable the ajax call on click of the button, and then do a Response.BinaryWrite(pdfBytes) of the pdf data, which basically is then doing a regular postback. I get the pdf file transfered correctly to the client pc with a save as/open diaglog box.
Now I want to do the same thing but from a button in each row of the grid. I have a grid with a standard linq datasource. I have a gridbuttoncolumn with an imagebutton which has a command of Print. I've hooked the client OnCommand to some javascript to cancel the ajax call. I've hooked the server side OnItemCommand to a c# handler and everything fires ok.
My problem is that the e.item.itemIndex value is always zero regardless of what row button I click. All my row data seems to be in place because inside the OnItemCommand if I supply a hard coded index to a GetDataKeyValues, I get the data key correctly. Here are the two samples. The first only ever gives me the data key of item zero because e.Item.ItemIndex is always 0.
This one proves the data is there because I get the key value for the 7th row (indexed from 0).
Why is my ItemIndex always Zero?
Thanks,
Steve (pulling my hair out...)
btw. Here's some code extracted from my app to give you more context.
aspx.
javascript:
c#
Now I want to do the same thing but from a button in each row of the grid. I have a grid with a standard linq datasource. I have a gridbuttoncolumn with an imagebutton which has a command of Print. I've hooked the client OnCommand to some javascript to cancel the ajax call. I've hooked the server side OnItemCommand to a c# handler and everything fires ok.
My problem is that the e.item.itemIndex value is always zero regardless of what row button I click. All my row data seems to be in place because inside the OnItemCommand if I supply a hard coded index to a GetDataKeyValues, I get the data key correctly. Here are the two samples. The first only ever gives me the data key of item zero because e.Item.ItemIndex is always 0.
| protected void RadGridLetters_ItemCommand(object source, Telerik.Web.UI.GridCommandEventArgs e) |
| { |
| if (e.CommandName == "Print") |
| { |
| int letterId = (int)e.Item.OwnerTableView.DataKeyValues[e.Item.ItemIndex]["LetterId"]; |
| CreatePDF(letterId); |
| } |
| } |
This one proves the data is there because I get the key value for the 7th row (indexed from 0).
| protected void RadGridLetters_ItemCommand(object source, Telerik.Web.UI.GridCommandEventArgs e) |
| { |
| if (e.CommandName == "Print") |
| { |
| int letterId = (int)e.Item.OwnerTableView.DataKeyValues[6]["LetterId"]; |
| CreatePDF(letterId); |
| } |
| } |
Why is my ItemIndex always Zero?
Thanks,
Steve (pulling my hair out...)
btw. Here's some code extracted from my app to give you more context.
aspx.
| <Telerik:RadAjaxPanel ID="RadAjaxPanelLetters" runat="server" LoadingPanelID="RadAjaxLoadingPanel2" ClientEvents-OnRequestStart="onRequestStartLetters"> |
| <telerik:RadGrid ID="RadGridLetters" runat="server" GridLines="None" EnableEmbeddedSkins="false" Skin="RealMax" |
| AllowAutomaticInserts="True" AllowAutomaticUpdates="True" AllowAutomaticDeletes="true" |
| DataSourceID="LinqDS_Letters" OnItemCommand="RadGridLetters_ItemCommand" |
| AllowPaging="True" AllowSorting="True" EnableHeaderContextMenu="True" |
| AllowFilteringByColumn="True" > |
| <ClientSettings EnableRowHoverStyle="true" > |
| <ClientEvents OnRowContextMenu="RowContextMenu" OnRowMouseOut="rowMouseOut" OnRowMouseOver="rowMouseOver" OnPopUpShowing="PopUpCentered" OnCommand="GridCommand" ></ClientEvents> |
| <Selecting AllowRowSelect="true" /> |
| </ClientSettings> |
| <HeaderContextMenu Skin="Office2007" EnableTheming="True"> |
| <CollapseAnimation Type="OutQuint" Duration="200"></CollapseAnimation> |
| </HeaderContextMenu> |
| <PagerStyle Mode="NextPrevNumericAndAdvanced" AlwaysVisible="false" /> |
| <MasterTableView AutoGenerateColumns="False" CommandItemDisplay="Top" |
| DataKeyNames="LetterId" DataSourceID="LinqDS_Letters" EditMode="EditForms"> |
| <RowIndicatorColumn> |
| <HeaderStyle Width="20px"></HeaderStyle> |
| </RowIndicatorColumn> |
| <ExpandCollapseColumn> |
| <HeaderStyle Width="20px"></HeaderStyle> |
| </ExpandCollapseColumn> |
| <ItemStyle CssClass="RowMouseOut" /> |
| <AlternatingItemStyle CssClass="RowMouseOut" /> |
| <Columns> |
| <telerik:GridEditCommandColumn EditImageUrl="~/Images/Buttons/edit.gif" |
| ButtonType="ImageButton" CancelImageUrl="~/Images/Buttons/cancel.png" |
| Resizable="False"> |
| <HeaderStyle Width="16px" /> |
| <ItemStyle CssClass="CommandColumn" /> |
| </telerik:GridEditCommandColumn> |
| <Telerik:GridButtonColumn ButtonType="ImageButton" ImageUrl="~/Images/Buttons/printer.png" Text="Print" CommandName="Print" > |
| <HeaderStyle Width="16px" /> |
| <ItemStyle CssClass="CommandColumn" /> |
| </Telerik:GridButtonColumn> |
| ... etc |
javascript:
| var cancelAjaxLetters = false; |
| function GridCommand(sender, args) { |
| if (args.get_commandName() == "Print") cancelAjaxLetters = true; |
| } |
| function onRequestStartLetters(ajaxManager, eventArgs) { |
| if (cancelAjaxLetters) { |
| //alert("disabling Ajax"); |
| eventArgs.EnableAjax = false; |
| } |
| cancelAjaxLetters = false; |
| } |
c#
| protected void RadGridLetters_ItemCommand(object source, Telerik.Web.UI.GridCommandEventArgs e) |
| { |
| if (e.CommandName == "Print") |
| { |
| int letterId = (int)e.Item.OwnerTableView.DataKeyValues[e.Item.ItemIndex]["LetterId"]; |
| CreatePDF(letterId); |
| } |
| } |