在我的代码中,我将DropDownList1.SelectedItem.Text
中的值写入Label1.Text
并将uploadFolder
方法写入DropDownList1_SelectedIndexChanged
。调用ASPxUploadControl1_FileUploadComplete
方法时,值为Label1.Text
但不在uploadFolder
中,为空。这是为什么?
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
if (DropDownList1.SelectedItem != null)
{
Label1.Text = "You selected " + DropDownList1.SelectedItem.Text;
uploadFolder = DropDownList1.SelectedItem.Text;
}
}
protected void ASPxUploadControl1_FileUploadComplete(object sender, DevExpress.Web.ASPxUploadControl.FileUploadCompleteEventArgs e)
{
if (e.IsValid)
{
string uploadDirectory = Server.MapPath("~/files/");
//string uploadDirectory = @"\\DOCSD9F1\TECHDOCS\";
string fileName = e.UploadedFile.FileName;
string path = (uploadDirectory + uploadFolder + "/" + fileName);
//string path = Path.Combine(Path.Combine(uploadDirectory, uploadFolder), fileName);
e.UploadedFile.SaveAs(path);
e.CallbackData = fileName;
}
}
答案 0 :(得分:2)
看起来uploadFolder
是您在页面上声明的变量,如下所示:
public class MyPage : System.Web.UI.Page
{
string uploadFile = null;
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
// Your code here
}
protected void ASPxUploadControl1_FileUploadComplete(object sender, DevExpress.Web.ASPxUploadControl.FileUploadCompleteEventArgs e)
{
// Your code here
}
}
正在发生的事情是,您在uploadFile
中设置的DropDownList1_SelectedIndexChanged
内容未在回发之间保留,因为它不是其中一个控件的属性页。您需要将值存储在持久化的某个位置,例如View State或Session State。
要执行此操作,您应该添加到DropDownList1_SelectedIndexChanged
方法,使其类似于:
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
if (DropDownList1.SelectedItem != null)
{
Label1.Text = "You selected " + DropDownList1.SelectedItem.Text;
Session["UploadFolder] = DropoDownList1.SelectedItem.Text;
}
}
调整ASPxUploadControl1_FileUploadComplete
方法,使其从Session中提取`uploadFolder:
string path = (uploadDirectory + Session["UploadFolder"] + "/" + fileName);
如果你想让它看起来更优雅,可以考虑以这种方式使用ViewState:
public string UploadFolder
{
get
{
return (string)ViewState["UploadFolder"];
}
set
{
ViewState["UploadFolder"] = value;
}
}
然后你可以这样做:
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
if (DropDownList1.SelectedItem != null)
{
Label1.Text = "You selected " + DropDownList1.SelectedItem.Text;
UploadFolder = DropoDownList1.SelectedItem.Text;
}
}
和
string path = (uploadDirectory + UploadFolder + "/" + fileName);
答案 1 :(得分:1)
我会想象你没有坚持uploadFolder
通过网页回帖。将值存储在隐藏字段中,例如:
<asp:HiddenField ID="hidden_UploadFolder" runat="server" />
然后:
hidden_UploadFolder.Value = DropDownList1.SelectedItem.Text;
然后您可以在下一篇文章中再次阅读:
string uploadFolder = hidden_UploadFolder.Value;
确保添加错误捕获。
答案 2 :(得分:1)
看起来您在一个回发中设置了upload文件夹的值,并在另一个回发中使用它。如果要在回发之间保留数据,请使用Session。
离。
Session["uploadFolder"] = DropDownList1.SelectedItem.Text;
string path = (uploadDirectory + Session["uploadFolder"].ToString() + "/" + fileName);
答案 3 :(得分:0)
尝试
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
if (DropDownList1.SelectedItem != null)
{
Label1.Text = "You selected " + DropDownList1.SelectedItem.Text;
Session["uploadFolder"] = DropDownList1.SelectedItem.Text;
}
}
protected void ASPxUploadControl1_FileUploadComplete(object sender, DevExpress.Web.ASPxUploadControl.FileUploadCompleteEventArgs e)
{
if (e.IsValid)
{
string uploadDirectory = Server.MapPath("~/files/");
//string uploadDirectory = @"\\DOCSD9F1\TECHDOCS\";
string fileName = e.UploadedFile.FileName;
string uploadfolder = Session["uploadFolder"] as String;
string path = (uploadDirectory + uploadfolder + "/" + fileName);
//string path = Path.Combine(Path.Combine(uploadDirectory, uploadFolder), fileName);
e.UploadedFile.SaveAs(path);
e.CallbackData = fileName;
}
}