Archive | Code Samples RSS feed for this section

Filling a Grid with Files in a Folder – C#

This code sample shows, in C#, how to get all the files within a specific folder, and list them all, including the file size, in a Gridview control.
To access the FileSystem, you’ll need to import the System.IO namespace, and to use a DataTable, you must import the System.Data namespace:
using System.IO;
using System.Data;
<%@ Import [...]

Read more

Displaying an Image with a DataList

Sometimes, with a Datalist, it would be nice to actually display an image as one of the Data items. This sample uses a simple Access DataSource ID to do so

<asp:AccessDataSource ID=”AccDS1″
Runat=”Server”
SelectCommand = “SELECT id, ProdName, ImgPath, Comments From Products”
DataFile=”\YourPath\products.mdb”>
</asp:AccessDataSource>
<asp:DataList id=”MyDataList” Runat=”Server” DataSourceID=”accDS1″>
<HeaderTemplate>
<table border=”1″>
</HeaderTemplate>
<ItemTemplate>
<tr>
<td colspan=”2″>
<font color=”#000000″>
<b>ProdName : </b>
<%# Container.DataItem(“ProdName”)%>
</td>
</tr>
<tr>
<td align=”Left” valign=”Top”>
<b>Comments: </b>
<br>
<%# Container.DataItem(“Comments”)%></td>
<td align=”Left” [...]

Read more

List and Read Text Files

This sample takes files from a folder (in this situation, text files), and lists them in a listbox. Then, the user can click on a particular file, and display the text from the selected text file, in the textbox to the right of the listbox.
<%@ Page Language="VB" %>
<%@ Import Namespace="System.IO" %>
<script runat="server">
[...]

Read more

Visible Panels via RadioButtonList

This is a sample which shows how to, by choosing items in a RadioButtonList, you can show (make visible/invisible) only certain panels.
<script language="VB" Runat="server">
Sub GetPanel(Source as Object, E as EventArgs)
Select Case rb1.selectedItem.Value
Case 1
pnl1.visible=True
pnl2.visible=False
pnl3.visible=False
Case 2
pnl1.visible=False
pnl2.visible=True
pnl3.visible=False
Case 3
pnl1.visible=False
pnl2.visible=False
pnl3.visible=True
End Select
End Sub
</script>
<html>
<head>
<meta name="GENERATOR" Content="ASP Express 5.0">
<title>RadioButtonList – Panels</title>
</head>
<body>
<form id="form1" Runat="server">
<asp:RadioButtonList ID="rb1" Runat="Server"
OnSelectedIndexChanged="GetPanel"
AutoPostBack="True">
<asp:ListItem Value="1">Panel 1</asp:ListItem>
<asp:ListItem Value="2">Panel 2</asp:ListItem>
<asp:ListItem Value="3">Panel 3</asp:ListItem>
</asp:RadioButtonList>
<asp:panel ID="pnl1" Runat="server" [...]

Read more

Mass Delete Using Gridview with Checkboxes

This sample shows how to use a Gridview to delete multiple records all at once, having marked them with a Checkbox, and clicking one button, external to the Gridview, to delete them all

As usual, here, we’re using the Northwind Database. If you want to try this on your own Northwind database, adhere to this word [...]

Read more

Databound Accordian Control

This sample shows how to bind data to an AJAX Accordian control, using the AJAX Control Toolkit. The first thing you will need to do, is to download it from the AJax.ASP.Net website.
Then, once it’s downloaded and installed, go to the Bin folder, where it’s located, find the AjaxcontrolToolkit.dll and copy it to the Bin [...]

Read more

HTML Named Colors + Hex Using Reflection

This sample actually shows several things, like how to add controls to a page/Placeholder, dynamically, how to get a color from RGB, convert a color to Hex, plus (the main point) it shows how, using reflection, you can iterate through the system colors and display them (using dynamic labels), plus get the HTML hex for [...]

Read more

Gridview – Conditional Images

This Gridview sample shows how to, for each row, based on other data within that row, to show a different image. We do this by creating a TemplateField, and putting an ASP.Net Image control within it, called ‘Image1′.
Then, inside the RowDataBound event of the Gridview, we put code, which first, checks and finds the Image [...]

Read more

Conditional Gridview Text – Checkboxes

This code sample shows how to either show or make invisible, a checkbox in each row of the Gridview, along with making text conditional, based on certain criteria. In this case, if the Postal code starts with a non-numeric character, we change it to “Alt Text”, and we set the Visible property of the checkbox [...]

Read more

Creating a Datatable Manually

This sample shows how to create a datatable manually, adding files from a directory (using System.IO), and then binding the datatable to a Gridview
<%@ Import Namespace=”System.IO” %>
<%@ Import Namespace=”System.Data” %>
<script language=”VB” Runat=”server”>
Dim dt As DataTable
Dim dr As DataRow
Sub Page_Load(Source as Object, E as EventArgs)
[...]

Read more