如何在c中回发后将镜像路径绑定到镜像上传控件
本文关键字:镜像 绑定 路径 控件 | 更新日期: 2023-09-27 18:28:10
我正在尝试将该图像路径绑定到图像上传控件(页面刷新后),该控件保存在会话和视图状态中,但缺少某些内容,无法处理该内容。。。存储了正确路径但未绑定的会话和视图状态。只需要解决这个问题。。。
正在获取异常"无法将类型为"System.String"的对象强制转换为类型为"System.Web.UI.WebControls.FileUpload".
fuUploadLogo是我的图像上传控件,倒数第二行无法将会话中的路径绑定到fileupload控件。
asp:FileUpload ID="fuUploadLogo"runat="server"
if (Session["PicturePath1"] == null)
{
Session["PicturePath1"] = ViewState["PicturePath"].ToString();
}
else if (Session["PicturePath1"] != null)
{
fuUploadLogo = (FileUpload)Session["PicturePath1"];
}
我认为您应该添加图像控件来查看您的图像,例如:
在aspx中,添加
<asp:Image ID="Image1" runat="server" />
在aspx.cs 中
Image1.ImageUrl=Convert.ToString(ViewState["PicturePath"]);
请像下面的一样尝试
<asp:FileUpload ID="fileupload1" runat="server"></asp:FileUpload>
//If first time page is submitted and we have file in FileUpload control but not in session
// Store the values to SEssion Object
if (Session["PicturePath"] == null && FileUpload1.HasFile)
{
Session["PicturePath"] = FileUpload1.PostedFile.FileName;
}
// Next time submit and Session has values but FileUpload is Blank
// Return the values from session to FileUpload
else if (Session["PicturePath"] != null && (! FileUpload1.HasFile))
{
FileUpload1 = (FileUpload) Session["PicturePath"];
}
// Now there could be another sictution when Session has File but user want to change the file
// In this case we have to change the file in session object
else if (FileUpload1.HasFile)
{
Session["PicturePath"] = FileUpload1.PostedFile.FileName;
}