2014-11-05 50 views
2

我在我的web应用程序中遇到问题,即使验证为false,按钮单击事件也会被触发。ASP.NET/JavaScript - “返回False”不能防止回传

这里我的设计页面:

<asp:TextBox ID="txtAddJournal" runat="server" /> 
<asp:Button ID="btnUpload" runat="server" Text="Upload" OnClientClick="Validate();" OnClick="btnUpload_click"/> 

这里我的验证功能:

function Validate() { 
    var Journal = document.getElementById('<%= txtAddJournal.ClientID %>').value.trim(); 
    // alert(Journal); 
    if (Journal == "" || Journal == null) { 
     alert("Enter the Journal name"); 
     return false; 
    } 
} 

背后

protected void btnUpload_click(object sender, EventArgs e) 
{ 
    check.Value = "1"; 
    Page.ClientScript.RegisterStartupScript(this.GetType(), "CallMyFunction", "Load_functions()", true);   

    //txtAddJournal.Attributes.Add("Style", "display:block"); 
    //btnUpload.Attributes.Add("Style", "display:block"); 
    if (fileuploader.HasFile) 
    { 
     try 
     { 
      string Filename = Path.GetFileName(fileuploader.FileName); 
      //fileuploader.SaveAs(Server.MapPath("~/") + Filename); 
      // fileuploader.SaveAs(Server.MapPath("D:\\Req Sep16\\") + Filename); 
      OleDbConnection myconnectionini = default(OleDbConnection); 
      OleDbDataAdapter mycommandini = default(OleDbDataAdapter); 
      if (fileuploader.PostedFile.FileName.EndsWith(".xls") == false & fileuploader.PostedFile.FileName.EndsWith(".xlsx") == false) 
      { 
       // lbl_Error.Text = "Upload only excel format"; 
       Response.Write(@"<script language='javascript'>alert('Upload only excel format');</script>"); 
       return; 
      } 
      else 
      { 
       gvDetails.DataSource = null; 

       string pathToSave = HttpContext.Current.Server.MapPath("~/UploadFiles/") + fileuploader.FileName; 
       fileuploader.PostedFile.SaveAs(pathToSave); 
       //strFilePath = "D:\\Files\\" + fileuploader.FileName; 

       string constrini = "provider=Microsoft.Jet.OLEDB.4.0;data source=" + pathToSave + ";Extended Properties=Excel 8.0;"; 
       DataSet ds = new DataSet(); 
       // DataTable dt = new DataTable();   
       myconnectionini = new OleDbConnection(constrini); 
       mycommandini = new OleDbDataAdapter("select * from [Sheet1$]", myconnectionini); 
       ds = new DataSet(); 
       mycommandini.Fill(ds); 

       gvDetails.DataSource = ds.Tables[0]; 
       gvDetails.DataBind(); 
      } 
     } 
     catch (Exception ex) 
     { 
      string msg = ex.Message; 
     } 
    } 
} 

我的代码,请建议我得到一个解决方案

回答

7

你也是已经退货falseOnClientClick

OnClientClick="return Validate();" 

function Validate() { 
    var Journal = document.getElementById('<%= txtAddJournal.ClientID %>').value.trim(); 
    // alert(Journal); 
    if (Journal == "" || Journal == null) { 
     alert("Enter the Journal name"); 
     return false; 
    } 
    return true; 
} 
+0

非常感谢... – Bala 2014-11-05 12:15:37