我有一个网页表单,我遇到了问题。
当我在点击保存之前测试它。一切都按预期工作。但是一旦我清除了lblEstNo文本框中的文本,我就会收到错误消息,说明它是必需的。 所以我在文本框中键入123(或任何东西),单击远离该文本框后OnTextChanged事件不会触发?
为什么会出现这种情况?有可能解决这个问题吗?
感谢。
下面是我的代码。
<%@ Page Title="a test page" Language="C#" MasterPageFile="~/MasterPage.master" AutoEventWireup="true" CodeFile="testPage3.aspx.cs" Inherits="app_Member_testPage3" %>
<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolderHead" Runat="Server">
<script>
function lblCustSampleIDValidation() {
return true;
}
</script>
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolderBody" Runat="Server">
<asp:Label runat="server" Text="select"></asp:Label>
<asp:DropDownList ID="ddlSampleForm" DataTextField="Name" DataValueField="SampleFormID" runat="server">
<asp:ListItem Value="ING">ING</asp:ListItem>
<asp:ListItem Value="INGP">INGP</asp:ListItem>
</asp:DropDownList>
<asp:Label runat="server" Text="est no."></asp:Label>
<asp:TextBox ID="lblEstNo" runat="server" Text="" BackColor="PaleGoldenrod" AutoPostBack="True" OnTextChanged="validateEstNo"></asp:TextBox>
<asp:RequiredFieldValidator ControlToValidate="lblEstNo" runat="server" ErrorMessage="est no can't be blank" Display="None" />
<asp:Button ID="savesubmit" runat="server" OnClick="savesubmit_onClick" Text="Save"
OnClientClick="if (typeof(Page_ClientValidate) == 'function') {var x = Page_ClientValidate(''); if(!x) return false;} return lblCustSampleIDValidation();"/>
<asp:Label runat="server" ID="msg"></asp:Label>
<asp:validationsummary ID="Validationsummary1" runat="server" displaymode="List" showmessagebox="true" showsummary="false" />
</asp:Content>
代码背后的代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class app_Member_testPage3 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void validateEstNo(object sender, EventArgs e)
{
var txtBox = (TextBox)sender;
var item = txtBox.NamingContainer;
var sampleFormDDL = (DropDownList)item.FindControl("ddlSampleForm");
var selectedSampleForm = sampleFormDDL.SelectedItem.Text;
if (selectedSampleForm == "ING")
{
if (txtBox.Text == "123")
{
txtBox.Text = "NA";
txtBox.Focus();
ScriptManager.RegisterStartupScript(this, typeof(string), "ALERT", "alert('Thats an invalid Est No for the selected sample form.');", true);
}
}
}
protected void savesubmit_onClick(object sender, EventArgs e)
{
msg.Text = "done." + lblEstNo.Text;
}
}
答案 0 :(得分:2)
发现问题......
将onClientClick函数更改为:
<asp:Button ID="savesubmit" runat="server" OnClick="savesubmit_onClick"
OnClientClick="if (typeof(Page_ClientValidate) == 'function')
{var x = Page_ClientValidate(''); if(!x) { Page_BlockSubmit = false; return false;} } return lblCustSampleIDValidation();"
Text="Save" style="height:30px" />
它有效。
在阅读this后发现了这一点。