简体   繁体   English

ViewState的奇怪行为

[英]Strange behaviour of ViewState

I have an Asp.Net page (named 'PostAD') which allows user to upload up to 4 pictures. 我有一个Asp.Net页面(名为'PostAD'),允许用户上传最多4张图片。 The file upload button function is as follows: 文件上传按钮功能如下:

protected void btnUpload_Click(object sender, EventArgs e)
{
    if ((ViewState["Img1"] != null) && (ViewState["Img2"] != null) && (ViewState["Img3"] != null) && (ViewState["Img4"] != null))
    {
        lblUploadMsg.Text = "You cannot upload more than 4 pictures";
        return;
    }
    if (FileUpload1.HasFile)
    {
        //FileUpload1.Attributes.Clear();
        string fileExtension = System.IO.Path.GetExtension(FileUpload1.FileName);
        if (fileExtension.ToLower() == ".jpg")
        {
            int fileSize = FileUpload1.PostedFile.ContentLength;

            if (FileUpload1.PostedFile.ContentLength < 2097152)
            {

                //FileUpload1.SaveAs(Server.MapPath("~/Temp/" + FileUpload1.FileName));
                //Response.Write("Successfully Done");

                string sp = Server.MapPath("~/ItemPictures/");
                String fn = Guid.NewGuid().ToString() + FileUpload1.FileName.Substring(FileUpload1.FileName.LastIndexOf("."));
                if (sp.EndsWith("\\") == false)
                    sp += "\\";
                sp += fn;
                FileUpload1.PostedFile.SaveAs(sp);
                lblUploadMsg.ForeColor = System.Drawing.Color.Green;
                lblUploadMsg.Text = "Picture Uploaded successfully. You can upload upto 4 pictures";

                if (ViewState["Img1"] == null)
                {
                    ViewState["Img1"] = "~/ItemPictures/" + fn;
                }
                else if (ViewState["Img2"] == null)
                {
                    ViewState["Img2"] = "~/ItemPictures/" + fn;
                }
                else if (ViewState["Img3"] == null)
                {
                    ViewState["Img3"] = "~/ItemPictures/" + fn;
                }
                else if (ViewState["Img4"] == null)
                {
                    ViewState["Img4"] = "~/ItemPictures/" + fn;
                }
            }
            else
            {
                lblUploadMsg.Text = "Maximum 2MB files are allowed";
            }
        }
        else
        {
            lblUploadMsg.Text = "Only JPG files are allowed";
        }
    }
    else
    {
        lblUploadMsg.Text = "No File was Selected";
    }
    ShowAvailblImgs();
}

I have four Asp.Net images that are invisible at page load time. 我有四个在页面加载时不可见的Asp.Net图像。 To show them i have the following code. 为了显示它们,我有以下代码。

private void ShowAvailblImgs()
{
    if (ViewState["Img1"] != null)
    {
        //The string URL variable is used just to show what value ViewState["image1"] currently has.
        string URL = (string)ViewState["img1"];
        Response.Write(URL);
        Image1.ImageUrl = (string)ViewState["img1"];
        Image1.Width = 130;
        Image1.Height = 130;
        Image1.Visible = true;
    }
    else
        Image1.Visible = false;
    if (ViewState["Img2"] != null)
    {
        Image2.ImageUrl = (string)ViewState["img2"];
        Image2.Width = 130;
        Image2.Height = 130;
        Image2.Visible = true;
    }
    else
        Image2.Visible = false;
    if (ViewState["Img3"] != null)
    {
        Image3.ImageUrl = (string)ViewState["img3"];
        Image3.Width = 130;
        Image3.Height = 130;
        Image3.Visible = true;
    }
    else
        Image3.Visible = false;
    if (ViewState["Img4"] != null)
    {
        Image4.ImageUrl = (string)ViewState["img4"];
        Image4.Width = 130;
        Image4.Height = 130;
        Image4.Visible = true;
    }
    else
        Image4.Visible = false;
}

I am having very strange behaviour of ViewState variable. 我对ViewState变量有非常奇怪的行为。 Upon loading images, they are not shown in Asp.Net image control. 加载图像后,它们不会显示在Asp.Net图像控件中。 Instead empty image areas are shown. 而是显示空图像区域。 Though the URL variable i used print exact path to the image. 虽然我使用的URL变量打印图像的确切路径。 Upon saving the image (which are really blank image areas), it get saved my .aspx page. 保存图像(实际上是空白图像区域)后,它会保存我的.aspx页面。 I was using Session variable which worked fine but due to some reasons, i want to use ViewState variable. 我使用的Session变量工作正常,但由于某些原因,我想使用ViewState变量。

I don't want to say a wrong thing, but, reading from documentation, it was written that 我不想说错话,但是,从文档中读到,就是这样写的

"Viewstate is used to preserve page and control values between round trips" http://msdn.microsoft.com/en-us/library/ms178198(v=vs.85).aspx “Viewstate用于在往返之间保留页面和控制值” http://msdn.microsoft.com/en-us/library/ms178198(v=vs.85).aspx

Have you tried to put the URLs in variables, and then assigning to ViewState["imgX"], and try to do another Postback and see if the ViewState["imgX"] contains the URL? 您是否尝试将URL放入变量,然后分配给ViewState [“imgX”],并尝试执行另一个Postback并查看ViewState [“imgX”]是否包含URL?

ViewState is fine. ViewState很好。 You are using different case for your ViewState index string, so they don't refer to the same ViewState property. 您对ViewState索引字符串使用不同的大小写,因此它们不引用相同的ViewState属性。 "Img1" is not equal to "img1". “Img1”不等于“img1”。

ViewState["Img1"] != null)
    {
        Image2.ImageUrl = (string)ViewState["img1"];

I recommend either using a constant for the value name, as below. 我建议使用常量作为值名称,如下所示。

    const string image1 = "img1";
    const string image2 = "img2";
    const string image3 = "img3";
    const string image4 = "img4";

Or refer to my blog post to create strongly-typed pseudo-properties using extension methods. 或者参考我的博客文章,使用扩展方法创建强类型的伪属性。 http://coding.grax.com/2013/06/simple-strongly-typed-pattern-for.html http://coding.grax.com/2013/06/simple-strongly-typed-pattern-for.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM