存储在事件处理程序中的变量中的值以后为null,为什么?

时间:2011-06-08 19:42:54

标签: c# drop-down-menu

在我的代码中,我将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;
        }
    }

4 个答案:

答案 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 StateSession 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;
        }
    }