Showing posts with label Gridview. Show all posts
Showing posts with label Gridview. Show all posts

Thursday, September 15, 2011

Loop through all folders and display files in a Gridview using ASP.NET

Loop through all folders and display files in a Gridview using ASP.NET This is one of the usefull code snippet everybody looking for.
When I search google I found lot of methods to accomplish this task but all seems to be very complex and lengthy code.
This is just a 4 line code to loop through all the folders, including subfolder to get the file detals and display it in Gridview.
You may alter this based on your need.
    
 protected void Page_Load(object sender, EventArgs e)
     {
       ArrayList arrFileList = new ArrayList();
       string[] strFiles = Directory.GetFiles("C:\\Test", "*", SearchOption.AllDirectories);
       foreach (string fileName in strFiles)
       {  
         arrFileList.Add(fileName); 
       }  
       GridView1.DataSource = arrFileList;  
       GridView1.DataBind();  
    }  

Thursday, May 19, 2011

Check and uncheck all checkbox in GridView using JavaScript.


This is one of the most common query I saw in Microsoft forums when a beginner start coding. So I thought I will take some time and post a simple and useful code here.
 
<html>
<head id="Head1" runat="server">
  <title>Untitled Page</title>
  <script type="text/javascript">
      function CheckAllRow(chkBox) {
          var gridViewCtlId = '<%=GridView1.ClientID%>';
          var grid = document.getElementById(gridViewCtlId);
          var gridLength = grid.rows.length;
          for (var i = 1; i < gridLength; i++) {
              cell = grid.rows[i].cells[1];
              for (var j = 0; j < cell.childNodes.length; j++) {
                  if (cell.childNodes[j].type == 'checkbox') {
                      cell.childNodes[j].checked = chkBox;
                  }
              }
          }
      }
  </script>
</head>
<body>
  <form id="form1" runat="server">
  <div>
    <asp:LinkButton ID="chkAll" Text="Check all" OnClientClick=" CheckAllRow(true); return false;"
      runat="server"></asp:LinkButton><br />
    <asp:LinkButton ID="lnkUncheck" Text="Uncheck all" OnClientClick=" CheckAllRow(false); return false;"
      runat="server"></asp:LinkButton><br />
    <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" AutoGenerateSelectButton="True"
       OnSelectedIndexChanged="GridView1_SelectedIndexChanged"
      OnDataBound="GridView1_DataBound" OnRowDataBound="GridView1_RowDataBound">
      <Columns>
        <asp:TemplateField>
          <HeaderTemplate>
            Select All
            <asp:CheckBox ID="chkBoxAll" runat="server" onclick="CheckAllRow(this.checked);" />
          </HeaderTemplate>
          <ItemTemplate>
            <asp:CheckBox ID="chkBoxSel" runat="server" />
          </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Course ID">
          <ItemTemplate>
            <%# Eval("ID") %>
          </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Course Name">
          <ItemTemplate>
            <%# Eval("Name")%>
          </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Title">
          <ItemTemplate>
            <%# Eval("Address")%>
          </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Description">
          <ItemTemplate>
            <%# Eval("Phone")%>
          </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Course Type">
          <ItemTemplate>
            <%# Eval("Description") %>
          </ItemTemplate>
        </asp:TemplateField>
      </Columns>
    </asp:GridView>
  </div>
  </form>
</body>
</html>