Click here to Skip to main content
15,891,033 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I have two web forms one WebForm1.aspx and second WebForm2.aspx.

On WebForm1 i put FileUpload Control to get image from the system and a Button control to transfer to WebForm2 page.
Now i want to show this image in Image Control, which is on WebForm2 when i click the button on WebForm1. So how can i achive this.
Posted

When you click Button in WebForm1, save that image either in File System or Database.
Then redirect to the second page that is WebForm2.

In WebForm2 Page Load, retrieve image from File or Database and bind that to a asp Image Control.
 
Share this answer
 
Comments
JR009 26-Dec-13 1:59am    
I tried this but its not works.

Here is my Code :

WebForm1.aspx

<body>
<form id="form1" runat="server">
<div>
<asp:FileUpload ID="FileUpload1" runat="server" />
<br />
<asp:Button ID="Button1" runat="server" Text="Button" onclick="Button1_Click"/>
</div>
</form>
</body>

WebForm1.aspx.cs

protected void Button1_Click(object sender, EventArgs e)
{
string fileName = System.IO.Path.GetFileName(FileUpload1.PostedFile.FileName);
FileUpload1.PostedFile.SaveAs(Server.MapPath("~/images/" + fileName));
Session["image"] = "~/images/"+ fileName;
Response.Redirect("WebForm2.aspx");
}

WebForm2.aspx

<body>
<form id="form1" runat="server">
<div>
<asp:Image ID="img1" runat="server" />
</div>
</form>
</body>

WebForm2.aspx.cs

protected void Page_Load(object sender, EventArgs e)
{
if (Session["image"] != null)
{
img1.ImageUrl = Session["image"].ToString();
}
}
Debug and check the value of Session in WebForm2 Page load. What value you get?
As Tadit mentioned, decide how to save the image , File system or DB.

Then use Querystring to pass imagepath or( imageid in case saved in database) in the Navigation ( Response.Redirect) and in Webform2 retrieve the querystring value . imagepath in case saved in File System or inserted imageid if in db to display in Image control.
 
Share this answer
 
Comments
Waqas Nowsherwan 16-Jun-15 3:09am    
how to just display the image in imagebox before saving it to the database
I have solved this myself, thank you guys for your support.
Here is my Code :

WebForm1.aspx.cs
C#
protected void Button1_Click(object sender, EventArgs e)
        {
            
            string fileName = System.IO.Path.GetFileName(FileUpload1.PostedFile.FileName);
            FileUpload1.PostedFile.SaveAs(Server.MapPath("~/images/" + fileName));
            Session["image"] = fileName;
            Response.Redirect("WebForm2.aspx");
        }

WebForm2.aspx.cs
C#
protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["image"] != null)
            {
                string fileName = Session["image"].ToString();
                img1.ImageUrl = "~/images/" + fileName;
            }
        }
 
Share this answer
 
v2
Comments
Good work, you should also accept my answer as I gave you some idea, right? :P :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900