1

我想验证文本更改事件的文本框。当我单击按钮并输入任何无效地址时,首先出现无效电子邮件地址错误,然后立即隐藏。

我还有一个 isRecordAlreadyExist 函数,这个函数检查数据库中是否存在重复值。我希望在 textchanged 事件中执行此检查。但是事件 Dosent 被触发。并且不检查该值。我的代码:

<tr>
            <td style="padding-top: 20px;">
                Email Address<asp:RequiredFieldValidator ID="RequiredFieldValidator5" runat="server"
                    ErrorMessage="*" ControlToValidate="txtEmail" ForeColor="Red" ValidationGroup="val" Display="Dynamic"></asp:RequiredFieldValidator>
            </td>
            <td style="padding-top: 20px;">
                <asp:UpdatePanel ID="UpdatePanel2" runat="server">
                    <ContentTemplate>
                        <asp:TextBox ID="txtEmail" runat="server" OnTextChanged="txtEmail_TextChanged" AutoPostBack="true"></asp:TextBox><asp:RegularExpressionValidator
                            ID="regexEmail" runat="server" ErrorMessage="Invalid Email Address" ControlToValidate="txtEmail"
                            ValidationExpression="\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*" ForeColor="Red"
                            ValidationGroup="">
                        </asp:RegularExpressionValidator>
                    </ContentTemplate>
                </asp:UpdatePanel>
            </td>
        </tr>
<tr>
            <td style="padding-top: 10px;">
                <asp:Button ID="btnSubmit" runat="server" Text="Submit" CssClass="btnSubmitCSS" OnClick="btnSubmit_Click"
                    ValidationGroup="val" OnClientClick="return chk_isValid();" />
                <asp:Button ID="btnReset" runat="server" CausesValidation="False" Text="Reset" CssClass="btnSubmitCSS"
                    OnClick="btnReset_Click" />
            </td>
        </tr>

我的代码背后..

protected bool isRecordAlreadyExist(TextBox txt_Value, int res)
{
    ds = new DataSet();
    paramArray = new string[3, 2];
    paramArray[0, 0] = "@uname";
    paramArray[0, 1] = txtuname.Text.Trim();
    paramArray[1, 0] = "@emailid";
    paramArray[1, 1] = txtEmail.Text.Trim();
    paramArray[2, 0] = "`";
    obj = new DalLib();
    ds = obj.getDataSet("sp_Tbl_Login_MatchValues", paramArray);
    gvLogin.DataSource = ds.Tables[res].DefaultView;
    if (ds.Tables[res].Rows.Count > 0)
    {
        mtvResult.ActiveViewIndex = 3;
        btnSubmit.Enabled = false;
        return false;
    }
    else
    {
        btnSubmit.Enabled = true;
        return true;
    }
}

我的 asp:查看控件:

<asp:View ID="vAlreadyExist" runat="server">
            <img src="Images/delete.png" alt="Edit" />
            <asp:Label ID="lblAlreadyExist" runat="server">Record Already Exist</asp:Label>
        </asp:View>


protected void btnSubmit_Click(object sender, EventArgs e)
    {

        if (ViewState["action"].ToString() == "Insert")
        {
            obj = new DalLib();
            paramArray = new string[7, 2];
           // if (!string.IsNullOrEmpty(txtFullName.Text.Trim()))
            {
                paramArray[0, 0] = "@FullName";
                paramArray[0, 1] = txtFullName.Text.Trim();
            }

            //if (!string.IsNullOrEmpty(txtuname.Text.Trim()))
            {

                paramArray[1, 0] = "@uname";
                paramArray[1, 1] = txtuname.Text.Trim();
            }
            //if (!string.IsNullOrEmpty(txtPwd.Text.Trim()))
            {
                paramArray[2, 0] = "@pwd";
                paramArray[2, 1] = txtPwd.Text.Trim();
            }
            //if (!string.IsNullOrEmpty(txtEmail.Text.Trim()))
            {
                paramArray[3, 0] = "@emailid";
                paramArray[3, 1] = txtEmail.Text.Trim();
            }

            paramArray[4, 0] = "@isAdmin";
            paramArray[4, 1] = chkAdmin.Checked.ToString();

            paramArray[5, 0] = "@isActive";
            paramArray[5, 1] = chkActive.Checked.ToString();
            paramArray[6, 0] = "`";
            result = obj.setData("sp_Tbl_Login_Insert", paramArray);
            LoadData();
            EmptyFields();
            mtvResult.ActiveViewIndex = 0;
        }

}

protected void txtEmail_TextChanged(object sender, EventArgs e)
{
    isRecordAlreadyExist(txtEmail, 1);
}
4

1 回答 1

3

您正在将客户端验证与服务器混合。你有AutoPostBack="true"文本框。正则表达式检查客户端上的验证,不需要 AutoPostBack。单击按钮时需要服务器验证。Removing AutoPostBackfrom text box 将导致客户端验证文本框check the email format模糊,服务器验证将在按钮单击时完成。也remove OnTextChanged="txtEmail_TextChanged"来自文本框。

于 2012-10-20T07:23:16.077 回答