我目前正在使用telerik Radcontrols ASP.NET Ajax,我想为我的页面创建客户端验证。我创建了一个usercontrol,并按照我在telerik网站上找到的this教程。不幸的是它不起作用,我不知道我做错了什么。有人可以看看我的代码,并指出我正确的方向吗?
<%@ Control Language="C#" AutoEventWireup="false" CodeBehind="ManageAccountControl.ascx.cs" Inherits="Docl.WebDoc.Controls.Account.ManageAccountControl" %>
<style type="text/css">
.btn {
margin-top: 15px;
}
</style>
<div id="CreateUser" style="margin: 10px 10px 10px 10px">
<telerik:RadScriptManager ID="RadScriptManager1" runat="server"></telerik:RadScriptManager>
<telerik:RadSkinManager ID="QsfSkinManager" runat="server" ShowChooser="true" />
<telerik:RadFormDecorator ID="QsfFromDecorator" runat="server" DecoratedControls="All" EnableRoundedCorners="false" />
<telerik:RadAjaxPanel ID="rapPanel" runat="server" LoadingPanelID="LoadingPanel1">
<div onclick="Validate()">
<table class="adminProps">
<tr>
<td>Email</td>
<td>
<telerik:RadTextBox ID="ctrlEmail" runat="server">
</telerik:RadTextBox></td>
<td>
<asp:Panel ID="pnlEmail" runat="server" Width="25px">
<asp:RegularExpressionValidator Display="Dynamic" ID="regexEmail"
ValidationExpression="^[a-zA-Z][\w\.-]*[a-zA-Z0-9]@[a-zA-Z0-9][\w\.-]*[a-zA-Z0-9]\.[a-zA-Z][a-zA-Z\.]*[a-zA-Z]$"
runat="server" ControlToValidate="ctrlEmail" ErrorMessage="A valid Email is required!" EnableClientScript="true">*</asp:RegularExpressionValidator>
<asp:RequiredFieldValidator ID="rfvEmail" runat="server" ValidationGroup="group1" ControlToValidate="ctrlEmail" ErrorMessage="Email is required" Display="Dynamic">*</asp:RequiredFieldValidator>
</asp:Panel>
</td>
</tr>
<tr>
<td>Voornaam</td>
<td>
<telerik:RadTextBox ID="ctrlFirstName" runat="server"></telerik:RadTextBox></td>
<td>
<asp:Panel ID="pnlFirstName" runat="server" Width="25px">
<asp:RequiredFieldValidator ID="rfvFirstName" runat="server" ValidationGroup="group1" ControlToValidate="ctrlFirstName" ErrorMessage="FirstName is required" Display="Dynamic" EnableClientScript="true">*</asp:RequiredFieldValidator>
</asp:Panel>
</td>
</tr>
<tr>
<td>Achternaam</td>
<td>
<telerik:RadTextBox ID="ctrlLastName" runat="server"></telerik:RadTextBox></td>
<td>
<asp:Panel ID="pnLastName" runat="server" Width="25px">
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" ValidationGroup="group1" runat="server" ControlToValidate="ctrlLastName" ErrorMessage="LastName is required" Display="Dynamic" EnableClientScript="true">*</asp:RequiredFieldValidator>
</asp:Panel>
</td>
</tr>
<tr>
<td colspan="2">
<telerik:RadButton ID="ctrlCreate" runat="server" Text="Voeg toe"></telerik:RadButton>
</td>
</tr>
<tr>
<td colspan="3">
<asp:Label ID="lblRed" Style="color: red" runat="server" Width="159px"></asp:Label>
<asp:Label ID="lblGreen" Style="color: green" runat="server" Width="159px"></asp:Label></td>
</tr>
<tr>
<td></td>
<td colspan="2">
<asp:Panel ID="Panel3" runat="server">
<asp:ValidationSummary ID="ValidationSummary" runat="server" Width="174px" BorderColor="Red"
BorderWidth="1px" HeaderText="List of errors" EnableClientScript="true" DisplayMode="BulletList" ValidationGroup="group1"></asp:ValidationSummary>
</asp:Panel>
</td>
</tr>
</table>
</telerik:RadAjaxPanel>
</div>
<telerik:RadAjaxLoadingPanel ID="LoadingPanel1" runat="server" MinDisplayTime="300">
</telerik:RadAjaxLoadingPanel>
</div>
<script type="text/javascript">
function Validate() {
Page_ClientValidate('group1');
}
</script>
代码背后:
public partial class ManageAccountControl : System.Web.UI.UserControl
{
RootRepos _repos;
protected override void OnInit(EventArgs e)
{
InitializeComponent();
base.OnInit(e);
}
protected void Page_Load(object sender, EventArgs e)
{
_repos = new RootRepos();
InitCtrls();
foreach (BaseValidator validator in Page.Validators)
{
validator.EnableClientScript = true;
}
}
private void InitializeComponent()
{
this.ctrlCreate.Click += new System.EventHandler(ctrlCreate_Click);
this.Load += new System.EventHandler(Page_Load);
}
private void InitCtrls()
{
lblRed.Text = string.Empty;
lblGreen.Text = string.Empty;
}
protected void ctrlCreate_Click(object sender, EventArgs e)
{
if (Page.IsValid)
{
lblRed.Text = "Name Registered: ";
lblGreen.Text = ctrlLastName.Text + " " + ctrlFirstName.Text;
}
else
{
lblRed.Text = "Account not registered! Validation error.";
}
}
private bool CheckIfUserExists()
{
if (_repos.AccountRepos.GetSingle(ctrlEmail.Text) != null)
return true;
else
return false;
}
private Data.Account CreateAccount(Guid partyid)
{
Data.Account account = new Data.Account();
account.PartyId = partyid;
account.Name = ctrlEmail.Text;
return account;
}
private Data.Party CreateParty()
{
Data.Party party = new Data.Party();
party.PartyId = Guid.NewGuid();
party.LastName = ctrlLastName.Text;
party.Name = ctrlFirstName.Text + " " + ctrlLastName.Text;
party.FirstName = ctrlFirstName.Text;
return party;
}
}
谢谢!