2012-12-21 10 views
1

다음과 같은 jqgrid 및 버튼 이미지 열이 있습니다.세션을 사용하여 버튼을 사용하지 않음

버튼 | 샘플 1 | 빨간색
버튼 | 샘플 2 | 파란색
버튼 | 샘플 3 | 빨간색
버튼 | 샘플 4 | 빨간색
버튼 | 샘플 5 | 파란색
버튼 | 샘플 6 | 녹색
해야 할 일을

은 이것이다 : 사용자가 빨간색 세션의 세션 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

단추 사용 메서드 및 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 여기에 필요합니까? 코드에는 단 3 개의 if 문만 있습니다! Switch로 변경하면 Speed ​​Performance가 코딩됩니까? – Sagotharan

+0

2 ~ 3 개 밖에 없다면 동의합니다. 나는 사용하지 않는다고 말하지 않았다. 나는 그것이 "스위치를 사용하는 것이 더 좋다"고 말했다. +1 –

관련 문제