Posted on Thursday, September 17, 2009 at 6:49 AM
The following method would be called after a form submit occurs on the popup. I placed the method call in the OnClick event for my submit button. The preprocessor directive of System.Text is required for the stringbuilder that I use below (You don't actually have to use a stringbuilder, but I find it makes it easier to read and debug later).
private void RegisterCloseWindowScript()
{
if (!Page.IsClientScriptBlockRegistered("CloseWindow"))
{
StringBuilder sb = new StringBuilder();
sb.Append("<script language='javascript'>");
sb.Append("window.opener.location.href = ");
sb.Append("window.opener.location.href;");
sb.Append("window.opener.focus();");
sb.Append("window.close();");
sb.Append("</script>");
Page.RegisterClientScriptBlock("CloseWindow", sb.ToString());
}
}
The following is an example of how to validate a field using the asp:customvalidator control. I am going to be validating a textbox to ensure that its entry is alphanumeric without any spaces.
This is the field to be validated...
<asp:TextBox id='PersonId' runat='server' />
Here is the asp:CustomValidator on the aspx page.
<asp:CustomValidator id='PersonIdCustomValidator' runat='server'
Display='Dynamic'
ControlToValidate='PersonId'
EnableClientScript='true'
ClientValidationFunction='ValidatePersonId'
OnServerValidate='ValidatePersonId'
Text='* PersonId must be alphanumeric without any spaces' />
Javascript implementation of the client-side validation.
function ValidatePersonId(source, args)
{
var re = new RegExp("^\\w*$");
args.IsValid = (window.event.srcElement.value.match(re));
}
C# codebehind implementation of the server-side validation.
protected void ValidatePersonId(object source, ServerValidateEventArgs args)...