Simple servier side validation and javascript alert

Experts,

I have a server side validation function that validates the duplicacy of textbox contents against database on textchanged event. I have keep this textbox in an updatepanel  control to avoid page refresh. I want a pop alert or message the moment validation fails on the textbox. Please tell me how to do that and any code sample will be a big help.
LVL 1
dotnetproAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
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.

suganthkumarCommented:
hi,

At the server side, once your validation is over, you can use the below code to put a clientside script to show a message.
ClientScript.RegisterStartupScript(this.GetType(), "someName", "alert('some message3333');", true);

Open in new window

0
suganthkumarCommented:
Hi,

in the case of AJAX Update panel, you can use the "ScriptManager" object instead of "ClientScript"

0
dotnetproAuthor Commented:
This is what i did . Nothing happened

   


 protected void CheckDuplicateServices(object sender, EventArgs e)
    {
        if (dbq.CheckServices(txtnewservice.Text))
        {
          ClientScript.RegisterStartupScript(Page.GetType(), "msg", "alert('Service name already exists');",true);
        }
       

Open in new window

0
Become a CompTIA Certified Healthcare IT Tech

This course will help prep you to earn the CompTIA Healthcare IT Technician certification showing that you have the knowledge and skills needed to succeed in installing, managing, and troubleshooting IT systems in medical and clinical settings.

suganthkumarCommented:
Hi,

if you are using AJAX and UpdatePanel, then ClientScript object has no effect. instead, you need to use the "ScriptManager" object.



protected void CheckDuplicateServices(object sender, EventArgs e)
    {
        if (dbq.CheckServices(txtnewservice.Text))
        {
          ScriptManager.RegisterStartupScript(this, Page.GetType(), "msg", "alert('Service name already exists');",true);
        }
       

Open in new window

0
dotnetproAuthor Commented:
Yaar, i have plugged this line of code nearly every where....but this thing is just not working.
I have plugged it in textchanged event of a textbox..nothing happened..because that is where i initially wanted to check the duplicacy...then i tried sticking it in btnsubmit_Click() event and there also it apparently did not work...so there has to be some thing of a size of blunder that i am doing because of which it is not happening...i know you are correct , i am the only one making this mistake..can you please correct me ???
0
suganthkumarCommented:
Hi,

I have attached a sample work that i have done inside the ajax panel. please refer to it.

Regards
webform.zip
0
dotnetproAuthor Commented:
Its doing what its supposed to do...thanks for the nice sample...but the problem i have is that a user fills a textbox and i have to make a database call to verify whether that text already exists in the db ....If it does then pop up a message that the text already exists without a postback....This could either happen on the onblur event and the focus remains on the textbox text deleted from it or onclick event of submit button and textbox gets the focus with text deleted.....that is killing me...i am not able to do that..
0
suganthkumarCommented:
In the same example, you can modify the code to add an event handler for the TextChanged Event, even though the TextChanged event happens when you loose the focus to the text box, you can restore back the focus from the server side.

Also Enable the Autopostback to true for the TextBox so that the textchanged event really generates  a postback which is handled through AJAX call
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
C#

From novice to tech pro — start learning today.