名称'sloginname'在当前上下文中不存在 [英] The name 'sloginname' does not exist in the current context
问题描述
我想从c#asp.net中的会话对象中检索用户名,因为我在主页的aspx页面中使用了if else条件。但是获取此错误字符串登录名在当前上下文中不存在如何解决这个问题
我尝试了什么:
i want to retrieve the username from session object in c# asp.net for that i have used if else condition in aspx page of home page. But getting this error string login name does not exist in the current context how to solve this
What I have tried:
<% if (Session["username"] == null)
{ %>
<div class="col-md-3 col-4 sign-btn">
<a href="#" data-toggle="modal" data-target="#signModalCenter">Signin</a>
</div>
<div class="col-md-3 col-4 sign-btn">
<a href="#" data-toggle="modal" data-target="#registerModalCenter"> Register</a>
</div>
<div class="search col-md-2 col-4">
<div class="mobile-nav-button">
<button type="button" id="trigger-overlay"></button>
</div>
<div class="overlay overlay-door">
<button class="overlay-close" type="button"></button>
<form action="#" method="post" class="d-flex">
<input class="form-control" type="search" placeholder="Search here..." required="required" />
<button type="submit" class="btn btn-primary submit"></button>
</form>
</div>
</div>
<%} %>
<% else
{ %>
<div class="col-md-3 col-4 sign-btn">
Welcome <% =sLoginName %> <a href="Logout.aspx" data-toggle="modal" data-target="#userModalCenter" id="ALogout"> Logout</a>
</div>
<%} %>
代码落后:
公共字符串sLoginName = string .Empty;
页面加载:
Code behind:
public string sLoginName = string.Empty;
in page load:
sLoginName = Session["username"].ToString();
推荐答案
检查sLoginName的定义是否是页面类定义的一部分,而不是子类或普通类: br />
Check that the definition of sLoginName is part of the page class definition, not in a subsidiary class, or a general class:
public partial class MyPage: System.Web.UI.Page
{
public string sLoginName = string.Empty;
protected void Page_Load(object sender, EventArgs e)
{
sLoginName = Session["username"].ToString();
}
}
如果这不能解决问题,请复制并粘贴实际的错误消息和相关信息,而不是解释它。
If that doesn't fix it, copy and paste the actual error message and related info instead of paraphrasing it.
另一种选择是在页面上定义 sLoginName
并将其从Page_Load
Another option would be to define sLoginName
on the page and remove it from Page_Load
</form>
</div>
</div>
<% }
else
{
string sLoginName = Session["username"].ToString();
%>
<div class="col-md-3 col-4 sign-btn">
Welcome <% =sLoginName %> <a href="Logout.aspx" data-toggle="modal" data-target="#userModalCenter" id="ALogout">class="fa fa-sign-out"> Logout</a>
</div>
它可能不太理想,但它可以通过捏
It may not be ideal but it can come through in a pinch
这篇关于名称'sloginname'在当前上下文中不存在的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!