2012-12-21 21 views
1

我有一個jqgrid和按鈕圖像列,看起來像這樣:使用會話禁用按鈕

button | sample1 |紅色
按鈕| sample2 |藍色
button | sample3 |紅色
按鈕| sample4 |紅色
按鈕| sample5 |藍色
button | sample6 |綠色

什麼需要做的是這樣的:

如果用戶有紅會話的會話ID [「顏色」] =紅色;

然後所有不是紅色的按鈕將被禁用。 與其他顏色相同。

但如果會話顏色不存在。它只是隱藏的列

這裏是我的示例代碼:

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} 

問題:我將如何禁用按鈕,如果用「紅」會話的用戶將登錄

+0

那麼有什麼問題? – Ulises

+1

我不知道我將如何禁用/隱藏按鈕。如果它包含「紅色」 – StackOverflowUser

+0

使用按鈕Collection方法,並且button.Visible = true;和button.Visible = false;代碼/ – Sagotharan

回答

0

試用按鈕收集想法。請參閱下面的代碼。

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; 

        } 
       } 
+1

更好地使用開關而不是if和elses –

+0

@NagaHarishMovva它是否需要在這裏。代碼只有三個if語句!如果我更改爲開關將編碼速度性能? – Sagotharan

+0

我同意,如果只有兩到三個,它說好。我沒有說如果不使用。我說這是「使用開關更好」。 +1 –