4

I am trying to add an alert box so that the user can choose either Yes or No or Ok and Cancel, but it is not working correctly. It is my first time I am trying to do this. I am using visual studio 2010. I am not sure if my code is correct. Can anyone please guide me if I am mistaken.

This is my code:

private void AlertWithOption()
  {
     Response.Write("<script language='javascript'>");
     Response.Write("function onsub() ");
     Response.Write("{ ");
     Response.Write("var where_to= confirm: (\"Are You sure?\")");
     Response.Write("if (where_to== true)");
     Response.Write("{ ");
     Response.Write("return true");
     Response.Write(" }");
     Response.Write("else ");
     Response.Write("{");
     Response.Write("return false;");
     Response.Write(" } ");
     Response.Write("} ");
     Response.Write("</script>");
   }
Shawn Chin
  • 84,080
  • 19
  • 162
  • 191
mikespiteri
  • 901
  • 4
  • 13
  • 25

6 Answers6

1

First I would try changing this:

Response.Write("var where_to= confirm: (\"Are You sure?\")");

to this:

Response.Write("var where_to= confirm(\"Are You sure?\")");

(The method is confirm() and not confirm:())

karim79
  • 339,989
  • 67
  • 413
  • 406
1

As already mentioned, you had an extra colon in your code. However, there is no need for the lengthy if/then logic, as the confirm javascript method returns a boolean. Your code can be condensed to 3 lines:

Response.Write("<script>");
Response.Write("function onsub() { return confirm(\"are you sure?\"); }");
Response.Write("</script>");
Jamiec
  • 133,658
  • 13
  • 134
  • 193
  • I have to do a database check which is done in c sharp not just link that function to a button clicked event. thanks – mikespiteri Aug 16 '11 at 11:40
1

modify this line of code-> Response.Write("var where_to= confirm: (\"Are You sure?\")");
try this->Response.Write("var where_to= confirm ("Are You sure?")");

sush
  • 476
  • 1
  • 7
  • 20
1

method confirm() is correct but the string inside should be within double quotes that is confirm("Are You Sure?");

sush
  • 476
  • 1
  • 7
  • 20
1

Use confirm() function.

private void AlertWithOption()
  {
    Response.Write("<script language='javascript'>");
    Response.Write("function onsub() ");
    Response.Write("{ ");
    Response.Write("return confirm(\"Are you sure?\")");
    Response.Write("} ");
    Response.Write("</script>");
}

I think you are doing mistake in the syntax of the confirm()

Sangeet Menon
  • 9,555
  • 8
  • 40
  • 58
0

I dont think you want the colon after the confirm, you could do it like this

Response.Write("<script language='javascript'>");
Response.Write("function onsub() ");
Response.Write("{ ");
Response.Write("return confirm(\"Are you sure?\")");
Response.Write("} ");
Response.Write("</script>");
saj
  • 4,626
  • 2
  • 26
  • 25