Learning
Learning

Reputation: 20001

Passing Value from function to class level variable always null

I defined a string variable at class level and set this variable in protected void UploadButton_Click(object sender, EventArgs e) after uploading the file successfully.

I do this so that i can pass value of fileName variable from this function to another protected void btnSave_Click(object sender, EventArgs e) {} where i save it in the database. but value always is null. Am i doing something wrong or it remain null as functions are defined as Protected type

public partial class News: System.Web.UI.Page
{
    string _fileName = null;

     protected void Page_Load(object sender, EventArgs e)
     {
         if (!IsPostBack)
         {
          // some code here......
         }
     }

protected void UploadButton_Click(object sender, EventArgs e)
    {
        if (FileUploadControl.HasFile)
        {
            try
            {
                System.IO.FileInfo f = new System.IO.FileInfo(FileUploadControl.PostedFile.FileName);

                if (f.Extension.ToLower() == ".pdf" || f.Extension.ToLower() == ".doc" || f.Extension.ToLower() == ".docx")
                {
                    //3MB file size
                    if (FileUploadControl.PostedFile.ContentLength < 307200)
                    {
                        string filename = Path.GetFileName(FileUploadControl.FileName);
                        if (!System.IO.File.Exists("../pdf/news/" + FileUploadControl.FileName))
                        {
                            FileUploadControl.SaveAs(Server.MapPath("../pdf/research/") + filename);
                            StatusLabel.Text = "Upload status: File uploaded!";
                            _fileName = FileUploadControl.FileName;
                        }
                        else
                        {
                            _fileName = null;
                            StatusLabel.Text = "File with this name already exsists, Please rename file and Upload gain";
                        }
                    }
                    else
                    {
                        _fileName = null;
                        StatusLabel.Text = "Upload status: The file has to be less than 3MB!";
                    }
                }
                else
                {
                    _fileName = null;
                    StatusLabel.Text = "Upload status: Only PDF or Word files are accepted!";
                }
            }
            catch (Exception ex)
            {
                _fileName = null;
                StatusLabel.Text = "Upload status: The file could not be uploaded. The following error occured: " + ex.Message;
            }
        }
    }
}

Upvotes: 0

Views: 86

Answers (1)

Lucas Trzesniewski
Lucas Trzesniewski

Reputation: 51330

ASP.NET WebForms doesn't quite work like you expect it to... You have to understand the page life cycle.

Basically, at each request, your page object is recreated from scratch. The two button clicks will generate two requests, and so you'll get two page instances, one for each request. You can't share data between them this way.

You have several ways to overcome this:

  • Use the ViewState for this. This is an object that is basically serialized, then sent to the client. The client sends it back at each postback, and it's deserialized so you can access it. So do not put sensitive data inside. There are facilities that let you encrypt this data though.
  • Use the Session. But this can become messy pretty quick, and you'll have to deal with the case when the user opens serveral instances of the same page.

Upvotes: 1

Related Questions