psj01
psj01

Reputation: 3245

OnTextChanged function not firing

I have a web forms page which I am having an issue with.

When I test it before hitting save. Everything works as expected. But once I clear the text inside the lblEstNo textbox, I get my error message saying that its required. So I type 123 (or anything) in to the text box the OnTextChanged event doesn't fire after i click away from that text box?

why is this behavior so? Is it possible to fix this?

Thanks.

below is my code.

<%@ 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>

code behind:

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;
    }
}

Upvotes: 1

Views: 311

Answers (1)

psj01
psj01

Reputation: 3245

Found the problem....

changed the onClientClick function to this :

<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" />

and it worked.

Found this out after reading this.

Upvotes: 2

Related Questions