【问题标题】:Disable button using session使用会话禁用按钮
【发布时间】:2012-12-21 05:03:30
【问题描述】:

我有一个 jqgrid 和按钮图像列,看起来像这样:

按钮 |样品1 |红色
按钮 |样品2 |蓝色
按钮 |样品3 |红色
按钮 |样品4 |红色
按钮 |样品5 |蓝色
按钮 |样品6 |绿色

需要做的是:

如果用户的会话 id 为红色 Session["Color"] = red;

那么所有不是红色的按钮都将被禁用。 与其他颜色相同。

但如果会话颜色不存在。它只会隐藏列

这是我的示例代码:

var sessionColor = '<%=Session["Color"]%>';
    if (sessionColor == 'red') { 
                    // code here
                    // hide button green and blue
                   }
                   if (sessionColor == 'green') {
                       // code here
                       // hide button red and blue
                   }
                   if (sessionColor == 'blue') {
                       // code here
                       // hide button gren and blue
                   }
                   else { //hide all button}

问题:如果具有“红色”会话的用户将登录,我将如何禁用该按钮。

【问题讨论】:

  • 我不知道如何禁用/隐藏按钮。如果它包含“红色”
  • 使用按钮Collection方法,并且button.Visible = true;和 button.Visible = false;确实是代码/
  • 有用的问题我为此添加了 +1

标签: c# asp.net session jqgrid


【解决方案1】:

尝试按钮收集创意。请参阅下面的代码。

var sessionColor = Session["Color"].ToString();
                if (sessionColor == "red")
                {
                    var buttons = ButtonPanel.Controls.OfType<Button>();
                    foreach (var button in buttons)
                    {
                        if (button.BackColor == Color.Red)
                        {
                            button.Visible = true;
                        }
                        else
                        {
                            button.Visible = false;
                        }
                    }

                }
                else if (sessionColor == "green")
                {
                    var buttons = ButtonPanel.Controls.OfType<Button>();
                    foreach (var button in buttons)
                    {
                        if (button.BackColor == Color.Green)
                        {
                            button.Visible = true;
                        }
                        else
                        {
                            button.Visible = false;
                        }
                    }
                }
                else if (sessionColor == "blue")
                {
                    var buttons = ButtonPanel.Controls.OfType<Button>();
                    foreach (var button in buttons)
                    {
                        if (button.BackColor == Color.Blue)
                        {
                            button.Visible = true;
                        }
                        else
                        {
                            button.Visible = false;
                        }
                    }
                }
                else
                {
                    var buttons = ButtonPanel.Controls.OfType<Button>();
                    foreach (var button in buttons)
                    {
                        button.Visible = false;

                    }
                }

【讨论】:

  • 最好用 switch 代替 if 和 elses
  • @NagaHarishMovva 这里需要吗。代码只有三个 if 语句!如果我更改为 Switch 是否会编码 Speed Performance?
  • 我同意,如果它只有两到三个就可以了。我没有说不要使用 if。我说这是“最好使用开关”。 +1
  • @NagaHarishMovva 哦,它的好朋友。,.
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-16
  • 2011-07-17
  • 1970-01-01
  • 2013-06-18
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多