Montag, 15. November 2010

Focus row in grouped Ext.Net GridPanel with DirectMethod Success Handler after new data binding

This Blog moved to

When you use the Grid Panel with a GroupField, the final grouping in the grid is made
by the ExtJS Client. Problem: Custom pre-sorting of your datasource doesn't matter any more.

If you bind now new data and want to focus the new added
record in the grid afterwards, you can either predict the row number (a bit difficult) or use another good
approach: The DirectMetods Success Handler.

In this Example a button fires a DirectMethod with a string parameter.
The called DirectMethod appends data to a session variable and updates the binding of the grid.
Finally the Method returns the new id to the defined success handler. The handler now checks
the grid/store for the id to set the focus.

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="Tests._Default" %>

<%@ Import Namespace="System.Collections.Generic" %>
<%@ Register Assembly="Ext.Net" Namespace="Ext.Net" TagPrefix="ext" %>
<script runat="server">
    private List<object> MyData = new List<object>()
        new { id="id1", group = "group1", test="test1" },
        new { id="id2", group = "group1", test="test2" },
        new { id="id3", group = "group1", test="test3" },
        new { id="id4", group = "group2", test="test1" },
        new { id="id5", group = "group2", test="test2" },
        new { id="id6", group = "group2", test="test3" },

    protected void Page_Load(object sender, EventArgs e)
        if (!X.IsAjaxRequest)
            Store store = this.GridPanel1.GetStore();
            store.DataSource = MyData;
            this.Session["MyData"] = MyData;

    [DirectMethod(Namespace = "MyNamespace")]
    public string DataBindAndSelect(string message)
        var myData = this.Session["MyData"] as List<object>;

        myData.Add(new { id = "id0", group = "group0", test = "test0" });

        Store store = this.GridPanel1.GetStore();
        store.DataSource = myData;
        this.Session["MyData"] = myData;

        X.Msg.Notify("AjaxAction", message).Show();
        return "id0";
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
<html xmlns="">
<head id="Head1" runat="server">
    <title>Ext.Net Example</title>
    <form id="Form1" runat="server">
    <ext:ResourceManager runat="server" />
    <ext:GridPanel ID="GridPanel1" runat="server" AutoHeight="true">
            <ext:Store runat="server" GroupField="group">
                    <ext:JsonReader IDProperty="id">
                            <ext:RecordField Name="id" />
                            <ext:RecordField Name="group" />
                            <ext:RecordField Name="test" />
        <columnmodel runat="server">
                <ext:Column Header="ID" DataIndex="id" />
                <ext:Column Header="Group" DataIndex="group" />
                <ext:Column Header="Test" DataIndex="test" />
            <ext:RowSelectionModel runat="server" SingleSelect="true" />
            <ext:GroupingView runat="server" HideGroupedColumn="true" />
    <ext:Button runat="server" Text="DataBind and Select">
            <click handler="MyNamespace.DataBindAndSelect('Added new data to Session', {
                success: function(id) {
                    var index = GridPanel1.getStore().indexOfId(id);
            });" />

Keine Kommentare:

Kommentar veröffentlichen