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

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




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

I.e.




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

Thursday, February 12, 2009

get HTML - of serverside control for Ajax handling

This post is moved to 

ASP DOT NET MATTERS

Click on Above Link to Get Redirect to Desired Page.

Direct Blog address : http://www.aspdotnetmatters.blogspot.com Digg Technorati Delicious StumbleUpon Reddit BlinkList Furl Mixx Facebook Google Bookmark Yahoo
ma.gnolia squidoo newsvine live netscape tailrank mister-wong blogmarks slashdot spurl

Monday, February 2, 2009

Ajax-Handle complex data type

This Post is Moved to 

Asp Dot Net Matters


Click on Above like to redirect to Desired page.

Check blog: http://www.aspdotnetmatters.blogspot.com Digg Technorati Delicious StumbleUpon Reddit BlinkList Furl Mixx Facebook Google Bookmark Yahoo
ma.gnolia squidoo newsvine live netscape tailrank mister-wong blogmarks slashdot spurl

Saturday, January 31, 2009

XMLHTTP class

This post is Move to ASP DOT NET MATTERS

Direct link : http://aspdotnetmatters.blogspot.com/2010/07/xmlhttpjsclass-json-class-for-ajax.html


For More Details check http://aspdotnetmatters.blogspot.com Digg Technorati Delicious StumbleUpon Reddit BlinkList Furl Mixx Facebook Google Bookmark Yahoo
ma.gnolia squidoo newsvine live netscape tailrank mister-wong blogmarks slashdot spurl

CSS- Bubble Tooltip

This Post is Moved to


ASP DOT NET MATTERS

http://www.aspdotnetmatters.blogspot.com Digg Technorati Delicious StumbleUpon Reddit BlinkList Furl Mixx Facebook Google Bookmark Yahoo
ma.gnolia squidoo newsvine live netscape tailrank mister-wong blogmarks slashdot spurl

Friday, January 30, 2009

Partial Page Rendering

Partial page rendering is one of the very amazing concepts by Microsoft Ajax extension. In this article we will discuss one very common and real time scenario for which Partial page rendering is very useful. Whenever there is need to render a server side control for Ajax web application, partial page rendering is very good option.

For example, in my ICallback articles we have discussed about the Ajaxifying the grid view; but consider following scenario,
Grid view contains details of the all employees of an organization, and then in that case employees are categories in various ways,
  • Depending on the role of an employee.
  • Depending on the CTC of an employee.
  • Depending on the City of an employee.
  • Depending on the Department of an employee.

And many more, then in such case if we are using same Gridview to display all employees, and along with it, we need to provide an instant update mechanism in the grid; but for each category of employee, details which need to be edited are different and may contain information which is not part of the grid, grid is containing only common information.

Partial page rendering is a very good option for such case; we don’t need to handle anything on client side other than managing partial rendering calls. For this we will use Microsoft Ajax extension, i.e. Update Panel, Update progress and script manager.
Now if we are going to user different web user controls for different purposes then this approach is more effective.

We will consider following case:
  • We have main page which contains a grid.
  • On click of edit image in grid column, user should be able to edit the details of that particular employee.
  • Depending on the employee id, display information to edit.
  • This may contain more than one table to fetch and update the data.
  • To make it more modular use Web user controls for data update.

To avoid database dependency we will use Data Tables as properties, and will use sessions to maintain different data tables.

Steps for the code development are as follows.
  1. Design a Grid View the way we want it to be displayed.
  2. Develop user controls to handle various types of users, and make sure all of them work fine. Here we can keep the login on the same page.
  3. Add an update panel in the Design, this update panel contains all required user controls in different Panels and web user controls; visibility of each panel and web user control is set to false.
  4. Add one server side Button on which we will update the update panel, add this button as a trigger for update panel.
  5. On click event of the button contains code to handle update events.

Develop Aspx Page

Form tag of aspx page will be as below


btnClick is set hidden using Style .
In above logic we have added Triggers for update panel, there can be more than one trigger for the update panel. These triggers are responsible to send Post back of update panel, i.e. Partial page will go to server side, and server will render only the contents inside the Update panel. asp:AsyncPostBackTrigger represents asynchrous post back.



We are going to write server side code on the click event of above button, and changes will reflect in update panel. So if we have more than one trigger for an update panel, we can handle different events very much effectively. Also if our update panel contains some user control or some button to update server side data, it gets updated asynchronously , for that we don’t need to put extra code.
E.g. if we have a web user control in Update panel, and that update panel contains, some server side buttons, click event of all such elements get handled automatically.

In the demo code we have used one static class, which has public static table; this is used just to avoid database connection.
We have following tables in the static class.
1. DT – General table containing general (Common) information about the Employees
2. Manager – Table containing information about the employees who are managers.
3. TeamLeader – Table contains information about the employees who are Team leader.

In Demo code we have a GridView which contains common information about employees. Thus on page load we have following code.




Our girdview on aspx is



Now onclick of Image1 we need to fire trigger of an update panel i.e. click event of btnClick. So onlick of Image1 we have to do following tasks
Set the employee id in hdnValue, this field is part of an update panel,thus we can read it’s value on server side, for partial page rendering. And call click event of btnClick.
For this we use server side GridView1_RowDataBound event.



So on click of Image1 in grid, we will fire AsyncPostBack to server. In above code check img.ID = e.Row.Cells[1].Text; this line of code will append empId for DT, to the image id so that we can assing the image id to hiddenField on client side. If we split the img.ID on client side on “_”, the last element of an array will be EmpId.
Check the schema of DT for this.

Up to this point we are able to send Asynchronous Post back request to server; after this we will develop logic to change the contents in Update panel, for the understanding purpose, we will only hide and show the controls in update panel and fetch employee data from table and display it in controls. Further we can make it more and more complex depending on the requirement.

In code Update panel contains one Panel and Two web user control, panel will take care of an employee who is manager and web user controls are used for other types of employees.

btnClick ‘s server side click event is used to hide and show the data.



This panel contains Update button, update details is handled totally on serverside.
This is how we will handle Page events of page. Now in click_Click event, TeamLeader1 is an instance of TeamLeader web usercontrol in UpdatePanel. TeamLeader web user control contains all server side logic to update the details of TeamLeader. To access value of hidden field of parent page we will use following line in page load event of web user control.



We have handled all update and retrive events on server side, upto this we have used javascript just to set the value of hidden field.

Now we will discuss about



Script manager is required to execute Asynchronous Post back, whenever we use Update Panel we need Scrpt manger, Scriptmanager is also used to add script refrences as above.

We can handle various client side events related to Partial Postback and Update panels, some of them we have used in Jscript.js file.


PageLoad is one such event in which we can initialize some functions and assign client side events. Page load event is as below, events which can be used, but not required for this demo are commented in the Jscript files. There are many such events Microsoft has provided.

PageLoad() event is as below



OnBeginRequest and OnEndRequest are functions which get called on perticular event. Here we haved used them to display a Processing Div tag, irrespective of update panel.
Update Process is one of the control provided by Microsoft Ajax1.0, which does the same thing but we need it to associate with UpdatePanel., we are not going into details, of it.



z-index of this div tag is set very high so that whenever it is visible, it will be above all objects of the page.
Just check JavaScript used for it.



Now this div tag will be visible when ever partial page render request goes to server, whether request is because of elements inside the update panel or out side the update panel. Many times such approch is useful, because , we might use partial page renderring many times on the page, with more than one update panel also.

args contains arguments, please debug the JavaScipt using breakpoint,just like c#.net, and check the atributes. There are many useful attributes, we can make use of.


Conclusion of this article
Many times it is useful to develop code using concept of partial page rendering, which can excel the development time, and managing code becomes more easy, no need to write extra JavaScript to handle Ajax requests.
Digg Technorati Delicious StumbleUpon Reddit BlinkList Furl Mixx Facebook Google Bookmark Yahoo
ma.gnolia squidoo newsvine live netscape tailrank mister-wong blogmarks slashdot spurl

Thursday, January 29, 2009

ICallback Event handler

This Post is Moved to

ASP DOT NET MATTERS Digg Technorati Delicious StumbleUpon Reddit BlinkList Furl Mixx Facebook Google Bookmark Yahoo
ma.gnolia squidoo newsvine live netscape tailrank mister-wong blogmarks slashdot spurl

blogger templates 3 columns | Make Money Online