Note
This code was created by the UnifiedASP Code Generator with only the following modifications:
- Removed columns from this grid. The UnifiedASP Code Generator puts all columns from the source table in the Grid by default
- This was built using our Authenticated templates, but the search and detail screens are available for all users regardless of authentication. We changed the class the page code-behind inherits, and removed an if statement testing for the module-specific read permission. We made the same change to the Chamber/Detail.aspx.vb page.
- Changed Website URL field on Chamber/Detail.aspx from an asp:Label to an asp:HyperLink
Detailed instructions for how this was created are available at http://www.unifiedasp.net/index.php/unified-asp-code-generator-example-1/
Requirements
- Present all Chamber records in a table with the following columns: Chamber Name (link to detail screen), Address, City, State
- Table should be sortable by clicking on the column headers
- Table should show 50 records at a time and allow the user to page through them
- Table should be filterable by City or State
- Any filter selections will be retained so if the user navigates off the page and returns, the list will be filtered as it was left.
- If the user is logged in and has the proper credentials, an "Add New Chamber" link should be shown in the Search title bar
You can login to this demo through the Log In link above
Code is shown at bottom of page
Chamber/Default.aspx
<%@ Page Language="VB" AutoEventWireup="false" CodeFile="Default.aspx.vb" Inherits="Chamber_Default" MasterPageFile="~/Asset/MasterPage/UnifiedASP.master" %>
<%@ MasterType VirtualPath="~/Asset/MasterPage/UnifiedASP.master" %>
<asp:Content ID="Content1" ContentPlaceHolderID="cpPageTitle" runat="server">
Chamber
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="cpPageMain" runat="server">
<div class="box-large">
<div class="index">
<div class="title">
<div class="text">
Search
</div>
<div class="link" runat="server" id="divTitleLink">
<asp:HyperLink runat="server" ID="lnkAddNewChamber" Text="Add New Chamber" NavigateUrl="~/Chamber/Edit.aspx"></asp:HyperLink>
</div>
</div>
<div class="filter">
<table cellspacing="3" cellpadding="0" border="0">
<tr>
<td class="label">City</td>
<td class="label">State</td>
<td class="label"> </td>
</tr>
<tr>
<td><asp:TextBoxX id="txtCity" runat="server" />
<td><asp:DropDownList id="ddlStateId" runat="server" />
<td><asp:button id="btnSearch" runat="server" Text="Search"></asp:button></td>
</tr>
</table>
</div>
<asp:GridViewX runat="server" ID="grdChamber">
<columns>
<asp:HyperLinkField HeaderText="Chamber" DataTextField="ChamberName" SortExpression="ChamberName" DataNavigateUrlFields="ChamberId" DataNavigateUrlFormatString="~/Chamber/Detail.aspx?ChamberId={0}" />
<asp:BoundField HeaderText="Address" DataField="Address" SortExpression="Address" />
<asp:BoundField HeaderText="City" DataField="City" SortExpression="City" />
<asp:BoundField HeaderText="State" DataField="StateName" SortExpression="StateName" />
</columns>
</asp:GridViewX>
</div>
</div>
</asp:Content>
Chamber/Default.aspx.vb
Imports UnifiedASP.Utility
Partial Class Chamber_Default
Inherits UnifiedASP.Base.Page
Private myChamber As New USChamber.Domain.Chamber
Private myState As New USChamber.Domain.State
Private myCity As String
Private myStateId As Integer
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If IsGridEvent = True Then
DoLoadData()
End If
End Sub
Protected Sub Page_LoadComplete(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.LoadComplete
If IsGridEvent = False Then
DoLoadData()
End If
End Sub
Protected Sub Page_PreRender(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.PreRender
ToggleControl(ddlStateId, myState, "StateId", "StateName", myState.HasRecords, True, False, myStateId, 150)
divTitleLink.Visible = myPermission.HasPermission("Chamber.Create")
Form.DefaultButton = btnSearch.UniqueID
End Sub
Protected Sub btnSearch_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSearch.Click
DoClearSessionValues()
End Sub
Private Sub DoClearSessionValues()
SetSessionValue(txtCity.UniqueId, Nothing)
SetSessionValue(ddlStateId.UniqueId, Nothing)
End Sub
Private Sub DoLoadData()
myCity = GetInput(txtCity, True, CStr(Nothing))
myStateId = GetInput(ddlStateId.UniqueId, False, True, CInt(Nothing))
myChamber.City = myCity
myChamber.StateId = myStateId
myChamber.LoadAll()
myState.LoadAll
grdChamber.DataSource = myChamber
grdChamber.DataBind()
End Sub
End Class
Data-Access Class App_Code/Domain/Chamber.vb, exactly as generated by the UnifiedASP Code Generator
Imports UnifiedASP.Utility
Namespace USChamber.Domain
Public Class Chamber
Inherits UnifiedASP.Base.DomainSQL
Public ChamberId As Integer
Public ChamberName As String
Public Address As String
Public City As String
Public StateId As Integer
Public StateName As String
Public ZipCode As String
Public PhoneNumber As String
Public FaxNumber As String
Public WebsiteUrl As String
Public Sub LoadMe()
If IsBlank(ChamberId) = True Then
ClearAll()
Initialize()
Else
InitCommand("chChamber_Get")
AddParameter("ChamberId", ChamberId)
ExecCommand()
End If
End Sub
Public Sub LoadAll()
InitCommand("chChamber_GetAll")
AddParameter("City", 200, City)
AddParameter("StateId", StateId)
ExecCommand()
End Sub
Public Sub Save()
InitCommand("chChamber_Save")
AddParameter("ChamberId", ChamberId)
AddParameter("ChamberName", 200, ChamberName)
AddParameter("Address", 200, Address)
AddParameter("City", 200, City)
AddParameter("StateId", StateId)
AddParameter("ZipCode", 10, ZipCode)
AddParameter("PhoneNumber", 50, PhoneNumber)
AddParameter("FaxNumber", 50, FaxNumber)
AddParameter("WebsiteUrl", 200, WebsiteUrl)
ExecCommand()
End Sub
Public Sub Remove()
InitCommand("chChamber_Remove")
AddParameter("ChamberId", ChamberId)
ExecCommand()
End Sub
Public Overrides Sub Populate()
ChamberId = GetValue("ChamberId")
ChamberName = GetValue("ChamberName")
Address = GetValue("Address")
City = GetValue("City")
StateId = GetValue("StateId")
StateName = GetValue("StateName")
ZipCode = GetValue("ZipCode")
PhoneNumber = GetValue("PhoneNumber")
FaxNumber = GetValue("FaxNumber")
WebsiteUrl = GetValue("WebsiteUrl")
End Sub
Public Overrides Sub ClearValues()
ChamberId = Nothing
ChamberName = Nothing
Address = Nothing
City = Nothing
StateId = Nothing
StateName = Nothing
ZipCode = Nothing
PhoneNumber = Nothing
FaxNumber = Nothing
WebsiteUrl = Nothing
End Sub
End Class
End Namespace
chChamber_GetAll Stored Procedure, exactly as generated by the UnifiedASP Code Generator
CREATE PROCEDURE [dbo].[chChamber_GetAll]
( @City varchar(200) = null
, @StateId int = null
)
AS
BEGIN
set nocount on
select
c.ChamberId
, c.ChamberName
, c.Address
, c.City
, c.StateId
, s.StateName
, c.ZipCode
, c.PhoneNumber
, c.FaxNumber
, c.WebsiteUrl
from
Chamber c with (nolock)
join State s with (nolock) on s.StateId = c.StateId
where
(@City is null or c.City = @City)
and (@StateId is null or c.StateId = @StateId)
order by
c.ChamberName
END