ViewState不适用于AjaxFileUpload

时间:2017-10-26 07:11:49

标签: asp.net ajaxcontroltoolkit

我将上传文件的名称存储在AjaxFileUpload UploadComplete函数中的ViewState中,但在发生回发时无法在Page_Load函数中检索该名称。 ViewState为null。 当我使用ViewState在一个简单的按钮单击函数中存储一些变量时,在回发后的Page_Load中可以使用这些值。

问题在于AjaxFileUpload UploadComplete事件。

 protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        btnParseDmp.Enabled = false;
        btnParseDmp.CssClass = "btnParseDmpDisable";
        lblTxtFile.Text = "";
        lblWindbgTxtFile.Text = "";
        btnTxt.Visible = false;
        btnWindbgTxt.Visible = false;
        lblOpt.Text = "";
        lblStatus.Text = "";

        fileListBox.Items.Clear();
        m_fileName = "";
        latestDir = null;
        bParseClicked = false;
        dirName = "";
        FileName = "";
        latestParsedFile = "";
        fileListBox.Visible = false;
        Response.Clear();

        ViewState["m_fileName"] = null;
        ViewState["latestDir"] = null;
        ViewState["bParseClicked"] = false;
        ViewState["dirName"] = null;
        ViewState["latestParsedFile"] = null;
        ViewState["FileName"] = null;
    }
    else
    {

        if (ViewState["FileName"] != null)
        {
            Console.WriteLine("Not null");
        }
        else
            Console.WriteLine("Null");

        m_fileName = (string)ViewState["m_fileName"];
        latestDir = (DirectoryInfo)ViewState["latestDir"];
        bParseClicked = (bool)ViewState["bParseClicked"];
        dirName = (string)ViewState["dirName"];
        FileName = (string)ViewState["FileName"];
        latestParsedFile = (string)ViewState["latestParsedFile"];
    }
}

protected void AjaxFileUpload1_UploadComplete(object sender, AjaxControlToolkit.AjaxFileUploadEventArgs e)
{
    Random rnd = new Random(DateTime.Now.Millisecond);
    int num = rnd.Next(1, 2147483640);
    dirName = System.Web.HttpRuntime.AppDomainAppPath + "Parsings\\" + num;
    ViewState["dirName"] = dirName;
    System.IO.Directory.CreateDirectory(dirName);

    FileName = e.FileName;
    ViewState["FileName"] = FileName;
    FileInfo fz = new FileInfo(FileName);
    string ext = fz.Extension;
    ext = ext.ToLower();
    if (ext.Contains("zip"))
    {

        AjaxFileUpload1.SaveAs(System.Web.HttpRuntime.AppDomainAppPath + "Temp" + "\\" + e.FileName);
    }
    else
    {
        AjaxFileUpload1.SaveAs(dirName + "\\" + e.FileName);
    }
    bParseClicked = false;
    ViewState["bParseClicked"] = bParseClicked;
}

1 个答案:

答案 0 :(得分:1)

AjaxFileUpload的工作方式,您将无法获得视图状态。