Use of ICallbackEventHandler may not be the good option to make grid Ajax enabled
In this post I will discuss following
In this post I will discuss following
- Sort the grid in ascending or in descending order by clicking on the arrows next to column name.
- Change pages of the grid.
- 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
e.g.
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
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).
No comments:
Post a Comment