Click here to Skip to main content
15,891,204 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I am using .NET Framework 1.1 and I need C# code to display a success message-box via a Javascript Alert box when the Save button is clicked. I do not want the Page to redirect to another page on clicking the button. And the page should not refresh.

More Info: I am using a DropdownList and a Button. After making a selection in the DropdownList, I save that value to a table when the Button is clicked. I already have validation checks for the DropdownList when the button is clicked.

I have the the following code at the end of my button click event handler.

Response.Write("<script>alert('Record Added Successfully');</script>");


When I run this code, the page refreshes whenever I click the button and the alert message pops up.

How do I resolve this problem?
Posted
Updated 21-May-10 2:38am
v5
Comments
bbirajdar 27-Aug-12 9:49am    
You want to execute the server code without posting back the page to the server ? Its time to revisit the concepts of client-server programming ...
zinebh 30-May-14 5:49am    
MERCI

Every time you write that code, it will run, that seems obvious. So, write the code only when the save button is pushed ( in the click event ). If that's not obvious to you, you should stop what you're doing ( which smells like real work ) and learn the absolute basics of programming, the stuff we in the west learned in the first week of the four year course we took before we started asking people to pay for our efforts.

Oh, I think I get it now. You save on a button click that does other things, too ? Well, that doesn't seem like great UI to me, but, in that case, you should in general be checking if the value was changed before saving it, and then only show the message if that was the case. It still sounds to me like what is happening is perfectly logical, and you need to break down the steps of your design to see when you want the message, when you do not, and how to write code to tell the difference. It's also bad to be saving when you don't need to, because unless it's a low use site, you should be more frugal with resources like database access.

One other thing - I doubt it applies here, but everyone is having a hard time decoding your question, so I throw it out there for what it's worth. You can use the OnClientClick method to run javascript BEFORE the postback. So you can use that to show a message box before a postback, or return false to stop the postback occuring.
 
Share this answer
 
v4
Comments
bbirajdar 27-Aug-12 9:48am    
+5.. You caught us right.. I read the question over and over but could not understand what he wants. The question title and the content tell a totally opposite story..
You can show a string message on the Button WebControl's click without refreshing the page by setting the Button's Attributes property accordingly on the form_Load event.
By Setting this property it will add javascript code at button click..
HTML
btn.Attributes.Add("onclick", "return confirm('" & strMessage & "');")
 
Share this answer
 
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900