在 aspx 上无法检测到 cs 文件上的 C# 参数
所以我对 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
我想知道我做错了什么......
So I have a condition on the aspx file:
<% if (yes)
{%>
{
<div>
<h1>hell yes!!</h1>
<p>Welcome</p>
</div>
<%}%>/
and here is my code on the page load
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
{
}
}
}
I get the error:
CS0103: The name 'yes' does not exist in the current context
I was wondering what I'm doing wrong...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
yes
是一个局部变量;它不存在于Page_Load
方法之外。您需要在代码隐藏中创建一个
public
(或protected
)属性。yes
is a local variable; it doesn't exist outside of thePage_Load
method.You need to create a
public
(orprotected
) property in the code-behind.我的建议,把这个
然后把
希望有帮助
my suggestion, put this
Then put
Hope it helps
如果您将
yes
设置为受保护的类级变量,它将起作用。 ASPX 页面是一个独立的类,它继承自代码隐藏中定义的类。If you make
yes
a protected class-level variable it will work. The ASPX page is a separate class that inherits from the class defined in the code-behind.您在 if 块中声明
yes
- 这就是变量的范围。一旦代码执行退出 if 块,您的yes
变量将排队等待垃圾回收,并且您将无法访问它。解决此问题的一种方法是在页面的类级别声明公共属性
Yes
,您可以在Page_Load
方法中设置该属性。然后您应该能够在 .aspx 中访问它。例子:You're declaring
yes
within an if block - that's the scope of the variable. Once code execution exits the if block, youryes
variable will be queued up for garbage collection and you won't be able to access it.One way to resolve this is declare a public property
Yes
at the page's class level, which you can set within thePage_Load
method. Then you should be able to access it within the .aspx. Example:yes
是Page_Load
本地的要么将 yes 提升到某个字段,要么更好的是,使用私有 setter 使其成为类的公共财产:
yes
is local toPage_Load
Either promote yes to a field or better yet, make it a public property of your class with a private setter: