2016-08-30 4 views
0

5 셀을 포함하는 각 세트의 첫 번째 행에 2 세트의 셀이있는 테이블이 있습니다. 각 셀에는 내부에 컨트롤이 들어 있습니다.표 셀을 그룹화하여 한 문장으로 숨길 수있는 방법은 무엇입니까? (webforms)

내 조건은 특정 조건이 참이면 조건이 거짓 일 때 한 집합을 숨기고 다른 집합을 표시해야하며 반대의 경우도 마찬가지입니다. 현재 나는 진실한 부분에 대해 내 코드에 10 개의 .Visible = 문을, 거짓 부분에 10 개의 문을 가지고 있습니다. 그룹을 숨기려면 모두 5를 숨길 수 있도록 함께 세포의 집합을 group 방법이 있습니까? 서버 측 코드에서 jQuery를 모두 사용해야합니다.

<table> 
<tr> 
<!-- first set --> 
<td runat="server" id="set1_cell1"> content here</td> 
<td runat="server" id="set1_cell2"> content here</td> 
<td runat="server" id="set1_cell3"> content here</td> 
<td runat="server" id="set1_cell4"> content here</td> 
<td runat="server" id="set1_cell5"> content here</td> 
<!-- end first set --> 


<!-- second set --> 
<td runat="server" id="set2_cell1"> content here</td> 
<td runat="server" id="set2_cell2"> content here</td> 
<td runat="server" id="set2_cell3"> content here</td> 
<td runat="server" id="set2_cell4"> content here</td> 
<td runat="server" id="set2_cell5"> content here</td> 
<!-- end second set --> 
</tr> 
... 
</table> 

이 난 그냥 하나 그 10 문을 대체하는 사랑

if (condition is true) 
{ 
set1_cell1.Visible = true; 
set1_cell2.Visible = true; 
set1_cell3.Visible = true; 
set1_cell4.Visible = true; 
set1_cell5.Visible = true; 

set2_cell1.Visible = false; 
set2_cell2.Visible = false; 
set2_cell3.Visible = false; 
set2_cell4.Visible = false; 
set2_cell5.Visible = false; 
} 
else 
{ 
    // opposite of the above 
} 

처럼 내 현재 코드는 모습입니다.

+0

행에 다른 셀이 있습니까? – ConnorsFan

+0

@ConnorsFan 예 – fahadash

답변

2

당신은 각 그룹의 세포에 다른 클래스 이름을 지정할 수 있습니다 : 코드 숨김에서

<table> 
    <tr id="row1" runat="server"> 
     <td class="set1">Content 1a</td> 
     <td class="set1">Content 1b</td> 
     <td class="set1">Content 1c</td> 
     <td class="set1">Content 1d</td> 
     <td class="set1">Content 1e</td> 

     <td class="set2">Content 2a</td> 
     <td class="set2">Content 2b</td> 
     <td class="set2">Content 2c</td> 
     <td class="set2">Content 2d</td> 
     <td class="set2">Content 2e</td> 

     ... 
    </tr> 
    ... 
</table> 

을, 당신은/클래스 이름과 조건의 값에 따라 셀을 숨기 보여

foreach (HtmlTableCell cell in row1.Cells) 
{ 
    string className = cell.Attributes["class"]; 

    if (className == "set1") 
    { 
     cell.Visible = condition; 
    } 

    if (className == "set2") 
    { 
     cell.Visible = !condition; 
    } 
} 

참고 1 : 클래스 이름은 (특히 jQuery와 함께) 원할 경우 클라이언트 측에서도 동일한 작업을 수행하는 데 사용할 수 있습니다.

참고 2 : 위의 코드에서 클래스 이름을 사용하지만 사용자 정의 속성 (예 : class="set1" 대신 data-group="set1", 코드 숨김에 해당하는 변경)으로 동일한 결과를 얻을 수 있습니다.

+0

@fahadash -이 솔루션을 사용해 볼 기회가 있었습니까? – ConnorsFan

관련 문제