1

I have a image control and button control.I want to choose new image and update image with button click. cs file like below:

protected void Button2_Click(object sender, EventArgs e)
        {
//choose new image 
        }
1
  • I want to do it dynamically Commented Aug 7, 2013 at 14:51

2 Answers 2

1

You could try something like this on your .ASPX:

<form id="form1" runat="server">
    <asp:Image ID="Image1" runat="server" /><br /><br />
    <asp:FileUpload id="FileUploadControl" runat="server" />
    <asp:Button runat="server" id="UploadButton" text="Upload" onclick="UploadButton_Click" />
    <br /><br />
    <asp:Label runat="server" id="StatusLabel" text="Upload status: " />
</form>

And in your .CS:

protected void UploadButton_Click(object sender, EventArgs e)
{
    if(FileUploadControl.HasFile)
    {
        try
        {
            string filename = Path.GetFileName(FileUploadControl.FileName);
            FileUploadControl.SaveAs(Server.MapPath("~/YourImageFolder") + filename);
            StatusLabel.Text = "Upload status: File uploaded!";
            Image1.ImageUrl = "~/YourImageFolder" + filename;
        }
        catch(Exception ex)
        {
            StatusLabel.Text = "Upload status: The file could not be uploaded. The following error occured: " + ex.Message;
        }
    }
}
Sign up to request clarification or add additional context in comments.

Comments

0

Try This.

protected void Button2_Click(object sender, EventArgs e) 
{ 
  image1.ImageUrl = "~/FolderName/yourimage.jpg";
}

4 Comments

I want to do it dynamically
Give some more details what is your requirement.
I want using dynamically FileUpload
then u need to save that image in folder then map it to image control.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.