locked
Updating Specific Panel in an ASPX file RRS feed

  • Question

  • User-1773686868 posted

    I am trying to upload some files and send as an email. I have two update panels because when I click the Menu item, it automatically refreshes the whole page and hence no file will be attached. How do I stop it? 

    I want my page to hold the files until I click Button3 then it can perform the function of sending the email. 

    Attached is my code 

    <%@ Page Title="Home Page" Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="EDMSProject._Default" %>
    
    
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head id="Head1" runat="server">
        <title></title>
        <style type="text/css">
            .auto-style2 {
                width: 186px;
            }
        </style>
        </head>
    <body>
    <form id="form1" runat="server">   
        <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
        <br />
            
        <br />
        
        <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Check DocuTray " />
        <asp:ScriptManager ID="ScriptManager1" runat="server" />
        <asp:UpdatePanel ID="UpdatePanel1" runat="server">
           <ContentTemplate>
        <asp:FileUpload ID="FileUpload1" runat="server" AllowMultiple="True" />
        <br />
                </ContentTemplate>
            <Triggers>
            <asp:PostBackTrigger ControlID = "Button3"/>     
        </Triggers>
        </asp:UpdatePanel>
    
             <asp:UpdatePanel ID="UpdatePanel2" runat="server">
           <ContentTemplate>
        
            <asp:Button ID="Button2" runat="server" Text="Check History/Status" />
           
           
                 <asp:Menu ID="Menu1" runat="server" OnMenuItemClick="Menu1_MenuItemClick" Orientation="Horizontal" style="margin-left: 41px">
                     <Items>
                         <asp:MenuItem Text="Action" Value="Action">
                             <asp:MenuItem Text="Forward" Value="Forward"></asp:MenuItem>
                             <asp:MenuItem Text="Save" Value="Save"></asp:MenuItem>
                             <asp:MenuItem Text="Close Ticket" Value="Close Ticket"></asp:MenuItem>
                         </asp:MenuItem>
                     </Items>
                     <StaticItemTemplate>
                         <%# Eval("Text") %>
                     </StaticItemTemplate>
                 </asp:Menu> 
                    
                  <asp:Label ID="Label2" runat="server" Text="Department"></asp:Label>
                  <asp:DropDownList ID="DropDownList1" runat="server" DataTextField="Department" DataValueField="Department" OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged">
                  </asp:DropDownList>
                  <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:EDMSConnectionString %>" SelectCommand="SELECT * FROM [Employee]"></asp:SqlDataSource>
           <br />
                  <asp:Label ID="Label3" runat="server" Text="Client"></asp:Label>
           <asp:DropDownList ID="DropDownList2" runat="server">
               <asp:ListItem>Pavis</asp:ListItem>
               <asp:ListItem>Dove Associates</asp:ListItem>
                  </asp:DropDownList>
            <br />
           <asp:Label ID="Label4" runat="server" Text="Recipient"></asp:Label>
           <asp:DropDownList ID="DropDownList3" runat="server"></asp:DropDownList>
                  <asp:ListBox ID="ListBox1" runat="server" SelectionMode="Multiple"></asp:ListBox>
           <br />
           <asp:Label ID="Label6" runat="server" Text="Subject"></asp:Label>
           <asp:TextBox ID="TextBox2" runat="server" Width="182px"></asp:TextBox>
            <br />
           <asp:Label ID="Label5" runat="server" Text="Message"></asp:Label>
           <asp:TextBox ID="TextBox1" runat="server" Height="109px" Width="265px"></asp:TextBox>
           <br />
           <asp:Button ID="Button3" runat="server" Text="Button" OnClick="Button3_Click" style="margin-left: 260px" />
                 </ContentTemplate> 
                      
               </asp:UpdatePanel> 
          
    </form>     
        </body>
    </html>
    
    
    
    

    Wednesday, March 19, 2014 5:07 AM

Answers

All replies

  • User527778624 posted

    Hi,

    i think, File uploads not works with partial postbacks (update panels), it requires full postback.

    You can try converting your file (stream) into base64 string and save it into custom viewstate.

    that way u can persist data until ur desired event.

    Wednesday, March 19, 2014 7:27 AM
  • User-1773686868 posted

    Hi raju, 

    I do not understand what you mean by converting file stream into base64.

    Wednesday, March 19, 2014 12:31 PM
  • User-417640953 posted

    Hi boyfunky,

    Thank you post the issue to our forum.

    Based on your description, I see you want to maintain the UploadFile when the updatepanel do a partial postback.

    With your code provided, I see you have two updatepanels in your page, when a updatepanel do a partial postback.

    The other one will also be refreshed, that because the UpdatePanel UpdateMode is "Always" defaultly.

    So we should set the UpdatePanel UpdateMode as "Conditional" which contains the UploadFile control like below.

     <asp:ScriptManager ID="ScriptManager1" runat="server"></asp:ScriptManager> 
     <asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Conditional">
                <ContentTemplate>
                      <asp:FileUpload ID="FileUpload1" runat="server" AllowMultiple="True" />
                      <br /> 
                </ContentTemplate>
                <Triggers>
                  <asp:PostBackTrigger ControlID = "Button3"/>     
                </Triggers> 
     </asp:UpdatePanel>
     <asp:UpdatePanel ID="UpdatePanel2" runat="server">
                  <ContentTemplate>
                        <asp:Menu ID="Menu1" runat="server" OnMenuItemClick="Menu1_MenuItemClick" Orientation="Horizontal" style="margin-left: 41px">
                             <Items>
                                 <asp:MenuItem Text="Action" Value="Action">
                                     <asp:MenuItem Text="Forward" Value="Forward"></asp:MenuItem>
                                     <asp:MenuItem Text="Save" Value="Save"></asp:MenuItem>
                                     <asp:MenuItem Text="Close Ticket" Value="Close Ticket"></asp:MenuItem>
                                 </asp:MenuItem>
                             </Items> 
                         </asp:Menu>
                      <asp:Button ID="Button1" runat="server" Text="Button1" />
                      <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label> 
                      <asp:Button ID="Button3" runat="server" Text="Button" OnClick="Button3_Click" style="margin-left: 260px" /> 
                  </ContentTemplate>
            </asp:UpdatePanel>

    code behind:

      protected void Menu1_MenuItemClick(object sender, MenuEventArgs e)
            {
                this.Label1.Text += "1";
            }
    
            protected void Button3_Click(object sender, EventArgs e)
            {
                Response.Write(this.FileUpload1.FileName);
            }

    Hope that helps, thanks.

    Best Regards!

    Thursday, March 20, 2014 3:21 AM
  • User-1773686868 posted

    Hi fuxiang, 

    I tried your method but whenever i clicked on Menu, i have a back end code that tells it to bind a dropdownlist and what happens is that it doesnt bind tht dropdownlist because somehow the updatePanel1 is blocking it from happening. 

    But I also have another problem.

    I want to send the uploaded files as an attachment to an email. 

    My uploaded files are in this directory "C:\Users\user\Documents\DocuTray"

    How do i get the files already selected in FileUpload1 to be added as attachment to send an email?

    if (FileUpload1.HasFiles)
    {
    foreach (HttpPostedFile fileName in FileUpload1.PostedFiles)
    {
    string fileName1 = fileName.FileName.ToString();
    mM.Attachments.Add(new Attachment(fileName1));
    }
    
    }





    Friday, March 21, 2014 1:16 AM
  • User-417640953 posted

    But I also have another problem.

    I want to send the uploaded files as an attachment to an email. 

    Hi,

    Thanks for your back.

    For attaching email a file dirrectly from the FileUpload control, please chec below code.

    private bool SendMail()
     {
     try
     {
     
    /* Create a new blank MailMessage */
     MailMessage mailMessage = new MailMessage();
     
    mailMessage.From = txtTo.Text;
     
    mailMessage.To = "your email";
     
    mailMessage.Subject = txtSubject.Text ;
     
    msgMail.Body = txtMessage.Text ;
     
    /* We use the following variables to keep track of
     attachments and after we can delete them */
     
    string attach1 = null;
     
    /*strFileName has a attachment file name for attachment process. */
     
    string strFileName = null;
     
    if (FileUpload1.PostedFile != null)
     {
     /* Get a reference to PostedFile object */
     HttpPostedFile attFile = FileUpload1.PostedFile;
     
    /* Get size of the file */
     int attachFileLength = attFile.ContentLength;
     
    /* Make sure the size of the file is > 0 */
     if (attachFileLength > 0)
     {
     /* Get the file name */
     strFileName = Path.GetFileName(FileUpload1.PostedFile.FileName);
     
    /* Save the file on the server */
     FileUpload1.PostedFile.SaveAs(Server.MapPath(strFileName));
     
    /* Create the email attachment with the uploaded file */
     MailAttachment attach = new MailAttachment(Server.MapPath(strFileName));
     
    /* Attach the newly created email attachment */
     mailMessage.Attachments.Add(attach);
     
    /* Store the attach filename so we can delete it later */
     attach1 = strFileName;
     }
     
    /* Set the SMTP server and send the email with attachment */
     SmtpMail.SmtpServer = ("your smtp");
     
    SmtpMail.Send(mailMessage);
     
    /* Delete the attachements if any */
     if (attach1 != null)
     {
     File.Delete(Server.MapPath(attach1));
     }
     }
     
    return true;
     }
     catch (Exception ex)
     {
     return false;
     }
     }
     
    protected void btn_Send_Click(object sender, EventArgs e)
     {
     bool TrueOrFalse = SendMail();
     
    if ((TrueOrFalse == true))
     {
     Response.Redirect("~/MailSent.aspx");
     }
     else
     {
     Label1.Text = "Try again";
     }
     }

    There is example for this, please follow below article.

    http://www.codedigest.com/Articles/ASPNET/374_Send_Email_with_Attachment_directly_from_FileUpload_control_in_ASPNet.aspx

    Thanks.

    Best Regards!

     

    Sunday, March 23, 2014 9:51 AM
  • User-1773686868 posted

    Hi, Fuxiang,

    I get this error warning message 

    'System.Web.Mail.SmtpMail' is obsolete: '"The recommended alternative is System.Net.Mail.SmtpClient. http://go.microsoft.com/fwlink/?linkid=14202"' 

    Cant we use System.Net.Mail namespace instead of System.Web.Mail namespace?

    Thanks 

    Kingsley

    Tuesday, March 25, 2014 12:07 PM
  • User-417640953 posted

    Hi boyfunky,

    Thanks for your response.

    Classes in this System.Web.Mailhave been deprecated. Please use the System.Net.Mail namespace instead.

    http://msdn.microsoft.com/en-us/library/System.Web.Mail(v=vs.110).aspx

    The System.Net.Mail namespace contains classes used to send electronic mail to a Simple Mail Transfer Protocol (SMTP) server for delivery.

    For how to use the System.Net.Mail, you can refer to below article.

    http://msdn.microsoft.com/en-us/library/system.net.mail(v=vs.110).aspx

    Hope that helps, thanks.

    Best Regards!

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, March 25, 2014 9:29 PM
  • User-1773686868 posted

    Dear Fuxiang, 

    Are you good with SQL? 

    I have a table Folder of which FolderID is an identity column but I deleted a row and now if i add a new row via C# using my TableAdapter, the row numbers jump. e.g i have FolderID (1,2,3,4,5,8,9...). 

    How Do I reset or ensure that the numbers continue in the right format in the event I delete a row i want it to be (1,2,3,4,5,6) etc. I read places that said i should try reseeding the value but cant find how to do that in Visual Studio 2012. 

    EDMSDataSetTableAdapters.FoldersTableAdapter folderTable = new EDMSDataSetTableAdapters.FoldersTableAdapter();
    
    folderTable.InsertQuery1(clientname, deptvalue);

    This is my insert query to add to my Folders table. 

    Thanks 

    Thursday, March 27, 2014 3:33 PM