ASP.NET - button click event not fireing - Asked By Dilip Sharma on 15-Nov-11 08:25 AM

hi in my asp.net page i m using file upload control to upload file...

but when i am selecting any file  and.. clicking on save button....
then my button click event not firing...

if i am not selecting any file in control then clicking on save...

then event is firing...

i not able to understand where is the  problem....

plse Help me.....
Reena Jain replied to Dilip Sharma on 15-Nov-11 08:46 AM
Hi,

To achieve this you can use RequiredFieldValidator with FileUpload control.Refer the below example.
<%@ Page Language="C#" %>
  
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  
<script runat="server">
  
</script>
  
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
  <title>Untitled Page</title>
</head>
<body>
  <form id="form1" runat="server">
  <div>
  <asp:FileUpload ID="fu" runat="server" />
  <asp:RequiredFieldValidator ID="rfv" runat="server" ErrorMessage="File not selected!!!" ControlToValidate="fu"></asp:RequiredFieldValidator>  
  <br />
  <asp:Button ID="btn" runat="server" Text="OK" />
  </div>
  </form>
</body>
</html>
Jitendra Faye replied to Dilip Sharma on 15-Nov-11 08:47 AM
Have you implemented Click event of Button.

like this-

<asp:Button ID="BtnLogin" runat="server" Text="Login" onclick="Login_Click" />



protected void Login_Click(object sender, EventArgs e)

{

//Your code
}

Try this and let me know.
Dilip Sharma replied to Reena Jain on 15-Nov-11 08:48 AM
i m checking it in javascript...
And there is not neccesery that user will select file....

Problem is....

when i am selecting any file then  Button_Click event not working....

Dilip Sharma replied to Jitendra Faye on 15-Nov-11 08:49 AM
Yes....

if i m not selecting file... then its working....
Dilip Sharma replied to Dilip Sharma on 15-Nov-11 09:32 AM
this is my aspx page <table>
  
   <tr>
 <td>Upload Document 1</td>
 <td>
   <asp:FileUpload ID="FileUpload1" runat="server" />
   </td>
  <td>
     <asp:LinkButton ID="LinkButton2" runat="server" onclick="LinkButton2_Click"></asp:LinkButton>
   </td>
  </tr>
  
  
 <tr>
 <td>Upload Image </td>
 <td>
  <asp:FileUpload ID="File_UP_Img" runat="server" />
   </td>
   <td>
     <asp:LinkButton ID="LinkButton1" runat="server" onclick="LinkButton1_Click"></asp:LinkButton>
   </td>
  </tr>
   
 </table>
  
   
   
<table>
<tr>
<td>
  <asp:Button ID="Btn_Save" runat="server" Text="Save" onclick="Btn_Save_Click"
    OnClientClick="return validatesave()"  />
</td>
</tr>
 
</table> THis is my cs page
protected void Btn_Save_Click(object sender, EventArgs e)
  {
    try
    {
 
      if (Btn_Save.Text == "Update")
      {
        bool dis_sts;
        if (Chk_Dispos.Checked)
        {
 
          dis_sts = true;
          ProductProvider.Update_Fixed_Asset(ID, dis_sts, Convert.ToDateTime(Txt_Dis_Dt.Text).Date, (Convert.ToDouble((Txt_DisposValue.Text == "") ? "0" : Txt_DisposValue.Text)));
          ProductProvider.Insert_JournalEntry(Convert.ToDateTime(Txt_Dis_Dt.Text).Date, "DISP", Convert.ToInt32(ddlAccrunalNominalAC.Items.FindByText("Asset Disposal").Value), ddlLocations.SelectedValue, "Fixed Asset", Convert.ToInt32("0"), (Convert.ToDouble((Txt_DisposValue.Text == "") ? "0" : Txt_DisposValue.Text)), Convert.ToDouble("0"), 0, "Null", Txt_Ref.Text, DateTime.Now, "none", DateTime.Now, 0);
 
        }
        else
        {
          dis_sts = false;
        }
      }
      else
      {
          //    Upload Image
 
        string tempfile = null;
        string fullname = null;
        if (File_UP_Img.HasFile)
        {
          tempfile = Path.GetFileName(File_UP_Img.FileName);
          File_UP_Img.SaveAs(Server.MapPath("~/Upload_Asset/") + tempfile);
 
          fullname = DateTime.Now.Year.ToString() + DateTime.Now.Month.ToString() + DateTime.Now.Day.ToString() + "_" + Txt_Ref.Text + "_" + tempfile;
          File.Move(Server.MapPath("~/Upload_Asset/") + tempfile, Server.MapPath("~/Upload_Asset/") + fullname);
          // FileUploadControl.SaveAs(Server.MapPath("~/") + filename);
 
        }
 
        //    Upload Document1
 
        string tempfile1 = null;
        string fullname1 = null;
        if (FileUpload1.HasFile)
        {
          tempfile1 = Path.GetFileName(FileUpload1.FileName);
          FileUpload1.SaveAs(Server.MapPath("~/Upload_Asset/") + tempfile1);
 
          fullname1 = DateTime.Now.Year.ToString() + DateTime.Now.Month.ToString() + DateTime.Now.Day.ToString() + "_" + Txt_Ref.Text + "_" + tempfile1;
          File.Move(Server.MapPath("~/Upload_Asset/") + tempfile1, Server.MapPath("~/Upload_Asset/") + fullname1);
          // FileUploadControl.SaveAs(Server.MapPath("~/") + filename);
 
        }
 
 
 
        ////    Upload Document2
 
        string tempfile2 = null;
        string fullname2 = null;
          
 
        DateTime Date_Pu = Convert.ToDateTime(txtDate_pur.Text).Date;
        DateTime Date_capt = Convert.ToDateTime(Txt_DTCP.Text).Date;
        DateTime Date_exp = Convert.ToDateTime(Txt_Expiry_dt.Text).Date;
        DateTime Date_dis = Convert.ToDateTime(Txt_Dis_Dt.Text).Date;
        double deplif = 0;
        double deprat = 0;
        bool dis_sts;
        string meth = null;
 
        if (Chk_Dispos.Checked)
        {
          dis_sts = true;
          ProductProvider.Insert_JournalEntry(Convert.ToDateTime(Txt_Dis_Dt.Text).Date, "DISP", Convert.ToInt32(ddlAccrunalNominalAC.Items.FindByText("Asset Disposal").Value), ddlLocations.SelectedValue, "Fixed Asset", Convert.ToInt32("0"), (Convert.ToDouble((Txt_DisposValue.Text == "") ? "0" : Txt_DisposValue.Text)), Convert.ToDouble("0"), 0, "Null", Txt_Ref.Text, DateTime.Now, "none", DateTime.Now, 0);
 
        }
        else
        {
          dis_sts = false;
        }
 
        if (Drp_method.SelectedIndex == 1)
        {
          deplif = Convert.ToDouble((Txt_methdLife.Text == "") ? "0" : Txt_methdLife.Text);
          meth = "STR";
 
          double depamount = 0;
 
          depamount = (Convert.ToDouble((Txt_PurCost.Text == "") ? "0" : Txt_PurCost.Text)) / (Convert.ToDouble((Txt_methdLife.Text == "") ? "0" : Txt_methdLife.Text));
 
 
          double life1 = Convert.ToDouble((Txt_methdLife.Text == "") ? "0" : Txt_methdLife.Text);
 
          int life = Convert.ToInt32(life1 * 12);
          DateTime datejournal = Convert.ToDateTime(Txt_DTCP.Text).Date; ;
 
 
 
 
 
          for (int i = 0; i < life; i++)
          {
            datejournal = Date_capt.AddMonths(i);
            ProductProvider.Insert_JournalEntry(datejournal, "FIASS", Convert.ToInt32(ddlNominalAccount.SelectedValue), ddlLocations.SelectedValue, "Fixed Asset", Convert.ToInt32("0"), Convert.ToDouble("0"), depamount, 0, "Null", Txt_Ref.Text, DateTime.Now, "none", DateTime.Now, 0);
            ProductProvider.Insert_JournalEntry(datejournal, "FIASS", Convert.ToInt32(ddlAccrunalNominalAC.SelectedValue), ddlLocations.SelectedValue, "Fixed Asset", Convert.ToInt32("0"), depamount, Convert.ToDouble("0"), 0, "Null", Txt_Ref.Text, DateTime.Now, "none", DateTime.Now, 0);
 
          }
 
 
 
 
 
        }
        else if (Drp_method.SelectedIndex == 2)
        {
 
          deprat = Convert.ToDouble((Txt_methodrate.Text == "") ? "0" : Txt_methodrate.Text);
          meth = "RED";
        }
 
        ProductProvider.Insert_Fixed_Asset(Txt_Ref.Text, Txt_Desc.Text, Convert.ToInt32(ddlLocations.SelectedValue), Convert.ToInt32(ddlNominalAccount.SelectedValue), Convert.ToInt32(ddlAccrunalNominalAC.SelectedValue)
          , Date_Pu, Date_capt, Convert.ToDouble(Txt_PurCost.Text), Convert.ToDouble(Txt_Qunty.Text), Date_exp, meth, deplif, deprat, dis_sts, Date_dis, 0, 0, (Convert.ToDouble((Txt_DisposValue.Text == "") ? "0" : Txt_DisposValue.Text)),fullname,fullname1,fullname2);
 
 
 
 
      }
 
      Response.Redirect("ShowAsset.aspx");
 
 
 
    }
    catch (Exception ex)
    {
      ScriptManager.RegisterStartupScript(this, this.GetType(), "temp", "<script language='javascript'>alert('" + ex.Message + "');</script>", false);
    }
}
Santhosh N replied to Dilip Sharma on 15-Nov-11 12:30 PM
I think you are modifying the text of submit button and depending on the text of it to decide to save in click event.. check that once
Kirtan Patel replied to Dilip Sharma on 15-Nov-11 01:13 PM
protected void Button1_Click(object sender, EventArgs e)
{
  if (FileUpload1.HasFile)
  {
    if (FileUpload1.FileName.Contains("'") == true)
    {
      FileUpload1.SaveAs("SomeOtherName.txt");
    }
    else
    {
      FileUpload1.SaveAs(FileUpload1.FileName);
    }
  }
}
 
alvin alvin replied to Kirtan Patel on 14-Aug-13 02:26 AM
Sir we have the same problem..did you resolve this?if yes how please tell me.
alvin alvin replied to Kirtan Patel on 14-Aug-13 02:26 AM
Sir we have the same problem..did you resolve this?if yes how please tell me.