我该如何解决这个问题? [英] How do I slove this problem?

查看:91
本文介绍了我该如何解决这个问题?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好。有可能根据角色明显控制按钮吗?例如。如果我使用管理员角色登录,我将能够看到按钮。当我登录到另一个角色时,我将无法看到该按钮。



我尝试了什么:



  if (User.IsInRole(  Admin))
{
Button1.Visible = true ;
}
else if (User.IsInRole( 成员))
{
Button1.Visible = ;
}



不起作用。

解决方案

引用:

不起作用



告诉我们什么。而且由于我们无法在相同的条件下运行您的代码 - 我们无法访问周围的代码,您的登录数据库或任何其他内容 - 所以它将取决于您。

使用调试器。



在函数的第一行放置一个断点,并通过调试器运行代码。然后查看您的代码,并查看您的数据并找出手动应该发生的事情。然后单步执行每一行检查您预期发生的情况正是如此。如果不是,那就是当你遇到问题时,你可以回溯(或者再次运行并仔细观察)以找出原因。


抱歉,但我们不能为您做到这一点 - 时间让您学习一门新的(非常非常有用的)技能:调试!


建议:出于测试目的,显示用户角色,可能有一个惊喜,比如没有管理员或会员。



你应该学会尽快使用调试器。而不是猜测你的代码在做什么,现在是时候看到你的代码执行并确保它完成你期望的。



调试器允许你跟踪执行逐行检查变量,你会看到它有一个停止做你期望的点。

调试器 - 维基百科,免费的百科全书 [ ^ ]

掌握Visual Studio 2010中的调试 - A初学者指南 [ ^ ]



调试器在这里向您展示您的代码正在做什么,您的任务是与它应该做什么进行比较。

hi all.Is it possible to control the button visibly based on the role?for example.If i log in with an admin role..i will be able to see the button. And when i log in to another role, i will not be able to see the button.

What I have tried:

if (User.IsInRole("Admin")) 
            {
                Button1.Visible = true;
            }
            else if(User.IsInRole("Member"))
            {
                Button1.Visible=false;
            }


which does not work.

解决方案

Quote:

does not work


Tells us nothing. And since we can't run your code under the same conditions you can - we don't have any access to the surrounding code, or your login database, or anything really - so it's going to be up to you.
Use the debugger.

Put a breakpoint on the first line in the function, and run your code through the debugger. Then look at your code, and at your data and work out what should happen manually. Then single step each line checking that what you expected to happen is exactly what did. When it isn't, that's when you have a problem, and you can back-track (or run it again and look more closely) to find out why.

Sorry, but we can't do that for you - time for you to learn a new (and very, very useful) skill: debugging!


Advice: For testing purpose, display the user role, may be there is a surprise like not an Admin nor a Member.

You should learn to use the debugger as soon as possible. Rather than guessing what your code is doing, It is time to see your code executing and ensuring that it does what you expect.

The debugger allow you to follow the execution line by line, inspect variables and you will see that there is a point where it stop doing what you expect.
Debugger - Wikipedia, the free encyclopedia[^]
Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]

The debugger is here to show you what your code is doing and your task is to compare with what it should do.


这篇关于我该如何解决这个问题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆