• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 510
  • Last Modified:

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.
0
dotnetpro
Asked:
dotnetpro
  • 5
  • 3
1 Solution
 
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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

  • 5
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now