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

GetDataKeyValue returning first Value in Grid

2 Answers 493 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Landon
Top achievements
Rank 2
Landon asked on 17 May 2011, 05:21 PM
Hi,

I have a couple Grids on my page which include a "Download" GridButtonColumn that has AJAX disabled via Javascript to enable a File Download. When clicking these download buttons, the Code behind uses GetDataKeyValue to get the ID of the row:

protected void CompleteTickets_ItemCommand(object sender, GridCommandEventArgs e)
{
     if (e.CommandName == "Refresh")
     {
         CompleteTicketsGrid.Rebind();
     }
             
     if (e.CommandName == "Download")
     {
          //  Get Ticket ID
          GridDataItem item = (GridDataItem)e.Item;
          int ticketID = Convert.ToInt32(item.GetDataKeyValue("TicketID"));
           
             //   Use Ticket ID to Get File from File Repository
     }
}


However, this code works for all of my other functions (edit), but on the "Download" button it automatically pulls the TicketID from the  first Row of the Grid. It doesn't matter if I delete rows or change pages on the Grid, the GetDataKeyValue method always pulls the first item's ID rather than the row I've clicked.  

I have DataKeyNames="TicketID"  set in the MasterTableView of the Grid, and the Grid is populated with a LINQ/IQueryable Datasource.

I've also attempted to create a work-around for this problem by using this code instead, considering I have a column which displays the TicketID:

int ticketID = Convert.ToInt32(item["TicketID"].Text);

But to no avail, it is still grabbing the first Row's "TicketID". When constructing the GridDataItem item object, it's almost as if it's automatically grabbing the wrong Row from the very beginning. I've included an image of what exactly is happening.

Here is also the block of ASPX code of my Grid.

<telerik:RadGrid
    runat="server"
    ID="CompleteTicketsGrid"
    Enabled="true"
    Width="100%"
    AutoGenerateColumns="false"
    Skin="Default"
    GroupingEnabled="false"
    PageSize="10"
    AllowPaging="true"
    OnNeedDataSource="CompleteTickets_OnNeedDataSource"
    OnItemCommand="CompleteTickets_ItemCommand">
            <MasterTableView
            DataKeyNames="TicketID"
            CommandItemDisplay="Top"
            CommandItemSettings-ShowRefreshButton="true"
            CommandItemSettings-ShowAddNewRecordButton="false"
            CommandItemStyle-HorizontalAlign="NotSet"
            TableLayout="Fixed"
            Name="Unpriced"
            AllowPaging="true">
            <Columns>
                <telerik:GridBoundColumn UniqueName="TicketID" DataField="TicketID" HeaderStyle-Width="5%" ItemStyle-Width="5%" HeaderText="#"></telerik:GridBoundColumn>
                <telerik:GridBoundColumn UniqueName="Customer" DataField="Customer" HeaderStyle-Width="15%" ItemStyle-Width="15%" HeaderText="Customer"></telerik:GridBoundColumn>
                <telerik:GridBoundColumn UniqueName="Location" DataField="Location" HeaderStyle-Width="18%" ItemStyle-Width="18%" HeaderText="Location"></telerik:GridBoundColumn>
                <telerik:GridBoundColumn UniqueName="JobNo" DataField="JobNo" HeaderStyle-Width="9%" ItemStyle-Width="9%" HeaderText="Job #"></telerik:GridBoundColumn>
                <telerik:GridBoundColumn UniqueName="CreatedDate" DataField="DateCreated" HeaderStyle-Width="11%" ItemStyle-Width="11%" HeaderText="Date Created" ReadOnly="true" DataFormatString="{0:d/M/yyyy}"></telerik:GridBoundColumn>
                <telerik:GridBoundColumn UniqueName="OrderedBy" DataField="OrderedBy" HeaderStyle-Width="11%" ItemStyle-Width="11%" HeaderText="Ordered By" ReadOnly="true"></telerik:GridBoundColumn>
                <telerik:GridButtonColumn UniqueName="Download" ButtonType="LinkButton" Text="Download" CommandName="Download" ItemStyle-Width="12%" HeaderStyle-Width="12%"/>
                <telerik:GridButtonColumn UniqueName="LoadTicket" ButtonType="LinkButton" Text="Load Ticket" CommandName="LoadTicket" ItemStyle-Width="13%" HeaderStyle-Width="13%"/>
            </Columns>
            </MasterTableView>
 
            <ItemStyle Height="40" VerticalAlign="Middle"/>
            <AlternatingItemStyle Height="40"  BackColor="CadetBlue" ForeColor="White" VerticalAlign="Middle" />
 
            <ClientSettings>
                <ClientEvents OnCommand="OnGridCommand" />
                <Scrolling AllowScroll="False" UseStaticHeaders="True" />
            </ClientSettings>
        </telerik:RadGrid>


Oh, and here's the JavaScript I'm using to Disable AJAX on the Download Button:

//  Disables Ajax on Buttons Clicked in Grid
//
function OnGridCommand(sender, args) {
    var itemArgument = args.get_commandArgument();
    var itemCommand = args.get_commandName();
    if (itemArgument == "DisableAjax" || itemCommand == "Download") {
        window["disableAjax"] = true;
    }
    else {
        window["disableAjax"] = false;
    }
 
    args.set_cancel(false);
}


Any ideas are greatly appreciated, as I've hit a brick wall with this one... Or maybe I've just got tunnel vision from staring at this for so long.

Best Regards,

Landon

2 Answers, 1 is accepted

Sort by
0
Landon
Top achievements
Rank 2
answered on 17 May 2011, 05:51 PM
My apologies for double posting,

But I've narrowed the problem down to being the Javascript Function I'm using to disable AJAX on the button. It seems like when disabling AJAX on the button, it's not allowing the GridItemCommand to access the correct Row.

By removing <ClientEvents OnCommand="OnGridCommand"> from the <ClientSettings>, and turning off AJAX on the page my problem is slightly fixed. However, I need AJAX on the page.

Is there a way to disable AJAX on the button, and still be able to access the DataKeyValue from the Code-Behind? Or perhaps grab the DataKeyValue Client side?

********  
EDIT:  
********  
Ended up using e.CommandArguments on the RadGrid to get the Item Index. I then used that index to get the DataKeyValues.  
********

Best Regards,

Landon
0
Accepted
Veli
Telerik team
answered on 20 May 2011, 01:12 PM
Hi Landon,

The approach you have taken is correct. When you assign an event handler to RadGrid's client-side OnCommand event, the grid uses another mechanism to postback and fire command events. When firing a custom command (excl. commands that invoke some action in RadGrid, e.g. Edit, Sort, Page, etc) with OnCommand handler attached, the server-side command handler's e.Item object does not represent the item that fired the command. Instead, the item index of the target item goes to e.CommandArgument. Thus, you would need to parse the index from the e.CommandArgument object and retrieve the item with the specified index from the e.Item.OwnerTableView.Items collection.

Greetings,
Veli
the Telerik team

Browse the vast support resources we have to jump start your development with RadControls for ASP.NET AJAX. See how to integrate our AJAX controls seamlessly in SharePoint 2007/2010 visiting our common SharePoint portal.

Tags
Grid
Asked by
Landon
Top achievements
Rank 2
Answers by
Landon
Top achievements
Rank 2
Veli
Telerik team
Share this question
or