Thursday, October 02, 2008

Add Scripts to Client Side Page 3

In my first post on this topic, I mentioned that there some cases that prompts may not be desired. You can avoid prompt display for a control's click event by calling the base class's method BypassModifiedMethod().

This method actually disables the prompt display by setting the client side page level script var on click event:

  m_needToConfirm = false;

However, in case of saving failure, such as exception raised from back-end DB, a postback call may be called back to the client side with some error message being displayed. This would cause a problem: if the user redirects to another page, the prompt would not be displayed and the changes may not be lost.

To resolve the problem, I added a method ResetOnSubmit() in the base class. You should call this method on Page_Load event for postback case if you want to reset the flag variable back.

protected void Page_Load(object sender, EventArgs e)
{
...
if (!IsPostBack)
{
base.ResetOnSubmit();
...
}
...
}

Another handy method I added to the client side base class is DebugChanges():

protected void DebugChanges(
WebControl wc,
string wcEventAttribute,
string clientIDForDisplay)
{
string script = string.Format("javascript: displayChanges('{0}')",
clientIDForDisplay);
wc.Attributes[wcEventAttribute] = script;
}

where displayChanges() is a client side javascript function registered on the client side page level. This function will display the current monitored changes and original values on a WebControl specified by control's event. You call this method in Page_Load event to add this debug feature. You can decide when the control is visible. I used a URL request parameter to enable the visibility of a control in one case. See my previous post on Using Query String Parameters in ASP.Net Page.

As I promised in my previous post, here is the source codes of ClientSidePage class.

0 comments: