【发布时间】:2018-01-11 02:32:11
【问题描述】:
我有一个 Web 表单页面,但我遇到了问题。
当我在点击保存之前测试它时。一切都按预期工作。但是,一旦我清除了 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;
}
}
【问题讨论】:
标签: javascript c# asp.net