日期:2014-05-03  浏览次数:22981 次

       

请输入合数:<asp:TextBox ID="txtNum" runat="server" Width="82px"></asp:TextBox>
        <asp:CustomValidator ID="cvNum" runat="server" ControlToValidate="txtNum" ErrorMessage="您输入的数字是素数"
            OnServerValidate="cvNum_ServerValidate"></asp:CustomValidator><br />
        <asp:Button ID="btnSubmit" runat="server" OnClick="btnSubmit_Click" Text="提交" /><br />
        <asp:Label ID="lblMessage" runat="server"></asp:Label>

 

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        if (IsValid == true)
        {
            lblMessage.Text = "输入的数据合适!";
        }
        else
            lblMessage.Text = "";
    }
    protected void cvNum_ServerValidate(object source, ServerValidateEventArgs args)
    {
        int t = int.Parse(args.Value);   //获取用户从页面文本框控件中输入的数据
        for (int i = 2; i <= t / 2; i++)
        {
            // 利用循环判断t能否是合数
            if (t % i == 0)
            {
                args.IsValid = true;
                return;
            }
            else
                args.IsValid = false;
        }
    }
}