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
}
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
}
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;
}
}
}
Try This.
protected void Button2_Click(object sender, EventArgs e)
{
image1.ImageUrl = "~/FolderName/yourimage.jpg";
}