Help me figure it out and fix it. An application in Asp.Net should work as a file manager. But beyond one link does not go, returns to the starting one. Here is a piece of code, I think the problem is here:

protected void Page_Load(object sender, EventArgs e) { PlaceForSubDirectories.Controls.Clear(); amount10To50 = 0; amountLess10 = 0; amountMore50 = 0; if (directory == null) { directory = new DirectoryInfo(Directory.GetCurrentDirectory()); } ShowCurrendDirectory(); Count(directory); ShowLenghtOfAllFiles(); } protected void Link_click(object sender, EventArgs e) { if ((sender as LinkButton).Text == "..." + "<br/>") { directory = directory.Parent; } else { PlaceForSubDirectories.Controls.Clear(); string subDirectory = Regex.Replace((sender as LinkButton).Text, "<br/>", ""); directory = new DirectoryInfo(directory.FullName + @"\" + subDirectory); } Page_Load(sender, e); } 
  • 1. Do you have LinkButton on LinkButton "... <br/>"? 2. The current directory on which computer (and for which process) will return Directory.GetCurrentDirectory() ? 3. Do not call Page_Load , this method is intended to be called by ASP.NET code. - Igor

2 answers 2

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.

  • You corrected the implementation of properties so that their connection is not "torn" by the unsuccessful assignment of a value. PS you forgot return in the DirectoryPath.get code, be careful - Pavel Mayorov
  • Thanks a lot, did not look through. - Serafim Prozorov

I found an exit using the "spear" method: the variable directory in the class was made static.

  • 2
    And now check how it will work for several users :) - Pavel Mayorov
  • You should not look for an answer at random, study the materiel. - Serafim Prozorov
  • @ Mihanik71 please be careful. It was not an addition to the question, it was the author's answer to your question. Yes, it is wrong - but it remains the answer, not a supplement. - Pavel Mayorov
  • @PavelMayorov i. Now the author will come to another wrong answer, publish it and have to leave it too? - Mihanik71
  • one
    @ Mihanik71 delete the wrong answers you need. But not with this wording. - Pavel Mayorov