数日間、ASP.NET のプロジェクトで問題が発生しました。次のようなウェブサイトを設計する必要があります。
- 最初: プロジェクト内のフォルダーがサブフォルダー bin であるかどうか、およびこの bin フォルダー内に 2 つの特定のファイルのいずれかがあるかどうかをチェックする関数
- 2 番目: 1 番目の関数の条件を持つフォルダーのみを Web サイトに表示する関数 (リスト)
今問題に。いくつかの助けを借りて、コードを書くことができました (私は ASP.NET のまったくの初心者です)。コードは次のとおりです。
ここにaspxコード:
<%@ Page Language="C#" AutoEventWireup="True" CodeBehind="Home.aspx.cs" Inherits="TestSite.Home" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<link href="Layout.css" rel="stylesheet" type="text/css" />
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:ListView ID="listView" runat="server">
<ItemTemplate>
<asp:Label ID="Label1" Text="<%#Container.DataItem %>" runat="server" />
</ItemTemplate>
</asp:ListView>
</div>
</form>
</body>
</html>
aspx.cs コードは次のとおりです。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
namespace TestSite
{
public partial class WorkingwithDirectory : System.Web.UI.Page
{
List<string> results = new List<string>();
public bool IsApplicationDirectory( string directory )
{
if ( Directory.Exists(Server.MapPath("~/../bin")) ) {
if ( File.Exists(Server.MapPath("~/../bin/Portal.Server.dll")) || File.Exists(Server.MapPath("~/../bin/Host.Server.dll")) ) {
return true;
}
else {
return false;
}
}
else {
return false;
}
}
public List<string> GetApplicationDirectories()
{
DirectoryInfo dir = new DirectoryInfo(Server.MapPath("~/"));
foreach (DirectoryInfo di in dir.GetDirectories())
{
if ( IsApplicationDirectory( dir.FullName ) )
{
results.Add(dir.FullName);
}
}
return results;
}
protected void Page_Load( object sender, EventArgs e )
{
if ( !Page.IsPostBack )
{
List<string> appDirectories = GetApplicationDirectories();
--> listView.DataSource = appDirectories;
--> listView.DataBind();
}
}
}
}
問題: 上記の aspx.cs ファイル (矢印を付けた場所) で、「名前 'listView' は現在のコンテキストに存在しません」というエラーが表示されます。
いくつかの解決策を試しましたが、残念ながらどのテーマも機能しませんでした。私は助けにもっと感謝しています。