0

I am working on an asp.net webforms project. I have a usercontrol with a boolean public property, named as IsRequired, which can be set either to true or false. I have a text box where the user will enter the social security number (SSN). If IsRequired = true, then the user has to enter the SSN value, otherwise validation should fail and "SSN is required" message should be displayed next to the textbox. If IsRequired = false, then if the user doesn't enter a SSN value, then a messagebox should be displayed with the message, "If you have a SSN, please enter it". Essentially the message encourages the suer to enter SSN. If the click "OK" on the messagebox, then they could enter the value. otherwise they could submit the page, and the validation should not fail.

I am using DevExpress controls, and came up with the following solution:

on ascx page:

<dx:ASPxTextBox  ID="SsnTextBox" runat="server" OnValidation="SocialSecurity_Validation">
                <MaskSettings Mask="999999999"></MaskSettings>
            </dx:ASPxTextBox>

on the codebehind:

 if (IsSubscriber && (e.Value == null || (string)e.Value == ""))
        {
            SsnTextBox.ErrorText = "Required";
            e.IsValid = false;
        }        
        else if (!IsSubscriber && (e.Value == null || (string)e.Value == ""))
        {
            e.IsValid = true;
            Page.ClientScript.RegisterClientScriptBlock(this.Page.GetType(), "UserControl",
                                                   "confirm('If Social Security Number is available, please enter it!')", true);
        }

The problem with the code it works only partially. For example, the message box is displayed, but when either ok or cancle is clicked, the page is posted without giving the option to enter a value. If IsRequired = true, and a value is not entered, validation fails, still the page gets submitted. Any help to resolve this or a better code is appreciated. The solution need not be DevExpress specific, instead it could a generic one.

Thanks

4

1 回答 1

0

Rather than use the confirm method; which will return either a true or false depending on what the user selects, why not use Alert() instead?

I.E

return Alert('If Social Security Number is available, please enter it!')

There won't be a choice for the user - just an OK button. 'return' will also prevent the postback.

于 2012-10-22T18:21:18.290 回答