partymps.aspxReturn to the Documentation page
  

functionsshowMPs
doFormatting
linkToOtherStatsMenu

‘Disable the viewstate at page level to improve the application's performance.’

<%@ Page Language="vb" EnableViewState="false" %>

‘Include the usual namespaces to enable a connection to be made to an Access 2000 database.’

<%@ import Namespace="System.Data" %>

<%@ import Namespace="System.Data.ODBC" %>

<script runat="server">

‘The showMPs function is run when the user clicks the ‘show MPs’ ASP button on the page.’

‘If the user has selected an item in the listbox [listBox1]….’

if listBox1.SelectedIndex >= 0 then

‘Define a string variable [Access2000] to hold the SELECT…FROM…WHERE…ORDER BY SQL statement. The statement below selects all the MPs who are members of the selected party – listBox1.SelectedItem.Text. They are sorted by the id field when they appear on the web page. The [firstvotes]-[secondvotes] calculated expression produces the majority for the datagrid [gridMP].’

Dim Access2000 as string = "Select fullresults.*, [firstvotes]-[secondvotes] as majority from fullresults where winningparty='" & listBox1.SelectedItem.Text & "' order by id"

‘Declare a variable [MyConn} of type ODBCConnection which takes the strConn connection string as a parameter, the value of which is fetched from the web.config file.’

Dim MyConn as New ODBCConnection(ConfigurationSettings.AppSettings("strConn"))

‘Declare a new DataSet object [ds] which will hold the data from the query above.’

Dim ds as DataSet=New DataSet()

‘Declare a new ODBCDataAdapter object [Cmd] with the parameters Access2000 and MyConn. This will pull the data from the election2001.mdb Access 2000 database and put it into the dataset [ds].’

Dim Cmd as New ODBCDataAdapter(Access2000,MyConn)

‘Fill the dataset and call the table created ‘mps’.’

Cmd.Fill(ds,"mps")

‘Set the datasource property of the DataGrid web control [gridMP] to the newly created dataset [ds].’

gridMP.Datasource=ds

‘Bind the data to the control on the web page.’

gridMP.DataBind()

‘Declare a string variable [party] which holds the party that the user selected in the listbox [listBox1].’

Dim party as string = listBox1.SelectedItem.Text

‘Use a SELECT..CASE statement which will change the text property of the ASP label control [partyLabel] depending on the party selected.’

select case party

‘If Labour is the party selected by the user then the partyLabel control will display the text ‘Labour MPs. Similarly for the other parties….’

Case "Labour"

partyLabel.Text = "Labour MPs"

Case "Conservative"

partyLabel.Text = "Conservative MPs"

Case "Liberal Democrats"

partyLabel.Text = "Liberal Democrat MPs"

Case "SNP"

partyLabel.Text = "Scottish National Party"

Case "Plaid Cymru"

partyLabel.Text = "Plaid Cymru"

Case "UUP"

partyLabel.Text = "Ulster Unionists"

Case "DUP"

partyLabel.Text = "Democratic Unionists"

Case "Sinn Fein"

partyLabel.Text = "Sinn Fein MPs"

Case "SDLP"

partyLabel.Text = "Social & Democratic Labour Party"

Case "UKU"

partyLabel.Text = "UK Unionists"

Case "Independent"

partyLabel.Text = "Independents"

end select

‘However, if there are no MPs for the party selected then…..’

if gridMP.items.count = 0 then

‘Display the appropriate text message in the partyLabel web control.’

partyLabel.Text = "No MPs for this party"

‘Set the text property of the ASP label control [countLabel] to blank.’

countLabel.Text = ""

‘Otherwise….’

else

‘Set the text property of countLabel to the number of MPs for that party using the .items.count property of the DataGrid web control [gridMP]. This number will appear inside brackets to the right of the partyLabel web control.’

countLabel.Text = " (" & gridMP.items.count & ")"

end if

‘Create session variables to hold the selected party, the contents of the partyLabel web control and the dataset itself, which will be used by showMP.aspx, if the user clicks on the ‘show details’ column.’

Session("picked") = listBox1.selectedItem.Text

Session("subsetLabel") = partyLabel.Text

Session("subsetDataSet") = ds

‘Tidy up and close the connection object.’

Cmd.Dispose

MyConn.Close

end if

‘The doFormatting function is run from the OnItemDataBound event of the DataGrid web control [gridMP].’

‘If the contents of the Constituency field is greater than 30 characters then….’

if e.Item.Cells(1).Text.Length > 30 then

‘….assign the cssClass property to 'lowerfont'. This style is defined within the onload.js file - see below and the documentation for this javascript file for the relevant coding.’

e.Item.Cells(1).cssClass = "lowerfont"

end if

‘If the contents of the winningparty field is greater than 15 characters then….’

if e.Item.Cells(4).Text.Length > 15 then

‘….assign the cssClass property to 'lowerfont'.’

e.Item.Cells(4).cssClass = "lowerfont"

end if

‘If the contents of the result field is greater than 15 characters then….’

if e.Item.Cells(5).Text.Length > 15 then

‘….assign the cssClass property to 'lowerfont'.’

e.Item.Cells(5).cssClass = "lowerfont"

end if

‘The usual script to redirect the user back to the other stats menu using an ASP linkbutton control.’

Response.Redirect("otherstats.aspx")

</script>

<html>

<head>

<title>View a party's MPs</title>

<link href=".\otherfiles\election2001.css" type="text/css" rel="stylesheet">

<script type="text/Javascript" src=".\otherfiles\onload.js"></script><style> </style>

</head>

‘The init function runs when the page first loads on the user's machine. The function is contained within the onload.js file which is loaded automatically by the <script> tag above - see the onload.js file for further details.’

<body bgcolor="white" onload="init()">

<form id="form1" method="post" runat="server">

‘The first table displays an image of a ballot box [ballotbox.jpg] in the top left-hand corner together with a label [View a party’s MPs] and the ASP linkbutton control mentioned above displays in the top right-hand corner of the page. This runs the linkToOtherStatsMenu function when clicked.’

<table width="100%">

<tr>

<td rowspan="3" width="85px" valign="top"><img src="./images/ballotbox.jpg" title="Cast your vote wisely!"></td>

<td><asp:Label id="Label1" runat="server"><u>View a party's MPs</u></asp:Label></td>

<td align="right"><asp:LinkButton id="LinkToEdit" onclick="linkToOtherStatsMenu" runat="server" Text="Return to the Other stats menu"></asp:LinkButton></td>

</tr>

<tr><td colspan="2">&nbsp;</td></tr>

‘Underneath, in the second row of the table, is an ASP listbox [listBox1] displaying the major parties of the British Isles – Labour, Conservative, etc. The user selects a party and then clicks the ASP button [btnLoadMPs] to the right which runs the showMPs function already discussed above.’

<tr>

<td>Select a party to see its MPs:

<p>

<asp:ListBox id="listBox1" runat="server" size="1">

<asp:ListItem>Labour</asp:ListItem>

<asp:ListItem>Conservative</asp:ListItem>

<asp:ListItem>Liberal Democrats</asp:ListItem>

<asp:ListItem>SNP</asp:ListItem>

<asp:ListItem>Plaid Cymru</asp:ListItem>

<asp:ListItem>UUP</asp:ListItem>

<asp:ListItem>DUP</asp:ListItem>

<asp:ListItem>Sinn Fein</asp:ListItem>

<asp:ListItem>SDLP</asp:ListItem>

<asp:ListItem>UKU</asp:ListItem>

<asp:ListItem>Independent</asp:ListItem>

<asp:ListItem>Green</asp:ListItem>

<asp:ListItem>UK Ind</asp:ListItem>

</asp:ListBox>

</td>

<td><asp:Button id="btnLoadMPs" onclick="showMPs" runat="server" Text="Show MPs"></asp:Button></td>

</tr>

</table>

<p>

‘When the users selects a party and clicks the ‘Show MPs’ button’ the results are displayed in the following datagrid [gridMP] on the web page. Just above the datagrid are two ASP label controls which display a party label [partyLabel] and the number of MPs [countLabel]. These are formatted as <H2> on the page and act as a heading for the contents of the datagrid.’

<center><H2><asp:Label runat="server" id="partyLabel"></asp:Label><asp:Label runat="server" id="countLabel"></asp:Label></H2><P>

<asp:Datagrid id="gridMP" cssClass="gridMP" Runat="server" AutoGenerateColumns="False" AllowPaging="false" BorderColor="Black" AlternatingItemStyle-Font-Name="Arial" AlternatingItemStyle-BackColor="#E7EFFF" Font-Name="Arial" BackColor="#E7EFFF" Headerstyle-Font-Size="12" Headerstyle-Font-Name="Arial" Headerstyle-ForeColor="blue" Headerstyle-BackColor="#BDCFE7" OnItemDataBound="doFormatting" cellspacing="0" cellpadding="4" GridLines="Both">

‘There are six bound columns, id, constituency, first, majority, winningparty and result. The majority calculated field produces the winning MP's majority which is formatted using the DataFormatString="{0:#,###}" – this produces a thousands separator and makes the numbers easier to read. The final column is a hyperlink column [See details].’

<columns>

<asp:BoundColumn HeaderText="" DataField="id" ItemStyle-HorizontalAlign="right" />

<asp:BoundColumn HeaderText="Constituency" DataField="constituency" />

<asp:BoundColumn HeaderText="MP" DataField="first" />

<asp:BoundColumn HeaderText="Majority" ItemStyle-HorizontalAlign="right" DataFormatString="{0:#,###}" DataField="majority" HeaderStyle-HorizontalAlign="right" />

<asp:BoundColumn HeaderText="Winners" DataField="winningparty" />

<asp:BoundColumn HeaderText="Result" DataField="result" />

‘When the user clicks ‘Show details’ a new window is opened using javascript - the window.open method. It opens the file showMP.aspx with all the details of the selected constituency displayed. The window will appear 10 pixels down from the top left-hand corner of the window. Go back to the Documentation menu to see the coding for the showMP.aspx file.’

<asp:hyperlinkColumn HeaderText="See details" DataTextFormatString="Show details" DataTextField="id" HeaderStyle-HorizontalAlign="center" ItemStyle-HorizontalAlign="center" DataNavigateUrlField="id" DataNavigateUrlFormatString="javascript:var w=window.open('showMP.aspx?id={0}',null,'left=10,top=10,scrollbars');" />

</columns>

</asp:Datagrid>

</center>

</form>

</body>

</html>

***********************************************