1

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.I have to do a database check which is done in c sharp not just link that function to a button clicked event. 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.

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

This is my full C# Code:

    protected void Import_Click(object sender, EventArgs e)
    {
        if (!Validation.ValidateDateFormat(dateField.Text))
        {
            errorMessageLabel.Text = "Invalid Date Format, for example: 1/1/2011 should be 01/01/2011";
        }
        else
        {
            //Validation to check if data is already imported
            if (BusinessLayerHandler.isImported(dateField.Text) == false)
            {
                try
                {
                    if (BusinessLayerHandler.isInProgress(dateField.Text)== true)
                    {
                        AlertWithConfirmation();
                    }
                }
                catch
                {
                    //catch error
                }
            }
            else if (BusinessLayerHandler.isImported(dateField.Text) == true)
            {
                Alert("That date was already imported");
            }
        }
mikespiteri
  • 901
  • 4
  • 12
  • 24

2 Answers2

1

your code is absolutely correct. only syntax error. just remove "\" before starting the double quotes in the line-> Response.Write("return confirm(\"Are you sure?\")");
replace with this line
Response.Write("return confirm("Are you sure?\")");

sush
  • 466
  • 1
  • 7
  • 20
0

I don't see where you're calling the function. Why not have the function always on the page and dynamically add the function call to the button in the code behind?

Something like:

button.Attributes.Add("onclick", "onsub();");
Giovanni Galbo
  • 12,831
  • 13
  • 57
  • 78
  • @ giovanni galbo.... i am calling it on page load:protected void Page_Load(object sender, EventArgs e) { AlertWithConfirmation(); } – mikespiteri Aug 16 '11 at 12:19
  • That's not calling the javascript function... it's calling the code that writes a javascript function. – Giovanni Galbo Aug 16 '11 at 12:24
  • Sorry I'm not sure. is this the javascript function? private void AlertWithConfirmation() { Response.Write(""); } – mikespiteri Aug 16 '11 at 12:26
  • That's the function, but you need javascript code to call the function. – Giovanni Galbo Aug 16 '11 at 14:28