Monday, September 8, 2008

Add onclick event in Gridview's page index

In gridview, you have a field in which checkboxes are available and using that checkeditems you are going to do some process, say update.  Doing so, when user checked the checkbox(es) and tried to click on next page, the selected items will be vanished.  If you wish to give a warning message as "if you choose next page to navigate, then selected items will lost".  What could you do ?

We cannot directly get the page index value.  Doing so, in Row_DataBound event, check as
e.Row.RowType as pager.  If so, take the count of pages as below :

int count = e.Rows.cells[0].controls[0].controls[0].count;

for(int index=0; index<=count-1;index++)
 { System.Web.UI.WebControls.TableCell cell = (System.Web.UI.WebControls.TableCell)e.Rows.cells[0].controls[0].controls[0]; cell.Attributes.Add("onclick","alert('If you choose next page to navigate, the selected items will lost.')"; } 

Pager row will be rendered as,


"
123
"
.To access this, we have to use controls[0].  

No comments: