我的网络应用程序出现问题,即使验证错误,也会触发按钮点击事件。
这是我的设计页面:
<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;
}
}
}
请建议我找到解决方案
答案 0 :(得分:7)
您还必须从false
返回OnClientClick
:
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;
}