![](/img/trans.png)
[英]ASP.NET/JavaScript - Why Isn't “Return False” Not Preventing Postback?
[英]ASP.NET/JavaScript - “Return False” Not Preventing Postback
我的Web應用程序中存在問題,即使驗證為假,也會觸發按鈕單擊事件。
在這里我的設計頁面:
<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;
}
}
}
請建議我得到一個解決方案
您還必須從OnClientClick
返回false
:
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;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.