DatakeyValue not updated with new search results

5 posts, 1 answers
  1. Axel
    Axel avatar
    41 posts
    Member since:
    Jul 2009

    Posted 14 Sep 2009 Link to this post

    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
  2. Tsvetoslav
    Admin
    Tsvetoslav avatar
    1823 posts

    Posted 17 Sep 2009 Link to this post

    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.
  3. UI for ASP.NET Ajax is Ready for VS 2017
  4. Axel
    Axel avatar
    41 posts
    Member since:
    Jul 2009

    Posted 17 Sep 2009 Link to this post

    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

  5. Axel
    Axel avatar
    41 posts
    Member since:
    Jul 2009

    Posted 22 Sep 2009 Link to this post

    ... and 5 days later I am thinking that I can not affort these stoppages and should just use the asp:Gridview...
  6. Answer
    Tsvetoslav
    Admin
    Tsvetoslav avatar
    1823 posts

    Posted 23 Sep 2009 Link to this post

    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.
Back to Top
UI for ASP.NET Ajax is Ready for VS 2017