Domanda

I have the following code. I am trying to display a message when a file is to big. My problem is that the message is displayed only when I press the submit button twice. It is always one step behind. How can i fix that?

What I am doing is save a viewstate when the file is to big, and when the page loads, set the visibility of the label to true or false, depending on the file size.

I am guessing the load happens before my check, so is there another method i can use instead of Page_Load or can I do something else to make it work?

<asp:UpdatePanel ID="updatePanel" runat="server">
    <ContentTemplate>
        <div>
            <div class="balClear">
                <asp:FileUpload ID="uploader" CssClass="balUploader" runat="server"/>
                <asp:ImageButton ID="uploaderEraser" CssClass="balUploaderCleaner" OnClick="uploaderEraser_Click" runat="server" Width="25" Height="25" />
            </div>
            <div style="clear:both">
                <asp:Label ID="fileSizeError" runat="server" ForeColor="Red">The file you are trying to upload is too big. The maximul file size is 2MB.</asp:Label>
            </div>
        </div>
    </ContentTemplate>
    <Triggers>
        <asp:AsyncPostBackTrigger ControlID="uploaderEraser" EventName="Click" />
    </Triggers>

</asp:UpdatePanel>

<asp:Button ID="submit" runat="server" Text="Submit" OnClick="submit_Click" />

cs code:

 string groupURL = "~/Uploads/Group/";
    string privateURL = "~/Uploads/Private/";

    int maximumMB = 2;

    string folderURL
    {
        get { return IsGroup == true ? groupURL : privateURL; } 
    }

    public bool IsGroup
    {
        get { return ViewState["balUploader_IsGroup"] == null ? false : (bool)ViewState["balUploader_IsGroup"]; }
        set { ViewState["balUploader_IsGroup"] = value; }
    }

    bool FileTooBig
    {
        get { return ViewState["balUploader_FileTooBig"] == null ? false : (bool)ViewState["balUploader_FileTooBig"]; }
        set { ViewState["balUploader_FileTooBig"] = value; }
    }

protected void Page_Load(object sender, EventArgs e)
    {
        if (FileTooBig == true)
        {
            fileSizeError.Visible = true; 
        }
        else
        {
            fileSizeError.Visible = false; 
        }
    }

 public void StoreFile()
    {
        if (uploader.HasFile)
        {
            if (uploader.FileBytes.Length / 1024 / 1024 < maximumMB)
            {
                FileTooBig = false;
                string fileName = Path.GetFileName(uploader.FileName);
                uploader.SaveAs(Server.MapPath(folderURL) + fileName);
            }
            else
            {
                FileTooBig = true;
            }
        }
        else
        {
            FileTooBig = false;
        }
    }


    protected void submit_Click(object sender, EventArgs e)
    {
        StoreFile();
    }
È stato utile?

Soluzione

Basically, what is happening is that the submit_Click event is being fired after the Page_Load. That is due to the ASP.NET page life cycle.

The basic order of events is the following:

1 - Init
2 - Load  (Page_Load is fired)
3 - Control Events (submit_Click is fired)
4 - Load Complete
5 - Pre_Render
6 - Render

You have two options here.

1 - Use the LoadComplete handler:

 protected override void OnLoadComplete(EventArgs e)
 {
     base.OnLoadComplete(e);
     //Label Logic here
 }

2 - Use the OnPreRender:

protected override void OnPreRender(EventArgs e)
{
    base.OnPreRender(e);
    //Label Logic here
}
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top