Call javascript function on asp.net validator fail

I have a required field validator in an Asp.net c# web application.  Everytime the validator fails, I need to call a JS function.

thanks! Mike
cmushaunAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

remorinaCommented:
Hi cmushaun,

Try this http://forums.asp.net/p/1398068/3098878.aspx#3098878 and let me know if it helps

Cheers
0
cmushaunAuthor Commented:
i'm trying to use the code below but I this doesn't call the alert('hi') until the second time I click the button and the page is marked invalid.  Any ideas?

<script language="javascript" type="text/javascript">
function fnShow() {

if ( Page_IsValid == false ) {
alert('hi');

}

}
</script>


<asp:Button ID="Button1" runat="server" onclick="Button1_Click" OnClientClick="javascript:fnShow();" >
0
Gagan_JauraCommented:
You can do a smart work here. You can call the javascript function and from that function you initiate validators on you page. Below is the samle code
<head runat="server">
    <script language="javascript" type="text/javascript">
     function btnClick()
        {
        if (Page_ClientValidate() == true)            
                alert('Validation Pass');
             else
                alert('Validation Fails');
        }
    </script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:TextBox ID="txtName" runat="server"></asp:TextBox>
        <asp:RequiredFieldValidator ID="RequiredFieldValidator1"
          runat="server" ErrorMessage="Please enter your name"
          ControlToValidate="txtName">Please enter your name
         </asp:RequiredFieldValidator>
        <input id="btnSubmit" type="button" value="Submit"
           onclick="btnClick();" /></div>
    </form>
</body>
</html>

Here if the validator fails, the Page_ClientValidate() function will return false and then you can write your javascript code in the else condition of btnClick() function.

0
The Ultimate Tool Kit for Technolgy Solution Provi

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy for valuable how-to assets including sample agreements, checklists, flowcharts, and more!

cmushaunAuthor Commented:
Gagan_Jaura, Doesn't this way of calling the javascript remove calling of the code_behind method I'm calling:"Button1_Click"?
0
Gagan_JauraCommented:
It will definately call "Button1_Click" event because the flow of the page is maintained. If you simply use validator (without using javascript) then also internally Page_ClientValidate() function is called when the event is fired and then "Button1_Click".
If at all in the worst case, the system do not fire "Button1_Click" event then you can put __doPostBack('Button1_Click','') after alert('Validation Pass');
0
cmushaunAuthor Commented:
I tried this, but I don't think this method will work because after the initial required field validation, I have other validation that occurs in the code-behind. I think actually the javascript method needs to be called after the button1_click is called and the page posts back.   Is this possible?
0
Gagan_JauraCommented:
I created a sample and added few validations at server side. And they are working fine. The first solution given by me is working good for me. I have made only one change in my code. I have changed the below code
<input id="btnSubmit" type="button" value="Submit" onclick="btnClick();" />
with
<asp:Button id="btnSubmit" Text="Submit" onClientClick="btnClick" onclick="btnSubmit_Click" />
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
ASP.NET

From novice to tech pro — start learning today.