在我的应用程序中我使用一个Fileupload控制器,一个下拉列表和一个按钮,这里首先我使用fileupload控制器选择一个.doc文件,然后我选择Dropdown值,当我点击按钮时,它检查下拉值是的> 0 与否,
if (ddlstype.SelectedValue != "0")
如果ddlstype值等于0,则它在标签中显示错误消息。 下拉列表中包含AutoPostBack,代码如下,
<asp:DropDownList ID="ddlstype" runat="server" AutoPostBack="true" OnSelectedIndexChanged="ddlstype_SelectedIndexChanged" Width="165px">
这里我的问题是如果页面是AutoPostBack,那么文件上传控件变为null,如何在AutoPostBack中将文件保存在fileupload控制器中?
答案 0 :(得分:0)
此处在页面加载时将文件路径存储在会话中。 这样您就不需要使用更新面板
protected void Page_Load(object sender, EventArgs e)
{
if (this.IsPostBack)
{
if (Session["FileUpload1"] == null && Request.Files["FileUpload1"].ContentLength > 0)
{
Session["FileUpload1"] = Request.Files["FileUpload1"];
ImageErrorLabel.Text = Path.GetFileName(Request.Files["FileUpload1"].FileName);
HttpPostedFile file = (HttpPostedFile)Session["FileUpload1"];
}
else if (Session["FileUpload1"] != null && (Request.Files["FileUpload1"].ContentLength == 0))
{
HttpPostedFile file = (HttpPostedFile)Session["FileUpload1"];
ImageErrorLabel.Text = Path.GetFileName(file.FileName);
}
else if (Request.Files["FileUpload1"].ContentLength > 0)
{
Session["FileUpload1"] = Request.Files["FileUpload1"];
ImageErrorLabel.Text = Path.GetFileName(Request.Files["FileUpload1"].FileName);
}
}
}
并在ddlstype_SelectedIndexChanged
protected void ddlstype_SelectedIndexChanged(object sender, EventArgs e)
{
HttpPostedFile FileUpload1 = Request.Files["FileUpload1"];
Session["FileUpload1"] = FileUpload1;
}