Related Posts Plugin for WordPress, Blogger...

About

Follow Us

Thursday, 12 February 2015

When we use file upload control to upload files file name does not retain after post back (if occurs before submitting the data). Post back may occur due to dropdown or checkbox or by other controls.
In this article i am going to explain with example How to keep/maintain image in the file upload control on occurring of post back event in asp.net using both the C# and VB.Net language.


Description: One of the common annoying problem that developer face while creating Asp.net application is to maintain the image on PostBack i.e. When we browse and select an image in FileUpload control and PostBack occurs before final submission then FileUpload control loses its value and we have to again select the image.
We can use Session or view-state to hold the name of the Image between page post-back. In this article I have used session to maintain filename.

Implementation: Let's create an example to understand better.
  • In the <BODY> tag of the design page (.aspx) Place a FileUpload Control, a Label control and a CheckBoxList Control as shown : 
  •  
Source Code:

<fieldset style="width:400px">
            <legend>Retain Image name in FileUpload control on PostBack</legend>
            <asp:FileUpload ID="FileUpload1" runat="server" />
            <asp:Label ID="lblImage" runat="server" ForeColor="#FF3300"></asp:Label>
        <asp:CheckBoxList ID="chkCity" runat="server" AutoPostBack="true" RepeatColumns="2">
            <asp:ListItem>Delhi</asp:ListItem>
            <asp:ListItem>Chandigarh</asp:ListItem>
            <asp:ListItem>Ambala</asp:ListItem>
            <asp:ListItem>Panchkula</asp:ListItem>
        </asp:CheckBoxList> 
        </fieldset>

Note:  AutoPostBack property of CheckBoxList control is set to true to cause Postback.


C#.NET Code to maintain/retain image on PostBack in FileUpload control in asp.net
  • In the code behind file(.aspx.cs) write the code on page load event as:
protected void Page_Load(object sender, EventArgs e)
        {
            //Case: 1 When the page is submitted for the first time(First PostBack) and there is file 
            // in FileUpload control but session is Null then Store the values to Session Object as:
            if (Session["FileUpload1"] == null && FileUpload1.HasFile)
            {
                Session["FileUpload1"] = FileUpload1;
                lblImageName.Text = FileUpload1.FileName;
            }
            // Case 2: On Next PostBack Session has value but FileUpload control is
            // Blank due to PostBack then return the values from session to FileUpload as:
            else if (Session["FileUpload1"] != null && (!FileUpload1.HasFile))
            {
                FileUpload1 = (FileUpload)Session["FileUpload1"];
                lblImageName.Text = FileUpload1.FileName;
            }
            // Case 3: When there is value in Session but user want to change the file then
            // In this case we need to change the file in session object also as:
            else if (FileUpload1.HasFile)
            {
                Session["FileUpload1"] = FileUpload1;
                lblImageName.Text = FileUpload1.FileName;
            }
        }


VB.NET Code to maintain/retain image on PostBack in FileUpload control in asp.net
  • In the code behind file(.aspx.vb) write the code on page load event as: 
Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
        'Case: 1 When the page is submitted for the first time(First PostBack) and there is file
        '  in FileUpload control but session is Null then Store the values to Session Object as:
        If Session("FileUpload1") Is Nothing AndAlso FileUpload1.HasFile Then
            Session("FileUpload1") = FileUpload1
            lblImageName.Text = FileUpload1.FileName
            ' Case 2: On Next PostBack Session has value but FileUpload control is
            ' Blank due to PostBack then return the values from session to FileUpload as:
        ElseIf Session("FileUpload1") IsNot Nothing AndAlso (Not FileUpload1.HasFile) Then
            FileUpload1 = DirectCast(Session("FileUpload1"), FileUpload)
            lblImageName.Text = FileUpload1.FileName
            ' Case 3: When there is value in Session but user want to change the file then
            ' In this case we need to change the file in session object also as:
        ElseIf FileUpload1.HasFile Then
            Session("FileUpload1") = FileUpload1
            lblImageName.Text = FileUpload1.FileName
        End If

    End Sub
Categories: ,

0 comments:

Post a Comment