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
  • 13
  • 25
  • You're mistaken, this code is pretty meaningless. What is your goal exactly? What are you trying to prevent if user click the Cancel button? – Shadow The GPT Wizard Aug 16 '11 at 12:04
  • Did you tried a semicolon after your confirm? – ibram Aug 16 '11 at 12:05
  • You're writing the function to the page but where are you triggering the function? There are other ways I think this should be done however given your example, it seems like you want the onsub method to fire when the page re-loads, in which case you'll want to attach it to the documents onload eveent. – Jamie Dixon Aug 16 '11 at 12:05
  • Have you tried not to wrap it in a function? – Kristoffer Lundberg Aug 16 '11 at 12:06
  • have you tried calling the function ^_^ – Tom Aug 16 '11 at 12:08
  • @ Jamie Dixon ....I only want the alert box to pop up if the data is being imported. – mikespiteri Aug 16 '11 at 12:14
  • You'll need to do this with an AJAX call, or multiple post backs. Writing to the response isn't going to make the browser execute your javascript. – Rob Aug 16 '11 at 12:16

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
  • 476
  • 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,963
  • 13
  • 59
  • 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