0
如果輸入的登錄電子郵件地址(用戶名)已存在於數據庫中,我寫了自定義驗證以防止用戶將信息保存在數據庫中。自定義驗證無法正常工作
<div class="span3">
<asp:TextBox ID="txtLoginEmailAddress" class="SearchTxtBox" runat="server" Text='<%# ds.Tables[0].Rows[0]["LoginEmailAddress"] %>'
MaxLength="50"> </asp:TextBox>
</div>
<div class="span6">
<asp:RequiredFieldValidator ID="valLoginEmailAddressRequired" runat="server" CssClass="Validator"
ValidationGroup="save" Display="Dynamic" ControlToValidate="txtLoginEmailAddress"
ErrorMessage="<b>User Name is required</b>"></asp:RequiredFieldValidator>
<asp:CustomValidator ID="CheckAvailablity" runat="server" ValidationGroup="save" ControlToValidate="txtLoginEmailAddress"
OnServerValidate="ValidateUserName"></asp:CustomValidator>
<asp:Label ID="valDuplicatePassword" runat="server" style="color:red" Visible="False"></asp:Label></td>
</div>
<asp:Button ID="btnSave" runat="server" Text="Save" CssClass="btn btn-small " OnClick="btnSave_Click"
ValidationGroup="save" />
在CS文件
protected void ValidateUserName(object sender, ServerValidateEventArgs e)
{
SystemUserBL bl = new SystemUserBL(SessionContext.SystemUser);
ds = new DataSet();
bl.FetchForLoginEmailAddress(ds, txtLoginEmailAddress.Text);
if (ds.Tables[0].Rows.Count > 0)
{
e.IsValid = false;
valDuplicatePassword.Visible = true;
valDuplicatePassword.Text = "<b>This User Name is already in use by another user.</b>";
btnSave.Enabled = false;
btnSaveclose.Enabled = false;
}
else
{
e.IsValid = true;
btnSave.Enabled = true;
btnSaveclose.Enabled = true;
valDuplicatePassword.Visible = true;
valDuplicatePassword.Text = "<b>Congratulations! " + txtLoginEmailAddress.Text + " is available.</b>";
}
}
protected void btnSave_Click(object sender, EventArgs e)
{
try
{
if (save())
{
SucessMessage();
}
}
catch (Exception ee)
{
ErrorMessage(ee.Message);
}
}
現在,當我們點擊保存按鈕將其保存在數據庫中後,會顯示確認消息。我希望它不應該保存在數據庫中,並且自定義驗證應該像其他ASP.NET的驗證一樣工作。請幫忙 !!!
你如何保存的東西數據庫?它發生在btnSave_Click裏面嗎? – Andrei
沒有...在btnsave點擊功能,只有保存功能writtem – Nida
請發佈相關代碼 – Andrei