Solved

Java Script "Confirm" in .net

Posted on 2007-12-06
6
747 Views
Last Modified: 2013-11-26
HI,
I am using java script confirm in my .net application to show the message box with yes/no options.
but even if i select the cancel option in message box,it is deleting the value.

using c#/.net
Thank you

private void Delete_Click(object sender, System.EventArgs e)
{
SqlConnection connection = new SqlConnection("Server=Local;DataBase=mktt; uid=sa;pwd=commt");
string strDelete="DELETE from produ where pid='"TextBox1.Text+ "'";
SqlCommand command = new SqlCommand(strDelete,connection);
connection.Open();
command.ExecuteNonQuery();
Response.Write("<script>if (confirm('Permanently Deletes . Do You Want to Continue?')){window.location = \"main.aspx\"};//--></script>");
}

Open in new window

0
Comment
Question by:tag_k
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
6 Comments
 
LVL 29

Expert Comment

by:Göran Andersson
ID: 20421372
Yes, of course it is. First you delete the record, then you output the code that will show the confirmation message.

Put the confirmation message in the client side click event of the button, so that it's executed before the event is sent to the server, not after the event has been sent to the server and the new page has been sent back to the browser:

<asp:Button id="Delete" runat="server" Text="Delete" OnClick="Delete_Click" OnClientClick="return confirm('Permanently Deletes . Do You Want to Continue?');" />

When the user clicks the button, the confirm message is shown. If the user chooses not to continue, the event is not sent to the server. If the user chooses to continue, the event is sent to the server, and you can delete the record.

If you want to go the "main.aspx" page after deleting, use a redirect last in the server event handler:

Response.Redirect("main.aspx");
0
 
LVL 26

Expert Comment

by:Anurag Thakur
ID: 20436153
if you want to set the message box from the server side then yoy can add the following code in the page load event

if (IsPostBack == false)
   btnDelete.OnClientClick = "return confirm ('ermanently Deletes . Do You Want to Continue?');";
0
 

Author Comment

by:tag_k
ID: 20437595
Hi,
i have tried both the solutions but i have same error

 'System.Web.UI.WebControls.Button' does not contain a definition for 'OnClientClick'

any way to resolve this.
thanks
0
Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

 
LVL 29

Expert Comment

by:Göran Andersson
ID: 20437722
Oh, you are using framework 1.1. Missed that.

Then there is no OnClientClick property, so you have to use the attribute collection:

btnDelete.Attributes.Add("onclick", "return confirm('Permanently Deletes . Do You Want to Continue?');");

(As the server side event OnClick has the same name as the client side event onclick, you can't add it in the markup code.)
0
 

Author Comment

by:tag_k
ID: 20439587
Thanks ,but i also want to redirect it to another page after(i.e after delete) user clicks ok.
0
 
LVL 29

Accepted Solution

by:
Göran Andersson earned 500 total points
ID: 20439959
Then you use a Response.Redirect in the method, after you have closed the connection.

I added a using statement to handle the closing of the connection.

I also added encoding of the string value in the SQL query, so that the code isn't open for SQL injections. Note that I assume that you are using SQL Server. If you are using MySQL the encoding process is different.
private void Delete_Click(object sender, System.EventArgs e) {
   using (SqlConnection connection = new SqlConnection("Server=Local;DataBase=mktt;uid=sa;pwd=commt")) {
      string strDelete="DELETE from produ where pid='" + TextBox1.Text.Replace("'", "''") + "'";
      SqlCommand command = new SqlCommand(strDelete,connection);
      connection.Open();
      command.ExecuteNonQuery();
   }
   Response.Redirect("AnotherPage.aspx");
}

Open in new window

0

Featured Post

Technology Partners: 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!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
In this video, viewers are given an introduction to using the Windows 10 Snipping Tool, how to quickly locate it when it's needed and also how make it always available with a single click of a mouse button, by pinning it to the Desktop Task Bar. Int…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…

623 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question