【问题标题】:Admin control in C# giving errorC# 中的管理员控制给出错误
【发布时间】:2015-01-07 18:26:58
【问题描述】:

我正在编写一个控件来添加一个按钮来编辑我的页面。该控件名为 adminlinks,它包含一个应该只对角色 admin 可见的链接。关于为什么我使用此脚本收到错误无效令牌的任何想法?提前致谢!

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.UserControl;
using Microsoft.Ajax.Utilities;

public partial class Controls_adminlinks : System.Web.UI.UserControl
{
protected void Page_Load(object sender, System.EventArgs e);

if (Context.User.IsInRole("Admin"))
{
    uc1:adminlinks.Visible = true;
}
else
{
    uc1:adminlinks.Visible = false;
}

【问题讨论】:

  • 哪一行给出了错误?
  • 这行:if (Context.User.IsInRole("Admin")) { uc1:adminlinks.Visible = true; } 其他
  • 但在if (Context.User.IsInRole("Admin"))uc1:adminlinks.Visible = true; 是否失败
  • 如果在 if 中失败,请道歉
  • 它指出:类、结构或接口成员声明中的标记“if”无效

标签: c# asp.net controls


【解决方案1】:

你需要一个方法。

您的 if 语句在类中,而不是方法中。例如:

    public void TestMethod()
    {
          if (Context.User.IsInRole("Admin"))
           {
             uc1:adminlinks.Visible = true;
            }
          else
         {
           uc1:adminlinks.Visible = false;
         }
    }

【讨论】:

  • 知道了-感谢您的宝贵时间!
【解决方案2】:

您的问题是代码未包含在您的 Page_Load 函数中。你有:

protected void Page_Load(object sender, System.EventArgs e);

但应该是:

protected void Page_Load(object sender, System.EventArgs e){ 

    if (Context.User.IsInRole("Admin"))
    {
        uc1:adminlinks.Visible = true;
    }
    else
    {
        uc1:adminlinks.Visible = false;
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-05-07
    • 1970-01-01
    • 2016-11-05
    • 1970-01-01
    • 2014-02-18
    • 1970-01-01
    相关资源
    最近更新 更多