Introduction | |||||
This article explains about resizing web controls in runtime. In browser output, usually resizing is achieved during Development for fixing the control’s width and height. But sometimes we require resizing the DataGrid, DataList or other data controls to adjust for clearly reading the displayed data. It is rare to see the internet page's output to be adjustable, but it is possible with a few lines of JavaScript. Event Types The whole resize logic (column width increase or decrease) depends upon catching the co-ordinate where the user drags the column to be resized. Mouse capturing will be set on initially when the page loads. Events to be captured are the following: · Mouse Over · Mouse Down · Mouse Up Style Settings Resizing can be done only on the column headers. Usually all data controls, like DataGrid, GridView or DataList, will be set with some style class for the header row. Here we take advantage of this style for tracking the user's mouse over an event on the header row. This event will initiate the trigger to start capturing the further events like mouse down and mouse up. There is no special style setting to be done other than the user’s look and feel styles. The JavaScript just requires a style name and that is it! Mouse Capturing The user will be automatically seeing the difference in the cursor which will signal to them that the DataGrid/GridView is ready for resizing. This will be done by the handler which we have set for document onmousemove. Whenever the user reaches the header row, the first level job is over and mouse down event will be ready to be raised for checking where the user clicks the mouse to start dragging. At this moment the mouse move event will again take over. Now the JavaScript will start handling mousedown and mousemove, which is equal to mouse drag. On the mouse drag, the column size and the table size will increase or decrease based on the mouse’s left or right movement respectively. Whenever the user makes the mouse go up (stops dragging and releasing the mouse), the resizing will get stopped and the current drag object will be destroyed.
Here is full JavaScript Code var firefox = document.getElementById&&!document.all; //Check whether the browser is FF;var isDraggable = true; var xX=0;anchorChild=0;anchorMain=0; var objOn=null;gridMain=null; var lastObj=undefined; var headerStyle="DataGrid-HeaderStyle"; document.onmouseup=mouseUpCapture; document.onmousemove=mouseMoveCapture; document.onmousedown=mouseDownCapture; /*EventHandler to capture Mouse Up*/ function mouseUpCapture(){isDraggable = false;lastObj=undefined;} //Dragging Stop and Forget LastObj; /*EventHandler to capture Mouse Down*/ function mouseDownCapture(e){ if(!firefox)e = event;objOn=(!firefox)?e.srcElement:e.target; //Check whether the browser is FF and set event object accordingly; //To Check whether the mouse hovers over the HeaderRow and cursor type is currently re-sizable; if(objOn.parentElement.className == headerStyle && document.body.style.cursor=='e-resize') { isDraggable = true; //Start understanding that we can start dragging; gridMain = objOn.parentElement.parentElement.parentElement; //Getting the object of main table which corresponds to current column's width variation; xX = e.clientX; //Getting the current Mouse X Position; anchorChild = objOn.offsetWidth; //Getting the current Cell/Column Width; anchorMain = gridMain.offsetWidth; //Getting the current Table Width; lastObj = objOn; //Stores the current mouse hovering object for moving reference; } else{lastObj = undefined;} } /*EventHandler to capture Mouse Move*/ function mouseMoveCapture(e){ if(!firefox)e = event; //Check whether the browser is FF and set event object accordingly; objOn=(!firefox)?e.srcElement:e.target; if(lastObj!=undefined)objOn=lastObj; //Loads the mouse hovered object for moving reference; //Check whether the mouse hovers over the Headerrow's column borders and Make the cursor re-sizable; document.body.style.cursor =(objOn.offsetWidth-e.offsetX < 10 && objOn.parentElement.className == headerStyle)?'e-resize':'default'; //Start Dragging if(isDraggable) { document.body.style.cursor='e-resize'; //Maintaing the cursor as re-sizable ic=e.clientX-xX+anchorChild; //Current_MouseX_Position - MouseX_DragStart + Cell_Width; it=e.clientX-xX+anchorMain; //Current_MouseX_Position - MouseX_DragStart + Table_Width; if(ic>0) { gridMain.style.width = it + "px"; //Increase Table Width; objOn.style.width = ic + "px"; //Increase Cell Width; } } } |
Resizing Data Controls in ASP.NET Applications
Posted by Vigneshwar Gupta
30
Jun
Subscribe to:
Post Comments (Atom)
0 Responses to "Resizing Data Controls in ASP.NET Applications"
Post a Comment