3338_SON
3338_SON

Reputation: 35

how to bind image path to image upload control after postback in c#

i'm trying to bind that image path to image upload control (after page refresh) which was saved in session as well as in viewstate but something is missing and not able to handle that thing...session and viewstate having correct path stored but not getting bind. only this thing need to solve...

Getting exception of Unable to cast object of type 'System.String' to type 'System.Web.UI.WebControls.FileUpload'.

fuUploadLogo is my image upload control and second last line is not able to bind the path which is in session to fileupload control.

asp:FileUpload ID="fuUploadLogo" runat="server"

        if (Session["PicturePath1"] == null)
        {
            Session["PicturePath1"] = ViewState["PicturePath"].ToString();
        }
        else if (Session["PicturePath1"] != null)
        {
            fuUploadLogo = (FileUpload)Session["PicturePath1"];                
        }

Upvotes: 0

Views: 2107

Answers (2)

Sagar Hirapara
Sagar Hirapara

Reputation: 1697

Please try like below

<asp:FileUpload ID="fileupload1" runat="server"></asp:FileUpload>

         //If first time page is submitted and we have file in FileUpload control but not in session 
        // Store the values to SEssion Object 
        if (Session["PicturePath"] == null && FileUpload1.HasFile)  
        { 
            Session["PicturePath"] = FileUpload1.PostedFile.FileName; 
        } 
        // Next time submit and Session has values but FileUpload is Blank 
        // Return the values from session to FileUpload 
        else if (Session["PicturePath"] != null && (! FileUpload1.HasFile)) 
        { 
            FileUpload1 = (FileUpload) Session["PicturePath"]; 

        } 
        // Now there could be another sictution when Session has File but user want to change the file 
        // In this case we have to change the file in session object 
        else if (FileUpload1.HasFile) 
        { 
            Session["PicturePath"] = FileUpload1.PostedFile.FileName; 

        }

Upvotes: 0

Neeraj Sharma
Neeraj Sharma

Reputation: 588

I think you should need to add image control to view your image,ex:

In aspx, add

 <asp:Image ID="Image1" runat="server" />

And in aspx.cs

Image1.ImageUrl=Convert.ToString(ViewState["PicturePath"]);

Upvotes: 1

Related Questions