|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Announcements
Want a new Job?
Chapters
Services
Feature Zones
|
IntroductionDid you know that you can call any JavaScript function or stop the user from moving to the next page when he clicks on the paging links in an ASP.NET ProblemI was once having an editable grid and wanted to make sure that if a user changes the data, he is alerted about it if he tries to go out of the page without saving the change. I was able to put a boundary validation everywhere on the web page, but I wasn't getting how to put it on the How I solved itI realized that it could only be managed if I use a custom data grid control and manage the paging myself on its BenefitsYou can not only alert a user that he is navigating out of the page but also confirm for save changes if he hasn't saved the information while navigating to a different page. Technical partHow I did itI created my own custom public class DataGridCustom : System.Web.UI.WebControls.DataGrid
{
..
public string BeforePagingCallJSFunction
..
protected override void Render(HtmlTextWriter output)
{
if ( AllowPaging == true && BeforePagingCallJSFunction != "" )
{
//Get the output string rendered.
System.Text.StringBuilder sb =
new System.Text.StringBuilder();
HtmlTextWriter writer =
new HtmlTextWriter(new System.IO.StringWriter(sb));
base.Render(writer);
//the output has been retrieved in the stringbuilder
//AND do the modification here
string datgridString = sb.ToString();
//Attach javascript to top pager
if ( this.PagerStyle.Position == PagerPosition.Top ||
this.PagerStyle.Position == PagerPosition.TopAndBottom )
{
int firstRowPos = datgridString.IndexOf(@"</tr>");
string firstpart = datgridString.Substring(0,firstRowPos);
string secondpart = datgridString.Substring(firstRowPos);
//Use Page.GetPostBackEventReference instead of __doPostBack
firstpart = firstpart.Replace("href=\"javascript:__doPostBack(",
( "style='cursor:hand;text-decoration:underline;" +
"color:blue' href1='javascript:void(0)'" +
" onclick=\"javascript:" +
beforePagingCallJSFunction + ";__doPostBack("));
datgridString = firstpart + secondpart;
}
//Attach javascript to bottom pager
if ( this.PagerStyle.Position == PagerPosition.Bottom ||
this.PagerStyle.Position== PagerPosition.TopAndBottom)
{
int lastRowPos = datgridString.LastIndexOf("<tr");
string firstpart = datgridString.Substring(0,lastRowPos);
string secondpart = datgridString.Substring(lastRowPos);
//Use Page.GetPostBackEventReference instead of __doPostBack
secondpart = secondpart.Replace("href=\"javascript:__doPostBack(",
( "style='cursor:hand;text-decoration:underline;" +
"color:blue' href1='javascript:void(0)' " +
"onclick=\"javascript:" +
beforePagingCallJSFunction + ";__doPostBack("));
datgridString = firstpart + secondpart;
}
output.Write(datgridString.ToString());
}
else
{
base.Render(output);
}
}
}
How to use itIn ASPX, get the proper references, enable paging (
Some Technical FAQs
Future
ConclusionNot necessarily every one will like it, but it is for developers who have ever tried implementing boundary handling in their web applications, because this is a place they never had control earlier while validating if the page content was saved or not, here they can now. Please free to post your comments and feedback.
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||