Not the fact that the only problem is this, but. An instance of the Page heir is created for each request, that is, your check in Page_Load is meaningless in this situation: the directory == null will always be true. If the field is made static, it will be the same for all users of the application. To save the directory value between queries, you need to create, as an option, although, to put it mildly, not the best, the DirectoryPath property with the path to the directory that will store the value in ViewState:
private DirectoryInfo _directory; protected DirectoryPath { get { return ViewState["DirectoryPath"].ToString(); } set { ViewState["DirectoryPath"] = value; _directory = null; } } protected DirectoryInfo Directory { get { return (_directory ?? (_directory = new DirectoryInfo(DirectoryPath)); } set { DirectoryPath = value.FullName; _directory = value; } } protected void Page_Load(object sender, EventArgs e) { // Some code... if (!IsPostBack) { DirectoryPath = Directory.GetCurrentDirectory(); } ListDirectory(); } protected void Link_click(object sender, EventArgs e) { // sender as LinkButton -- тоже не очень хорошо // <br/> в тексте кнопки -- тем более string text = (sender as LinkButton).Text; if (text.Equals("...")) { Directory = directory.Parent; } else { PlaceForSubDirectories.Controls.Clear(); DirectoryPath = Path.Combine(DirectoryPath, text); } ListDirectory(); } private void ListDirectory() { ShowCurrendDirectory(); Count(directory); ShowLenghtOfAllFiles(); }
In theory, this is how all properties should be implemented, the state of which should be maintained between requests.
The Page_Load method from the event handler should not be called, as mentioned above, if you need to change the state controls, you need to have a separate method for this (shown above).
Plus, I would like to say that shining the bin directory to the user is a bad idea, although it may be necessary for you as part of the task.
LinkButton
onLinkButton
"... <br/>"? 2. The current directory on which computer (and for which process) will returnDirectory.GetCurrentDirectory()
? 3. Do not callPage_Load
, this method is intended to be called by ASP.NET code. - Igor