ASP.Net/Validation by Control/Javascript Validation — различия между версиями
Admin (обсуждение | вклад) м (1 версия) |
|
(нет различий)
|
Текущая версия на 11:51, 26 мая 2010
Display ValidationSummary in a client side dialog (C#)
<%@ Page Language="C#" %>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
<title>Show Summary Popup</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:ValidationSummary
id="ValidationSummary1"
ShowMessageBox="true"
ShowSummary="false"
Runat="server" />
<asp:Label
id="lblFirstName"
Text="First Name:"
AssociatedControlID="txtFirstName"
Runat="server" />
<br />
<asp:TextBox
id="txtFirstName"
Runat="server" />
<asp:RequiredFieldValidator
id="reqFirstName"
ErrorMessage="First Name is required"
ControlToValidate="txtFirstName"
Display="None"
Runat="server" />
<br /><br />
<asp:Label
id="lblLastName"
Text="Last Name:"
AssociatedControlID="txtLastName"
Runat="server" />
<br />
<asp:TextBox
id="txtLastName"
Runat="server" />
<asp:RequiredFieldValidator
id="reqLastName"
ErrorMessage="Last Name is required"
ControlToValidate="txtLastName"
Display="None"
Runat="server" />
<br /><br />
<asp:Button
id="btnSubmit"
Text="Submit"
Runat="server" />
</div>
</form>
</body>
</html>
Use Javascript and asp validation control together (VB.net)
<%@ Page Language="vb" %>
<html>
<head>
<title>Validation Control Example</title>
<script language="javascript">
<!--
function ClientValidate(source, arguments)
{
//alert(arguments.Value);
var r, re; //Declare variables.
re = new RegExp(/^[1-9][0-9][0-9][0-9]$/); //Create regular expression object.
r = re.test(arguments.Value); //Test for match.
arguments.IsValid = r; //Return results.
}
-->
</script>
<script runat="server">
Sub Page_Load()
vsSummary.DisplayMode = ValidationSummaryDisplayMode.List
End Sub
Sub ServerValidation (source As object, args As ServerValidateEventArgs)
Dim RegExVal As New System.Text.RegularExpressions.Regex("^\d{4}$")
If RegExVal.IsMatch(args.Value) Then
args.IsValid = True
Else
args.IsValid = False
End If
End Sub
</script>
</head>
<body>
<h1>Validation Control Example</h1>
<form runat="server">
<asp:table id="MyTable" border="1" cellpadding="5" cellspacing="0" runat="server">
<asp:tablerow runat="server">
<asp:tablecell runat="server">
Compare Validator Control:
<br><br>
Enter two numbers to compare
</asp:tablecell>
<asp:tablecell runat="server">
<asp:textbox id="value1" runat="server"/><br>
<asp:textbox id="value2" runat="server"/><br>
<asp:comparevalidator id="cvCompare"
controltovalidate="value1"
controltocompare="value2"
operator="equal"
type="integer"
errormessage="Fields are not equal!"
display="dynamic"
runat="server"/>
</asp:tablecell>
</asp:tablerow>
<asp:tablerow runat="server">
<asp:tablecell runat="server">
ValidationSummary Control:
</asp:tablecell>
<asp:tablecell runat="server">
<asp:validationsummary id="vsSummary"
displaymode="bulletlist"
headertext="Page has the following errors: "
showsummary="true"
showmessagebox="false"
runat="server"/>
</asp:tablecell>
</asp:tablerow>
<asp:tablerow runat="server">
<asp:tablecell colspan="2" runat="server">
<asp:button text="submit" runat="server"/>
</asp:tablecell>
</asp:tablerow>
</asp:table>
<asp:label id="MyLabel" runat="server"/>
</form>
</body>
</html>
Validation summary in javascript dialog (C#)
<%@ Page language="c#" src="ValidationSummary.aspx.cs" AutoEventWireup="false" Inherits="MyOwnValidationSummary" %>
<HTML>
<body>
<form id="Form1" method="post" runat="server">
<P>A number (1 to 10):
<asp:textbox id="txtValidated" runat="server"></asp:textbox>
<asp:rangevalidator id="RangeValidator" runat="server" Display="None" Type="Integer" MinimumValue="1" MaximumValue="10" ControlToValidate="txtValidated" ErrorMessage="The First Number Is Not In The Range"></asp:rangevalidator><BR>
<BR>
Not validated:
<asp:textbox id="txtNotValidated" runat="server"></asp:textbox><BR>
</P>
<P><BR>
<asp:button id="cmdOK" runat="server" Text="OK"></asp:button><BR>
<BR>
<asp:label id="lblMessage" runat="server" EnableViewState="False"></asp:label><BR>
<BR>
<asp:ValidationSummary id="ValidationSummary1" runat="server" ShowMessageBox="True"></asp:ValidationSummary></P>
</form>
</body>
</HTML>
<%--
using System;
using System.Collections;
using System.ruponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
public class MyOwnValidationSummary : System.Web.UI.Page
{
protected System.Web.UI.WebControls.TextBox txtValidated;
protected System.Web.UI.WebControls.RangeValidator RangeValidator;
protected System.Web.UI.WebControls.TextBox txtNotValidated;
protected System.Web.UI.WebControls.Button cmdOK;
protected System.Web.UI.WebControls.Label lblMessage;
protected System.Web.UI.WebControls.ValidationSummary ValidationSummary1;
private void Page_Load(object sender, System.EventArgs e)
{
// Put user code to initialize the page here
}
#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: This call is required by the ASP.NET Web Form Designer.
//
InitializeComponent();
base.OnInit(e);
}
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.cmdOK.Click += new System.EventHandler(this.cmdOK_Click);
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
private void cmdOK_Click(object sender, System.EventArgs e)
{
if (!this.IsValid) return;
lblMessage.Text = "cmdOK_Click event handler executed.";
}
}
--%>