2011-08-16 67 views
1

我想添加一个提醒框,以便用户可以选择是或否或确定并取消,但它不能正常工作。我必须做一个数据库检查,这是在C彻底完成而不是只需将该功能链接到按钮点击的事件即可。这是我第一次尝试这样做。我正在使用visual studio 2010.我不确定我的代码是否正确。任何人都可以请引导我,如果我错了。Javascript提醒问题

  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>"); 
      } 

这是我完全C#代码:

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"); 
      } 
     } 
+0

你错了,这个代码是毫无意义的。你的目标究竟是什么?如果用户单击“取消”按钮,您试图防​​止什么? –

+0

您确认后是否尝试过分号? – ibram

+0

您正在向页面写入函数,但触发函数的位置在哪里?还有其他的方式,我认为这应该完成,但考虑到你的例子,好像你希望onsub方法在页面重新加载时触发,在这种情况下,你需要将它附加到文件onload eveent。 –

回答

1

你的代码是完全正确的。只有语法错误。只需删除“\”,然后在行中双引号开始 - > Response.Write(“return confirm(\”Are you sure?\“)”);
替换为这一行
Response.Write(“return confirm(”Are you sure?\“)”);

0

我看不到你在哪里调用函数。为什么不让函数总是在页面上动态添加函数调用到后面代码中的按钮?

喜欢的东西:

button.Attributes.Add("onclick", "onsub();"); 
+0

@ giovanni galbo ....我在页面加载时调用它:protected void Page_Load(object sender,EventArgs e) { AlertWithConfirmation(); } – mikespiteri

+0

这不是调用JavaScript函数......它调用编写javascript函数的代码。 –

+0

对不起,我不确定。这是JavaScript的功能? private void AlertWithConfirmation(){Response.Write(“”); } – mikespiteri