Update: Post #19 offers a Stored Procedure and a (part 2+) Sharepoint Webpart code for an employee directory.
Note: This uses asp.net 1.1 framework – should migrate to 2.0+ just fine but has not been tested on the newer frameworks. I plan to replace the data calls using the DAAB see Post #04. It should at least show you how easy you can filter and post to a grid.
As I spend time & convert this over to use a better data access layer I will repost but as is should give you an idea of what to do (if you know asp.net).
First here is what it looks like:
asp.net
<asp:dropdownlist id=”dlRecordsPerPage” runat=”server” AutoPostBack=”True”>
<asp:ListItem Value=”25″ Selected=”True”>25</asp:ListItem>
<asp:ListItem Value=”50″>50</asp:ListItem>
<asp:ListItem Value=”100″>100</asp:ListItem>
<asp:ListItem Value=”500″>500</asp:ListItem>
</asp:dropdownlist>
<asp:button id=”ExportToExcel” runat=”server” CssClass=”b1″ Visible=”False” Text=” Export to Excel”></asp:button>
<asp:datagrid id=”DataGrid1″ Width=”680px” BorderStyle=”None” Height=”20px” OnSortCommand=”DataGrid1_SortCommand”
AllowSorting=”True” OnPageIndexChanged=”DataGrid1_PageIndexChanged” Runat=”Server” PageSize=”25″
GridLines=”Vertical” CellPadding=”3″ BorderWidth=”1px” AllowPaging=”True” BorderColor=”#999999″
BackColor=”White” AutoGenerateColumns=”False”>
<SelectedItemStyle Font-Bold=”True” ForeColor=”White” BackColor=”#008A8C”></SelectedItemStyle>
<AlternatingItemStyle Height=”20px” BackColor=”Gainsboro”></AlternatingItemStyle>
<ItemStyle Height=”20px” ForeColor=”Black” BackColor=”#EEEEEE”></ItemStyle>
<HeaderStyle Font-Bold=”True” HorizontalAlign=”Center” Height=”40px” ForeColor=”White” BackColor=”#000084″></HeaderStyle>
<FooterStyle Height=”20px” ForeColor=”Black” BackColor=”#CCCCCC”></FooterStyle>
<Columns>
<asp:BoundColumn DataField=”PersonID” HeaderText=”Emp ID” SortExpression=”A.Employee”>
<ItemStyle Wrap=”False” HorizontalAlign=”Center” Width=”40px” CssClass=”smallAlert”></ItemStyle>
</asp:BoundColumn>
<asp:HyperLinkColumn Text=”<img border=’0′ src=’/newnet/images/intranet/person.gif’ />” Target=”_self”
DataNavigateUrlField=”PersonID” DataNavigateUrlFormatString=”http://intranet/newnet/resources/EmployeeView.aspx?id={0}”
HeaderText=”View ” Visible=”false”>
<ItemStyle CssClass=”smallredC”></ItemStyle>
</asp:HyperLinkColumn>
<asp:HyperLinkColumn Text=”<img border=’0′ src=’/newnet/images/intranet/email.gif’ />” DataNavigateUrlField=”Email”
DataNavigateUrlFormatString=”mailto:{0}” HeaderText=”Email”>
<ItemStyle HorizontalAlign=”Center” Width=”100px” CssClass=”smallredC”></ItemStyle>
</asp:HyperLinkColumn>
<asp:HyperLinkColumn Text=”<img border=’0′ src=’/newnet/images/intranet/person.gif’ />” DataNavigateUrlField=”Employee”
DataNavigateUrlFormatString=”http://intranet/newnet/images/emp/{0}.jpg” HeaderText=”Photo”>
<ItemStyle HorizontalAlign=”Center” Width=”100px” CssClass=”smallredC”></ItemStyle>
</asp:HyperLinkColumn>
<asp:BoundColumn DataField=”FirstName” HeaderText=”First Name” SortExpression=”FirstName”>
<ItemStyle Wrap=”False” Width=”90px” CssClass=”smallred”></ItemStyle>
</asp:BoundColumn>
<asp:BoundColumn DataField=”LastName” HeaderText=”Last Name” SortExpression=”LastName”>
<ItemStyle Wrap=”False” Width=”90px” CssClass=”smallred”></ItemStyle>
</asp:BoundColumn>
<asp:BoundColumn DataField=”WorkPhoneExt” HeaderText=”Ext”>
<ItemStyle Wrap=”False” Width=”35px” CssClass=”smalldetail”></ItemStyle>
</asp:BoundColumn>
<asp:BoundColumn DataField=”WorkPhone” HeaderText=”Direct”>
<ItemStyle Wrap=”False” Width=”65px” CssClass=”smalldetail”></ItemStyle>
</asp:BoundColumn>
<asp:BoundColumn DataField=”MobilePhone” HeaderText=”Mobile”>
<ItemStyle Wrap=”False” Width=”75px” CssClass=”smalldetail”></ItemStyle>
</asp:BoundColumn>
<asp:BoundColumn DataField=”Org” HeaderText=”Primary Dept” SortExpression=”Org”>
<ItemStyle Wrap=”False” CssClass=”smallredC”></ItemStyle>
</asp:BoundColumn>
<asp:BoundColumn DataField=”custSecondaryDepartment” HeaderText=”Secondary Dept” SortExpression=”C.custSecondaryDepartment”>
<ItemStyle Wrap=”False” CssClass=”smallredC”></ItemStyle>
</asp:BoundColumn>
<asp:BoundColumn DataField=”Office” HeaderText=”Office” SortExpression=”B.Cust5ULPhysical”>
<ItemStyle Wrap=”False” CssClass=”smallredC”></ItemStyle>
</asp:BoundColumn>
</Columns>
<PagerStyle Height=”20px” HorizontalAlign=”Center” ForeColor=”Black” BackColor=”#999999″ Mode=”NumericPages”></PagerStyle>
</asp:datagrid>
C#:
Note: The embedded query should be cleaned up using join clauses and the should be replaced by a stored procedure perhaps using WHEN CASE to ignore unused filter parms.
public void BindDataGrid( string sOrderParm )
{
SqlDataAdapter daList;
DataSet dsClients;
SqlConnection conn = new SqlConnection(connstring);
conn.Open();
string strDeptCode = “%” + dlDepartment.SelectedValue.ToString() + “%”;
string strRegionCode = “%” + dlRegion.SelectedValue.ToString() + “%”;
string strSQL = ” Select A.Employee , (RTRIM(LastName) + ‘ ‘ + ISNULL(Suffix,”)) as LastName , PreferredName As FirstName, MiddleName, Org, Region, WorkPhone, WorkPhoneExt, MobilePhone, B.Cust5ULPhysical As Office, C.custSecondaryDepartment “;
strSQL += ” , RTRIM(A.Employee) As PersonID, A.Email “;
strSQL += ” FROM EM A , EmployeeCustomTabFields B, EmployeeCustomTabFields C “;
strSQL += ” WHERE A.Status = ‘A’ “;
strSQL += ” AND A.Employee = B.Employee “;
strSQL += ” AND A.Employee = C.Employee “;
strSQL += ” AND A.[LastName] Like ‘” + txtLastName.Text.Trim() + “%’ And A.[Employee] Like ‘” + txtEmpID.Text.Trim() + “%’ And ( A.[PreferredName] Like ‘” + txtFirstName.Text.Trim() + “%’ OR A.[FirstName] like ‘” + txtFirstName.Text.Trim() + “%’ ) AND ( A.[Org] like ‘” + strDeptCode + “%’ or C.custSecondaryDepartment like ‘%” + strDeptCode + “%’ ) AND ( A.WorkPhone like ‘%” + txtPhone.Text.Trim() + “%’ OR A.WorkPhoneExt like ‘%” + txtPhone.Text.Trim() + “%’ OR A.MobilePhone like ‘%” + txtPhone.Text.Trim() + “%’) “;
strSQL += ” AND B.Cust5ULPhysical like ‘%” + strRegionCode + “%’ “;
strSQL += ” Order By ” + sOrderParm;
daList = new SqlDataAdapter(strSQL, conn );
dsClients = new DataSet();
daList.Fill( dsClients );
DataGrid1.PageSize = Int32.Parse(dlRecordsPerPage.SelectedValue.ToString());
DataGrid1.DataSource = dsClients;
try
{
DataGrid1.DataBind();
if (DataGrid1.Items.Count == 0)
{
txtError.Text = “No Records Found”;
txtError.Visible = true;
DataGrid1.Visible = false;
}
else
{
ExportToExcel.Visible = true;
DataGrid1.Visible = true;
txtError.Visible = false;
txtTest.Visible = false;
}
}
catch
{
txtError.Text = “No Records Found”;
txtError.Visible = true;
DataGrid1.Visible = false;
}
finally
{
if (conn != null) { conn.Close(); conn.Dispose(); }
if (daList != null) { daList.Dispose(); }
if (dsClients != null) { dsClients.Dispose(); }
}
}
