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

DatakeyValue not updated with new search results

4 Answers 70 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Axel
Top achievements
Rank 2
Axel asked on 15 Sep 2009, 12:16 AM
Hi,

I have a small search page that dumps it's results to a RadGrid. My search routine looks like this:
 var searchResults = db.AbsenceSearch(txtIndividualSearchBox.Text.ToUpper(),  
                ddlEnterprises.SelectedValue == "-1" || ddlEnterprises.SelectedValue == "" ? null : (int?)Convert.ToInt32(ddlEnterprises.SelectedValue), ddlDivisions.SelectedValue == "-1" || ddlDivisions.SelectedValue == "" ? null : (int?)Convert.ToInt32(ddlDivisions.SelectedValue),  
                dateFrom,  
                dateTo,  
                reevaluationPeriod,  
                cbIsOpen.Checked,  
                userid).ToList();  
 
            RadGrid1.DataSource = searchResults

My initial databind seems to have correct DataKeys values in the grid, but when I do another search with new search field values, my new data appears OK, but the old Datakeys are still there. I for example found out that on initial pageload I have a DataKey value of 2000 in the 3rd item row in the grid, and when I do an new search, and the grid loads with the new data, I still have DataKey value 2000 in the 3rd item row.

This is how I am retrieving the Datakeys:
protected void RadGrid1_ItemCommand(object source, Telerik.Web.UI.GridCommandEventArgs e)  
        {  
 
            if (e.Item.ItemType == Telerik.Web.UI.GridItemType.Item || e.Item.ItemType == Telerik.Web.UI.GridItemType.AlternatingItem)  
            {  
                int eID = (int)e.Item.OwnerTableView.DataKeyValues[e.Item.ItemIndex]["id"];  
                Response.Redirect(<to some page>);  
            }  
        } 

I am sure this is a simple beginner mistake. Please help.

reg.
Axel

4 Answers, 1 is accepted

Sort by
0
Tsvetoslav
Telerik team
answered on 17 Sep 2009, 12:13 PM
Hi Axel,

A better way to get the data key value for the given item is as follows:

int eID = ((GridDataItem)e.Item).GetDataKeyValue("ID"); 

However, you should keep in mind that the ItemCommand event is thrown after the grid has been bound to its new result set - this is the reason why you are getting the old values for the data keys. You need to extract the data key at a later stage of the page life cycle when the grid has already been bound - e.g. you can use the DataBound event of the grid.

I hope this helps.

All the best,
Tsvetoslav
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
0
Axel
Top achievements
Rank 2
answered on 17 Sep 2009, 03:41 PM
Thanks for the reply, but I do not understand it's logic.

You say "ItemCommand event is thrown after the grid has been bound". In my case the command gets thrown when I click on a rowItem in the grid. I filter out events not inside "item" and "AlternatingItem" and read the key from the row selected.
The grid is already bound to the new result set and the data is displayed waiting for the user to select a row.

How can you suggest I get the datakey from the DataBound event when my scenario is just selecting a row by clicking on it, picking up the datakey throug the event firing (itemCommand) and redirect to a new detail page.

Better yet, how would you do this everyday task? I applied conventional asp:Gridview logic or at least I think I did.

still stumpt
Axel

0
Axel
Top achievements
Rank 2
answered on 22 Sep 2009, 04:26 PM
... and 5 days later I am thinking that I can not affort these stoppages and should just use the asp:Gridview...
0
Accepted
Tsvetoslav
Telerik team
answered on 23 Sep 2009, 07:53 AM
Hello Axel,

Excuse me for the oversight - the ItemCommand event is thrown before the grid is bound to its new result set. Therefore, you can save the index of the item throwing the command in the ItemCommand event and use it in the DataBound event to get the item and extract its new content.

If on a row being clicked you do not rebind the grid - then the content in the clicked row will be the same as before the row-click and you can just extract it in the ItemCommand event. If on the other hand, you are repopulating the grid with new items on a row being clicked, then the DataBound event will be certainly thrown and you can use it to achieve this scenario.

Concerning the delay in responding to your ticket - please, note that the last two days were national holidays in Bulgaria and while we do our best not to leave behind late tickets during these periods it is more difficult to accomplish this.

Thanks for the understanding.

All the best,
Tsvetoslav
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
Tags
Grid
Asked by
Axel
Top achievements
Rank 2
Answers by
Tsvetoslav
Telerik team
Axel
Top achievements
Rank 2
Share this question
or