Saturday, February 14, 2009

Ajaxified Grid 1

Use of ICallbackEventHandler may not be the good option to make grid Ajax enabled

In this post I will discuss following

  1. Sort the grid in ascending or in descending order by clicking on the arrows next to column name.
  2. Change pages of the grid.
  3. Change page length of the gird

We will used RenderControl function to get HTML.

Using this function of control we are able to get HTML of that control., but for this we will have to use HtmlTextWriter and StringWriter as follows


Here result will contain HTML format of the grid control. We will convert grid control to HTML after binding the data.

We will start developing the UI and code in following steps

Simply copy paste following code in the tag of the page

In above code we are just creating one grid and one dropdownlist.

You can switch to Design view to see it.

We have activated RowDataBound event of the grid.

Now we will create a DataTable to be used as DataSource to grid.

To use this table we will simply say “_grid.DataSource = _sampleData;”

We will write following four functions for grid operations

Name of the above functions indicate their functionality. Second parameter of the first three functions is “pageLength” this is the value of the dropdown list


Result string is declared on top of the page so that all members of the page can access it “string result = string.Empty;”. Check it in the demo code.

If there is any confusion please again go through above pages.

We need four functions for ICallBackEventHandler two are JavaScript functions one for Callback to server and another for displaying response data from server.

And two are Server side functions (RaiseCallbackEvent(string eventArgument) and GetCallbackResult())

JavaScript functions are as follows

When we want to do Callback, we are actually firing an event which is related to WEBFORM. So we will have to put UpdateGrid(args) function in tag. Otherwise we will get JavaScript error.
Since we can register this function by using “Page.ClientScript.RegisterClientScriptBlock” If we register above function using this that function will appear in tag only.

This function will be fired after server sends response back to client, thus this function will manipulate server response. We have simply put the response as innerHTML of the Div tag. This innerHTML contains HTML format of the updated grid.

Server-Side functions are as follows

This function simply returns the result which we have put as innerHTML in ShowResult function (Refer previouse point)

In this function eventArgument will appear as “changePage$1$10” or “sort$1$10” or “changePageLength$1$10” or “sort$Contact Name Asc$10” or “sort$Contact Name Desc$10”

If we split this on “$”, we will get

- Action .
- Page index of the Grid.
- Page size from the dropdown list (i.e. id is ‘ddl’).

(In changePageLength we don’t need args[2]; I have kept is just to makecoding little simple)

In RaiseCallbackEvent we have simply called the function which we have decleared in POINT 3, each function in point 3 will do the respective action and return a HTML string of the Grid.

Add following code protected void Page_Load(object sender, EventArgs e)

[At this point if we compile the code we will able to chage the page length ]

Now we will write code in the RowDataBound event of the Grid.. I.e. in protected void _grid_RowDataBound(object sender, GridViewRowEventArgs e)

For adding columnwise sorting functionality in grid, we will modify columnheader row of the grid.

We have added Up and down images and onclick of either of them we have called UpdateGrid function (Ref. Point 4) , for sorting Ascending and Descending order repectively. [At this point you can complie the code and the grid will be Sortable].

After this we will modify the Pager row in such a way that we can use it to change the page index of the grid using UpdateGrid function.(Ref. point 4 public void RaiseCallbackEvent)

_pageCount contains the pagecount of the grid. We have to display pages as Numbers so we used HyperLink and Label to add space after each page number. We can choose our own way to display pages, just we will have to take care of adding the attribute of JavaScript.

And now we have created an Ajax enabled grid using ICallbackEventHandler.

(Don’t forget to Add ICallbackEventHandler). Digg Technorati Delicious StumbleUpon Reddit BlinkList Furl Mixx Facebook Google Bookmark Yahoo
ma.gnolia squidoo newsvine live netscape tailrank mister-wong blogmarks slashdot spurl

No comments:

Post a Comment

blogger templates 3 columns | Make Money Online