存储在事件处理程序中的变量中的值稍后为null,原因是什么

本文关键字:null 是什么 事件处理 程序 变量 存储 | 更新日期: 2023-09-27 17:58:37

在我的代码中,我用DropDownList1_SelectedIndexChanged方法将值从DropDownList1.SelectedItem.Text写入Label1.Text并写入uploadFolder。当调用ASPxUploadControl1_FileUploadComplete方法时,值在Label1.Text中,但不在uploadFolder中,后者为null。为什么会这样?

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;
        }
    }

存储在事件处理程序中的变量中的值稍后为null,原因是什么

看起来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
    }
}

发生的情况是,您在DropDownList1_SelectedIndexChanged中设置的uploadFile的内容不会在两次回发之间保留,因为它不是页面上某个控件的属性。您需要将值存储在持久化的某个位置,例如"视图状态"或"会话状态"。

要做到这一点,您应该添加到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方法,使其从会话中提取`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);

我想您不会通过页面回发来持久化uploadFolder。将值存储在隐藏字段中,例如:

<asp:HiddenField ID="hidden_UploadFolder" runat="server" />

然后:

hidden_UploadFolder.Value = DropDownList1.SelectedItem.Text;

然后你可以在下一篇文章中再次阅读:

string uploadFolder = hidden_UploadFolder.Value;

请确保添加错误补漏白。

看起来您正在一个回发中设置上传文件夹的值,并在另一个回寄中使用它。如果要在回发之间保持数据,请使用会话。

前任。

Session["uploadFolder"] = DropDownList1.SelectedItem.Text;
string path = (uploadDirectory + Session["uploadFolder"].ToString() + "/" + fileName);

尝试

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;
        }
    }