如何在asp.net中验证页面(page.isvalid) [英] how to validate page (page.isvalid) in asp.net

查看:110
本文介绍了如何在asp.net中验证页面(page.isvalid)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好!,



i在客户端使用和使用按钮点击事件进行验证

hi all!,

i have using on client side validation with and using the buton click event

Page.Validate("Register")
            If Page.IsValid Then
'-------------code
endif





但它不是vlid它显示page.IsValild是false。



but it is not vlid it show page.IsValild is false.

推荐答案

你使用的是验证控件,对?这个控件首先尝试在客户端验证,因此,只有在有效或者在客户端bronwser上禁用了js时才会触发事件。在后面的代码上你需要手动验证。



are you using validations controls, right? this controls first try to validate on client side, so, event will be fired only if is valid or if js is disabled on client bronwser. On code behind you need to verify manually.

<div>
        <asp:textbox id="TextBox1" runat="server" xmlns:asp="#unknown"></asp:textbox>
        <asp:requiredfieldvalidator xmlns:asp="#unknown">
        ID="rfv1" runat="server" ErrorMessage="*" ControlToValidate="TextBox1">
        </asp:requiredfieldvalidator>
        <br />
        <asp:button id="Button1" runat="server" text="Button" xmlns:asp="#unknown" />
        <br />
        <asp:literal id="Literal1" runat="server" xmlns:asp="#unknown"></asp:literal>
    </div>





代码落后:





code behind:

void Button1_Click(object sender, EventArgs e)
    {
        Page.Validate();
        if (Page.IsValid)
        {
            Literal1.Text = "valid";
        }
        else
        {
            Literal1.Text = "not valid";
        }
        
    }


<asp:CustomValidator ID="cusValChecks" runat="server" Display="None" OnServerValidate="cusValChecks_ServerValidate"></asp:CustomValidator>


protected void cusValChecks_ServerValidate()
{
    IsValid = true;

    if (txtName.Text.Trim() == "")
            {
                lblError.Text = "Invalid";
                IsValid = false;
            }
}


void Button1_Click(object sender, EventArgs e)
{
    if (!Page.IsValid)
            {
                return;
            }
}


这篇关于如何在asp.net中验证页面(page.isvalid)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆