asp中的JavaScript:向导

时间:2012-01-24 14:17:27

标签: javascript asp.net wizard

我有一个向导控制,用户可以在一步中输入他的名字,电子邮件地址,电话号码等。

当用户按下向导的“下一步”按钮时,我正在检查数据库以查看是否存在具有指定电话号码的激活帐户。

如果是这种情况,系统应该询问用户是否将新信息绑定到该号码,或者他是否会输入新的电话号码。

如果他说他将绑定信息,则信息被绑定,向导进入步骤2,如果他将输入新的电话号码,则向导将保留在步骤1。

代码看起来像这样:

protected void Wizard1_NextButtonClick(object sender, WizardNavigationEventArgs e)
{
    if (Wizard1.ActiveStepIndex == 0)
    {
        Page.Validate();
        if (Page.IsValid)
        {
            if (!Mobile_ValidateForExistingUsers(((TextBox)WizardStep1.ContentTemplateContainer.FindControl("txtPhone")).Text))
            {
        //JavaScript popup or something, which prompts the user?
            }
        }
    }
}

验证者是:

protected bool Mobile_ValidateForExistingUsers(string usrPhone)
{
    bool IsValid = false;

    using (SqlConnection conn = new SqlConnection(connString))
    using (SqlCommand cmd = new SqlCommand("spCheckMobile", conn))
    {
        cmd.CommandType = System.Data.CommandType.StoredProcedure;
        cmd.Parameters.Add(new SqlParameter("@Mobile", usrPhone));

        cmd.Connection.Open();

        object result = cmd.ExecuteScalar();

        if (result == null)
        {
            IsValid = true;
        }
    }
    return IsValid;
}

如何向用户询问此问题,然后继续或让他在向导中输入一些新信息?

1 个答案:

答案 0 :(得分:0)

我认为处理这个问题的最有效方法是使Mobile_ValidateForExistingUsers(phoneNumber)函数成为一个用于AJAX调用的WebMethod。编写AJAX代码后,将AJAX调用附加到<asp:CustomValidator>以获取其客户端验证函数,如果返回false,则使用window.alert('use new number')进行跟进。

或者,您可以使用<asp:CustomValidator>通过服务器端代码处理大部分内容,但如果您正在处理验证服务器端,则无需设置客户端验证功能。只需将CustomValidator1.IsValid属性设置为Mobile_ValidateForExistingUsers(phoneNumber)函数的结果,然后调用Page.Validate()以查看该页面是否仍然有效。如果没有,请标记runat="server"隐藏输入并让一些客户端代码在window.onload上提示用户,如果他们想要使用新号码,则将其存储在第二个隐藏字段中。示例如下:

protected void Wizard1_NextButtonClick(object sender, WizardNavigationEventArgs e)
{
    if (Wizard1.ActiveStepIndex == 0)
    {
        Page.Validate();
        if (Page.IsValid)
        {
            if (HiddenField2.Value == '')   //Client-side Flag not set
            {
                if (!Mobile_ValidateForExistingUsers(((TextBox)WizardStep1.ContentTemplateContainer.FindControl("txtPhone")).Text))
                {
                    CustomValidator1.IsValid = false;   // Invalidate the page so the Next Step fails.
                    HiddenField1.Value = false;         // Page will get re-rendered to client to fix errors and stay on the same step.
                }
            }
            else
            {
                if (HiddenField2.Value == 'true')
                {
                    // Use new binding logic.
                }
                else
                {
                    //User does not want to use new number, so logic to handle goes here.
                }
            }

        }
    }
}

然后在客户端标记的某处:

/*
 * Crude example
*/
<script type="text/javascript">
    window.onload = function () {
        'use strict';
        var hf1 = document.getElementById('<%=HiddenField1.ClientID %>');
        var hf2 = document.getElementById('<%=HiddenField2.ClientID %>');
        var myForm = document.getElementById('<%=Form1.ClientID %>');
        if (hf1.value === 'false') {    // or if (hf1.value) if you just want the truthiness of the value's existence (depending on how you flag stuff)
            hf2.value = window.confirm('Wanna use this number?').toString().toLowerCase();
        }
        myForm.submit();
    }
</script>

希望这有帮助,

皮特