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

ItemCommand's e.Item.ItemIndex always 0

7 Answers 542 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Paul J
Top achievements
Rank 1
Paul J asked on 26 May 2010, 05:53 PM
I have a radGrid. I have a GridButtonColumn for a delete button. when the delete button is pressed, it brings up a rad confirm box via the following method:

http://www.telerik.com/community/forums/aspnet-ajax/grid/need-dynamic-confirmtext-on-programmatically-added-column.aspx

the radconfirm box works fine. it shows the right text and everything. the confirmFn gets the correct itemIndex value as well. but once the process hits the codebehind's ItemCommand event for the DeleteMember CommandName ItemIndex is always zero.

So when OK is clicked (in the radWindow), it continues on to the radGrid's ItemCommand event. Once it goes into this event it claims the e.Item.ItemIndex is 0 (first row in grid), even when I've clicked on a row that is definitely not 0 index.

this has been driving me crazy. I'd like to get it resolved ASAP. any help is appreciated.

my code:

javascript code for the confirm box:

<script type="text/javascript"
        function confirmFn(text, itemIndex) { 
           var callBackFn = function(arg) { 
                 if (arg) { 
                     var masterTable = $find("<%= rgMembers.ClientID %>").get_masterTableView(); 
                     masterTable.fireCommand("DeleteMember", itemIndex); 
                 } 
             } 
             radconfirm(text, callBackFn); 
         } 
          
 
    </script> 

ajaxmanager on page:

<telerik:RadAjaxManager ID="ramManageMembers" runat="server"
        <AjaxSettings> 
         <telerik:AjaxSetting AjaxControlID="ramManageMembers">   
                <UpdatedControls>   
                    <telerik:AjaxUpdatedControl ControlID="rgMembers" />   
                </UpdatedControls>   
            </telerik:AjaxSetting> 
                        <telerik:AjaxSetting AjaxControlID="rgMembers">   
                <UpdatedControls>   
                    <telerik:AjaxUpdatedControl ControlID="rgMembers" />   
                </UpdatedControls>   
            </telerik:AjaxSetting>             
        </AjaxSettings> 
    </telerik:RadAjaxManager> 


radGrid

<telerik:RadGrid ID="rgMembers" runat="server" Width="100%" ShowStatusBar="False" Skin="mySkin" EnableEmbeddedSkins="false" 
           AutoGenerateColumns="False" PageSize="10" AllowSorting="True" AllowMultiRowSelection="False" 
           AllowPaging="True" OnItemDataBound="rgMembers_ItemDataBound"
          
          <PagerStyle NextPageText="" PrevPageText="" AlwaysVisible="True" Mode="NextPrevAndNumeric"  Position="TopAndBottom" Font-Bold="False" Font-Italic="False"  
           HorizontalAlign="Right" PageButtonCount="8" PagerTextFormat="Page: {4} &nbsp;&nbsp; Displaying page {0} of {1}, items {2} to {3} of {5}."  
           Wrap="True" FirstPageImageUrl="" FirstPageText="" LastPageText="" VerticalAlign="Middle"   />     
         
          <MasterTableView Width="100%" DataKeyNames="ApplicantID"  AllowMultiColumnSorting="False">         
                
               <Columns> 
                   <telerik:GridBoundColumn UniqueName="gcApplicantID" SortExpression="ApplicantID" HeaderText="ApplicantID" HeaderButtonType="TextButton" 
                       DataField="ApplicantID" Visible="False" ItemStyle-HorizontalAlign="Right"
                   </telerik:GridBoundColumn> 
                   <telerik:GridBoundColumn UniqueName="gcPassNumber" SortExpression="PassNumber" HeaderText="Pass #" HeaderButtonType="TextButton" 
                       DataField="PassNumber" Visible="True" DataformatString="{0:D5}" ItemStyle-HorizontalAlign="Right" ItemStyle-Width="40px"
                   </telerik:GridBoundColumn> 
                    <telerik:GridBoundColumn UniqueName="gcTitle" SortExpression="Title" HeaderText="Title" HeaderButtonType="TextButton" 
                       DataField="Title" Visible="false"
                   </telerik:GridBoundColumn>                                      
                   <telerik:GridBoundColumn UniqueName="gcLastName" SortExpression="LastName" HeaderText="Last Name" HeaderButtonType="TextButton" 
                       DataField="LastName"
                   </telerik:GridBoundColumn> 
                   <telerik:GridBoundColumn UniqueName="gcFirstName" SortExpression="FirstName" HeaderText="First Name" HeaderButtonType="TextButton" 
                       DataField="FirstName"  ItemStyle-Width="120px"
                   </telerik:GridBoundColumn> 
                   <telerik:GridBoundColumn UniqueName="gcMiddleName" SortExpression="MiddleName" HeaderText="Middle Name" HeaderButtonType="TextButton" 
                       DataField="MiddleName" Visible="false"
                   </telerik:GridBoundColumn>   
                           
                <telerik:GridTemplateColumn UniqueName="gtcEdit"
             <HeaderTemplate> 
                            Edit 
                        </HeaderTemplate> 
                        <ItemTemplate> 
                            <div class="functionButtons"
                                <asp:Button runat="server" Text="" CssClass="btnEdit" ID="btnEdit"  CommandName="EditClicked"  /> 
                            </div> 
                        </ItemTemplate> 
            </telerik:GridTemplateColumn>      
             
            <telerik:GridButtonColumn UniqueName="gbcDelete" Text="" HeaderText="Delete" ItemStyle-HorizontalAlign="Center" HeaderStyle-HorizontalAlign="Center"  
                   ButtonType="PushButton" ButtonCssClass="btnDelete" Visible="false"
                     
                    </telerik:GridButtonColumn>                                                          
               </Columns> 
           </MasterTableView> 
       </telerik:RadGrid> 


radwindowmanager

    <telerik:RadWindowManager ID="rwmConfirmDelete" runat="server" KeepInScreenBounds="true" Title="Delete Member?" InitialBehaviors="Close,Move" Behaviors="Close,Move"  
             Skin="Simple" Width="450px"
            </telerik:RadWindowManager> 


radGrid ItemCommand event

 Protected Sub rgMembers_ItemCommand(ByVal source As ObjectByVal e As Telerik.Web.UI.GridCommandEventArgs) Handles rgMembers.ItemCommand 
        Dim Applicant As New DAL.Applicant 
 
        Dim curUserID = AppSession.CurrentUser.UserID 
 
        Select Case e.CommandName 
            Case "EditClicked" 
                ApplicantID = CType(rgMembers.MasterTableView.DataKeyValues.Item(e.Item.ItemIndex)("ApplicantID"), Integer
 
                AppSession.CurrentApplicant = New DAL.Applicant(ApplicantID) 
 
                pnlsearch.Visible = False 
 
                appDetails.PopulateForm() 
 
            Case "ExportToExcel" 
                ConfigureExport() 
                'exporting data  
 
            Case "DeleteMember" 
                'show confirm box. 
 
                ApplicantID = CType(rgMembers.MasterTableView.DataKeyValues.Item(e.Item.ItemIndex)("ApplicantID"), Integer
 
                AppSession.CurrentApplicant = New DAL.Applicant(ApplicantID) 
 
                With AppSession.CurrentApplicant 
                    .Deleted = True 
                    .LastUpdated = Now 
                    .LastUpdatedID = curUserID 
                    .Update() 
                End With 
 
                rgMembers.Rebind() 
 
                MasterPage.PageStatusText = AppSession.CurrentApplicant.LastName + ", " + AppSession.CurrentApplicant.FirstName + " (Pass #: " + String.Format("{0:D5}", AppSession.CurrentApplicant.PassNumber.ToString) + ") has been successfully deleted." 
                
 
                AppSession.CurrentApplicant = Nothing 
 
            Case Else 
                Exit Sub 
        End Select 
 
    End Sub 


radGrid ItemDataBound

        If TypeOf e.Item Is GridDataItem Then 
            Dim item As GridDataItem = TryCast(e.Item, GridDataItem) 
            Dim cell As TableCell = item("gbcDelete"
            Dim btnDelete As Button = DirectCast(cell.Controls(0), Button) 
            btnDelete.Attributes.Add("onclick""confirmFn('Are you sure you want to delete member " + item("gcLastName").Text + ", " + item("gcFirstName").Text + " ?" + "','" + CStr(e.Item.ItemIndex) + "'); return false;"
 
        End If 
 
    End Sub 

help! thanks!



7 Answers, 1 is accepted

Sort by
0
Paul J
Top achievements
Rank 1
answered on 26 May 2010, 06:23 PM
Well finally bothered to look a little closer to the e object and see there's a e.CommandArgument that does have the right index. I could have sworn though I've used the code before and the ItemIndex had the right value...and I didn't have to use the CommandArgument value.

hrmmm.
0
Accepted
Sebastian
Telerik team
answered on 27 May 2010, 11:16 AM
Hello Paul,

Verify that you pass the corresponding index of the clicked item (itemIndex) as a second argument to the fireCommand client method of the grid. Thus it will be available as second argument (e.CommandArgument) in the ItemCommand server handler of the grid.

e.Item.ItemIndex will default to 0 in this case because you raise custom command through MasterTableView's client object method, not a particular row in the grid.

Best regards,
Sebastian
the Telerik team

Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items.
0
Paul J
Top achievements
Rank 1
answered on 16 Jun 2010, 09:44 PM
Thanks for the info!
0
Mike
Top achievements
Rank 1
answered on 23 May 2011, 08:26 PM
Can you provide sample code for passing the extra arguments?
0
Paul J
Top achievements
Rank 1
answered on 23 May 2011, 08:41 PM

Mike, what fixed my issue was changing the following line under my "DeleteMember" case of my radGrid's ItemCommand event from:


ApplicantID = CType(rgMembers.MasterTableView.DataKeyValues.Item(e.Item.ItemIndex)("ApplicantID"), Integer)


to:

ApplicantID = CType(rgMembers.MasterTableView.DataKeyValues.Item(e.CommandArgument)("ApplicantID"), Integer)

hope this helps.
0
Mike
Top achievements
Rank 1
answered on 24 May 2011, 04:38 AM
Paul J...thanks much for the reply, but I still have a problem.  I can't figure out when to pass the CommandArgument from?

My setup is this...Radgrid with 3 columns, one of them being a "GridClientSelect" column so that users can select a row.  Once a row is selected, I enable various buttons on a radToolBar which is sitting in the CommandItem of the radGrid.  I currently have a "CommandName" set for each button on the toolbar so that when the event bubbles up to the ItemCommand I can determine which button was pushed in my code-behind, but I don't know where to set the commandArgument. 

Thanks again for your help, I'm sitting here pulling my hair out!

Mike
0
Paul J
Top achievements
Rank 1
answered on 24 May 2011, 03:00 PM
Mike, you better post your code. I am not totally understanding your setup. Also, if a telerik support person chimes in, I'm sure they'll request to see it as well.
Tags
Grid
Asked by
Paul J
Top achievements
Rank 1
Answers by
Paul J
Top achievements
Rank 1
Sebastian
Telerik team
Mike
Top achievements
Rank 1
Share this question
or