我有一个图像控件和按钮控件。我想通过单击按钮来选择新图像和更新图像。cs文件如下:
protected void Button2_Click(object sender, EventArgs e)
{
//choose new image
}
你可以在你的 .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>
在您的 .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;
}
}
}
尝试这个。
protected void Button2_Click(object sender, EventArgs e)
{
image1.ImageUrl = "~/FolderName/yourimage.jpg";
}