我需要能够上传图片,然后让我上传的图片替换当前的背景div。这必须从.ascx文件中完成。以下是我当前与WebUserControl.ascx相关的所有相关代码:
<div class="box">
<img src="EnergyOilGas-ProductionEnhanced-Oil_Natural-Gas-Recovery_flow_diagram3.jpg" id="myimage" alt="image" height="800px" width="1700px" style="position: absolute;"/>
<div>
<asp:FileUpload ID="FileUpload1" runat="server" />
</div>
<asp:Button ID="Button2" runat="server" onclick="Button2_Click" Text="Upload" />
我正在尝试更改元素的.css:
.box
{
border: 1px solid blue;
margin-bottom: 8px;
width: 1700px;
height: 800px;
float: none;
clear: both;
background-image: url('EnergyOilGas-ProductionEnhanced-Oil_Natural-Gas-Recovery_flow_diagram3.jpg');
}
我的代码(WebUserControl.ascx.cs):
protected void Button2_Click(object sender, EventArgs e)
{
if (FileUpload1.PostedFile != null)
{
string fileExt =
System.IO.Path.GetExtension(FileUpload1.FileName);
if (fileExt == ".jpeg" || fileExt == ".jpg")
{
string FileName = Path.GetFileName(FileUpload1.PostedFile.FileName);
string conn4 = "Host=172.21.98.153; Database=UAT_smartcomm; UserName=iDacAdmin; Password=iDacAdmin2008; port=3306; allow zero datetime = yes";
string upload = "INSERT INTO ScadaImages (FileName, fileExt) VALUES (" + FileName + ", " + fileExt + ")";
DataBase.DataBaseIO.Save(conn4, upload);
string defaultImg = "EnergyOilGas-ProductionEnhanced-Oil_Natural-Gas-Recovery_flow_diagram3.jpg";
string uploadedImg = "" + FileName + "" + fileExt + "";
Image.DisabledCssClass.Replace(defaultImg, uploadedImg);
}
}
}
想法是将用户上传的任何图像设置为新的背景图像,替换当前图像。我知道背景图像元素存储在.box中,除了代码隐藏方法之外,我不确定这是否还需要一个函数。我已经坚持了几天。如果有人可以帮助我,我会非常感激。