2

I have a problem in my web application, even the validation is false the button click event will be fired.

here my design page:

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

here my validation function:

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

and my code behind

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

please suggest me to get a solution

1 Answer 1

7

You also have to return false from 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;
}  
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.