开发者

Value stored in a variable in an event handler is null later on, why?

In my code I write the value from DropDownList1.SelectedItem.Text to Label1.Text and into uploadFolder in the DropDownList1_SelectedIndexChanged method. When the ASPxUploadControl1_FileUploadComplete method is called, the value is in Label1.Text but not in uploadFolder, which is null. Why is this?

protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (DropDownList1.SelectedItem != null)
        {
            Label1.Text = "You selected " + DropDownLis开发者_如何学Pythont1.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;
        }
    }


It looks like uploadFolder is a variable you've declared on your page, something like this:

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

What's happening is that the content of uploadFile that you're setting in DropDownList1_SelectedIndexChanged isn't being preserved between post-backs, because it isn't a property of one of the controls on the page. You need to store the value somewhere that gets persisted, such as the View State or in Session State.

To do this, you should add to the DropDownList1_SelectedIndexChanged method so it reads something like:

protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
    if (DropDownList1.SelectedItem != null)
    {
        Label1.Text = "You selected " + DropDownList1.SelectedItem.Text;
        Session["UploadFolder] = DropoDownList1.SelectedItem.Text;
    }
}

And adjust the ASPxUploadControl1_FileUploadComplete method so it extracts `uploadFolder from the Session:

string path = (uploadDirectory + Session["UploadFolder"] + "/" + fileName);

If you want to make it look more elegant than that, consider using ViewState in this sort of way:

public string UploadFolder
{
    get
    {
        return (string)ViewState["UploadFolder"];
    }
    set
    {
      ViewState["UploadFolder"] = value;
    }
}

You can then do this:

protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
    if (DropDownList1.SelectedItem != null)
    {
        Label1.Text = "You selected " + DropDownList1.SelectedItem.Text;
        UploadFolder = DropoDownList1.SelectedItem.Text;
    }
}

And:

string path = (uploadDirectory + UploadFolder + "/" + fileName);


I would imagine that you are not persisting uploadFolder through page post backs. Store the value in a hidden field, e.g.:

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

And then:

hidden_UploadFolder.Value = DropDownList1.SelectedItem.Text;

You can then read it again on the next post back:

string uploadFolder = hidden_UploadFolder.Value;

Make sure you add error trapping.


It looks like you are setting the value for upload folder in one postback and using it in another. If you want to persist data between postbacks use the Session.

ex.

Session["uploadFolder"] = DropDownList1.SelectedItem.Text;

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


Try

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜