0

だから私は aspx ファイルに条件があります:

<% if (yes)  
   {%>
   {
<div>
    <h1>hell yes!!</h1>
    <p>Welcome</p>
</div>
<%}%>/

そして、これがページ読み込み時の私のコードです

protected void Page_Load(object sender, EventArgs e)
{
  if (accnt != null)
    {
        using (SqlConnection conn = new SqlConnection(connectionstring))
         {
            conn.Open();
            string strSql = "select statement"
                      :
                      :
            try
            {
                if (intExists > 0)
                {
                    bool yes= check(accnt);
                }
            }
            catch
            {
            }
        }
    }

エラーが発生します:

CS0103: The name 'yes' does not exist in the current context

何がいけないんだろう…と考えていました。

4

5 に答える 5

2

yesローカル変数です。Page_Loadメソッドの外には存在しません。コード ビハインドで(または) プロパティ
を作成する必要があります。publicprotected

于 2011-03-10T20:00:27.387 に答える
1

私の提案、これを置く

public partial class _Default : System.Web.UI.Page 
{
    public string yes = "";

次に入れます

protected void Page_Load(object sender, EventArgs e)
{
  if (accnt != null)
    {
        using (SqlConnection conn = new SqlConnection(connectionstring))
         {
            conn.Open();
            string strSql = "select statement"
                      :
                      :
            try
            {
                if (intExists > 0)
                {
                    bool yes= check(accnt);
                }
            }
            catch
            {
            }
        }
    }

それが役に立てば幸い

于 2011-03-10T20:02:29.510 に答える
1

yes保護されたクラスレベルの変数を作成すると、機能します。ASPX ページは、コード ビハインドで定義されたクラスから継承する別のクラスです。

于 2011-03-10T20:00:57.327 に答える
0

if ブロック内で宣言しyesています - それが変数のスコープです。コードの実行が if ブロックを終了すると、yes変数はガベージ コレクションのためにキューに入れられ、アクセスできなくなります。

Yesこれを解決する 1 つの方法は、メソッド内で設定できるページのクラス レベルでパブリック プロパティを宣言することですPage_Load。その後、.aspx 内でアクセスできるようになります。例:

public class MyPage : System.Web.UI.Page {
  public bool Yes()  { get; set; } 
}
于 2011-03-10T20:03:16.607 に答える
0

yesフィールドに yes をプロモートするPage_Load か、さらに良いことに、プライベート セッターを使用してクラスのパブリック プロパティにします。

public bool Yes { get; private set; }
于 2011-03-10T20:03:50.747 に答える