본문 바로가기
Web Program/Asp.net Lecture

gridview 에서 checkbox 모두 선택 구현.

by 현이빈이 2009. 4. 1.
반응형

.aspx
gridview 구현부

<asp:GridView ID="GridView1" runat="server" BackColor="White" BorderColor="#CC9966"
            BorderStyle="None" BorderWidth="1px" CellPadding="4" DataSourceID="SqlDataSource1" AllowPaging="True" AutoGenerateColumns="False" EmptyDataText="데이터가 없습니다." Width="730px" >
            <RowStyle BackColor="White" ForeColor="#330099" />
            <FooterStyle BackColor="#FFFFCC" ForeColor="#330099" />
            <PagerStyle BackColor="#FFFFCC" ForeColor="#330099" HorizontalAlign="Center" />
            <SelectedRowStyle BackColor="#FFCC66" Font-Bold="True" ForeColor="#663399" />
            <HeaderStyle BackColor="#990000" Font-Bold="True" ForeColor="#FFFFCC" />
            <Columns>
              <asp:TemplateField HeaderText="전체선택">               
                <ItemTemplate>
                  <asp:CheckBox ID="chkSelect" runat="server" />
                </ItemTemplate>
                <HeaderTemplate>
                  <input id="chkAll"  onclick="SelectAllCheckboxes(this);" runat="server" type="checkbox" />
                </HeaderTemplate>
              </asp:TemplateField>
                <asp:BoundField DataField="FILE_NAME" HeaderText="파일명" SortExpression="FILE_NAME" />
                <asp:BoundField DataField="FILE_SIZE" HeaderText="크기" SortExpression="FILE_SIZE" />
              <asp:BoundField DataField="REG_DATE" HeaderText="등록일시" SortExpression="REG_DATE" />
              <asp:BoundField DataField="FILE_ID" HeaderText="file id" />
              <asp:BoundField DataField="FILE_PATH" HeaderText="파일 패스" />
            </Columns>
        </asp:GridView>


버튼 이벤트에서 체크 확인
for (int i = 0; i < GridView1.Rows.Count; i++)
  {
   GridViewRow row = GridView1.Rows[i];
   bool isChecked = ((CheckBox)row.FindControl("chkSelect")).Checked;

   if (isChecked)
   {
//이벤트 처리
    }
  }


<script language="JavaScript">
<!--
  function SelectAllCheckboxes(spanChk){   
    var oItem = spanChk.children;
    var theBox= (spanChk.type=="checkbox") ? spanChk : spanChk.children.item[0];
    xState=theBox.checked;
    elm=theBox.form.elements;

    for(i=0;i<elm.length;i++)
      if(elm[i].type=="checkbox" && elm[i].id!=theBox.id)
      {      
        if(elm[i].checked!=xState)
          elm[i].click();      
      }
  }

//-->
</script>

반응형