Solved: Telerik RadGrid doesn’t retain data after a postback

You may come across this issue when you try to sort, filter or group your table that is built with Telerik RadGrid component. Each of these sorting, filtering, and grouping features trigger a postback. You may not see the data in your table after the postback is completed.

Solution

Make sure you define OnNeedDataSource attribute for your RadGrid object and handle NeedDataSource event. Telerik uses NeedDataSource event each time RadGrid is need to be bound with a data source.

ASPX file:

<telerik:RadGrid RenderMode="Lightweight" ID="RadGrid1" runat="server" CellSpacing="0"
GridLines="None" OnNeedDataSource="RadGrid1_NeedDataSource1" AllowPaging="true" PageSize="10">
     <MasterTableView AutoGenerateColumns="true" DataKeyNames="CustomerID">
     </MasterTableView>
</telerik:RadGrid>

Code-behind file:

protected void RadGrid1_NeedDataSource1(object sender, Telerik.Web.UI.GridNeedDataSourceEventArgs e)
{
      RadGrid1.DataSource = GetDataTable("SELECT CustomerID, CompanyName, ContactName FROM Customers");
}

References:

How to set focus into Telerik RadListBox control?

If user clicks a RadListBox control, control won’t be automatically in focus even though you can make selections in the control (I know, it’s unexpected). It may get tricky to set focus into control. Try the lines below.

function focusOnList() {
    var dropdownSource = $find("countryList"); 
    // var dropdownSource = $find("<%= countryList.ClientID %>");
    dropdownSource._getGroupElement().focus();
    console.log(dropdownSource);
}

Don’t forget to call this function when selected index is changed in your list:

<telerik:RadListBox ... OnClientSelectedIndexChanged="focusOnList">
...
</telerik:RadListBox>

References

How to simulate keyboard key press in JavaScript using jQuery?

You may need your user to press a certain key as part of your application flow. Use the line below if you want to automatize this action. In this example, “77” is the character code for the letter “m”. Change it to the key you want to simulate pressing (JavaScript Key Codes).

jQuery.event.trigger({ type: 'keydown', which: 77 });

If you want to simulate the situation where user presses 2 keys at the same time in an input field, use the code block below. It assumes that user presses Alt + M

var e = jQuery.Event("keydown");
e.which = 77; // m code value
e.altKey = true; // Alt key pressed
$("#inputBox").trigger(e);

Note: Depending on the browser, you may need to use “keypress” event instead of “keydown” event.

References: