ASP.NET Podcast Show #148 - ASP.NET WebForms to build a Mobile Web Application
Posted
by Wallym
on ASP.net Weblogs
See other posts from ASP.net Weblogs
or by Wallym
Published on Mon, 06 Dec 2010 12:00:00 GMT
Indexed on
2010/12/06
16:57 UTC
Read the original article
Hit count: 1481
Check the podcast site for the original url.
This is the video and source code for an ASP.NET WebForms app that I wrote that is optimized for the iPhone and mobile environments.
Subscribe to M4V for iPhone/iPad.
Source Code:
<%@ Page Title="MapSplore" Language="C#" MasterPageFile="iPhoneMaster.master" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="AT_iPhone_Default" %>
<asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="Content" Runat="Server" ClientIDMode="Static">
<asp:ScriptManager ID="sm" runat="server"
EnablePartialRendering="true" EnableHistory="false" EnableCdn="true" />
<script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=true"></script>
<script language="javascript" type="text/javascript">
<!--
Sys.WebForms.PageRequestManager.getInstance().add_endRequest(endRequestHandle);
function endRequestHandle(sender, Args) {
setupMapDiv();
setupPlaceIveBeen();
}
function setupPlaceIveBeen() {
var mapPlaceIveBeen = document.getElementById('divPlaceIveBeen');
if (mapPlaceIveBeen != null) {
var PlaceLat = document.getElementById('<%=hdPlaceIveBeenLatitude.ClientID %>').value;
var PlaceLon = document.getElementById('<%=hdPlaceIveBeenLongitude.ClientID %>').value;
var PlaceTitle = document.getElementById('<%=lblPlaceIveBeenName.ClientID %>').innerHTML;
var latlng = new google.maps.LatLng(PlaceLat, PlaceLon);
var myOptions = {
zoom: 14,
center: latlng,
mapTypeId: google.maps.MapTypeId.ROADMAP
};
var map = new google.maps.Map(mapPlaceIveBeen, myOptions);
var marker = new google.maps.Marker({
position: new google.maps.LatLng(PlaceLat, PlaceLon),
map: map,
title: PlaceTitle,
clickable: false
});
}
}
function setupMapDiv() {
var mapdiv = document.getElementById('divImHere');
if (mapdiv != null) {
var PlaceLat = document.getElementById('<%=hdPlaceLat.ClientID %>').value;
var PlaceLon = document.getElementById('<%=hdPlaceLon.ClientID %>').value;
var PlaceTitle = document.getElementById('<%=hdPlaceTitle.ClientID %>').value;
var latlng = new google.maps.LatLng(PlaceLat, PlaceLon);
var myOptions = {
zoom: 14,
center: latlng,
mapTypeId: google.maps.MapTypeId.ROADMAP
};
var map = new google.maps.Map(mapdiv, myOptions);
var marker = new google.maps.Marker({
position: new google.maps.LatLng(PlaceLat, PlaceLon),
map: map,
title: PlaceTitle,
clickable: false
});
}
}
-->
</script>
<asp:HiddenField ID="Latitude" runat="server" />
<asp:HiddenField ID="Longitude" runat="server" />
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js%22%3E%3C/script>
<script language="javascript" type="text/javascript">
$(document).ready(function () {
GetLocation();
setupMapDiv();
setupPlaceIveBeen();
});
function GetLocation() {
if (navigator.geolocation != null) {
navigator.geolocation.getCurrentPosition(getData);
}
else {
var mess = document.getElementById('<%=Message.ClientID %>');
mess.innerHTML = "Sorry, your browser does not support geolocation. " +
"Try the latest version of Safari on the iPhone, Android browser, or the latest version of FireFox.";
}
}
function UpdateLocation_Click() {
GetLocation();
}
function getData(position) {
var latitude = position.coords.latitude;
var longitude = position.coords.longitude;
var hdLat = document.getElementById('<%=Latitude.ClientID %>');
var hdLon = document.getElementById('<%=Longitude.ClientID %>');
hdLat.value = latitude;
hdLon.value = longitude;
}
</script>
<asp:Label ID="Message" runat="server" />
<asp:UpdatePanel ID="upl" runat="server">
<ContentTemplate>
<asp:Panel ID="pnlStart" runat="server" Visible="true">
<div id="topbar">
<div id="title">MapSplore</div>
</div>
<div id="content">
<ul class="pageitem">
<li class="menu">
<asp:LinkButton ID="lbLocalDeals" runat="server" onclick="lbLocalDeals_Click">
<asp:Image ID="imLocalDeals" runat="server" ImageUrl="~/Images/ArtFavor_Money_Bag_Icon.png" Height="30" />
<span class="name">Local Deals.</span>
<span class="arrow"></span>
</asp:LinkButton>
</li>
<li class="menu">
<asp:LinkButton ID="lbLocalPlaces" runat="server" onclick="lbLocalPlaces_Click">
<asp:Image ID="imLocalPlaces" runat="server" ImageUrl="~/Images/Andy_Houses_on_the_horizon_-_Starburst_remix.png" Height="30" />
<span class="name">Local Places.</span>
<span class="arrow"></span>
</asp:LinkButton>
</li>
<li class="menu">
<asp:LinkButton ID="lbWhereIveBeen" runat="server" onclick="lbWhereIveBeen_Click">
<asp:Image ID="imImHere" runat="server" ImageUrl="~/Images/ryanlerch_flagpole.png" Height="30" />
<span class="name">I've been here.</span>
<span class="arrow"></span>
</asp:LinkButton>
</li>
<li class="menu">
<asp:LinkButton ID="lbMyStats" runat="server">
<asp:Image ID="imMyStats" runat="server" ImageUrl="~/Images/Anonymous_Spreadsheet.png" Height="30" />
<span class="name">My Stats.</span>
<span class="arrow"></span>
</asp:LinkButton>
</li>
<li class="menu">
<asp:LinkButton ID="lbAddAPlace" runat="server" onclick="lbAddAPlace_Click">
<asp:Image ID="imAddAPlace" runat="server" ImageUrl="~/Images/jean_victor_balin_add.png" Height="30" />
<span class="name">Add a Place.</span>
<span class="arrow"></span>
</asp:LinkButton>
</li>
<li class="button">
<input type="button" value="Update Your Current Location" onclick="UpdateLocation_Click()">
</li>
</ul>
</div>
</asp:Panel>
<div>
<asp:Panel ID="pnlCoupons" runat="server" Visible="false">
<div id="topbar">
<div id="title">MapSplore</div>
<div id="leftbutton">
<asp:LinkButton runat="server" Text="Return"
ID="ReturnFromDeals" OnClick="ReturnFromDeals_Click" /></div></div>
<div class="content">
<asp:ListView ID="lvCoupons" runat="server">
<LayoutTemplate>
<ul class="pageitem" runat="server">
<asp:PlaceHolder ID="itemPlaceholder" runat="server" />
</ul>
</LayoutTemplate>
<ItemTemplate>
<li class="menu">
<asp:LinkButton ID="lbBusiness" runat="server" Text='<%#Eval("Place.Name") %>' OnClick="lbBusiness_Click">
<span class="comment">
<asp:Label ID="lblAddress" runat="server" Text='<%#Eval("Place.Address1") %>' />
<asp:Label ID="lblDis" runat="server" Text='<%# Convert.ToString(Convert.ToInt32(Eval("Place.Distance"))) + " meters" %>' CssClass="smallText" />
<asp:HiddenField ID="hdPlaceId" runat="server" Value='<%#Eval("PlaceId") %>' />
<asp:HiddenField ID="hdGeoPromotionId" runat="server" Value='<%#Eval("GeoPromotionId") %>' />
</span>
<span class="arrow"></span>
</asp:LinkButton></li></ItemTemplate></asp:ListView><asp:GridView ID="gvCoupons" runat="server" AutoGenerateColumns="false">
<HeaderStyle BackColor="Silver" />
<AlternatingRowStyle BackColor="Wheat" />
<Columns>
<asp:TemplateField AccessibleHeaderText="Business" HeaderText="Business">
<ItemTemplate>
<asp:Image ID="imPlaceType" runat="server" Text='<%#Eval("Type") %>' ImageUrl='<%#Eval("Image") %>' />
<asp:LinkButton ID="lbBusiness" runat="server" Text='<%#Eval("Name") %>' OnClick="lbBusiness_Click" />
<asp:LinkButton ID="lblAddress" runat="server" Text='<%#Eval("Address1") %>' CssClass="smallText" />
<asp:Label ID="lblDis" runat="server" Text='<%# Convert.ToString(Convert.ToInt32(Eval("Distance"))) + " meters" %>' CssClass="smallText" />
<asp:HiddenField ID="hdPlaceId" runat="server" Value='<%#Eval("PlaceId") %>' />
<asp:HiddenField ID="hdGeoPromotionId" runat="server" Value='<%#Eval("GeoPromotionId") %>' />
<asp:Label ID="lblInfo" runat="server" Visible="false" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</div>
</asp:Panel>
<asp:Panel ID="pnlPlaces" runat="server" Visible="false">
<div id="topbar">
<div id="title">
MapSplore</div><div id="leftbutton">
<asp:LinkButton runat="server" Text="Return"
ID="ReturnFromPlaces" OnClick="ReturnFromPlaces_Click" /></div></div>
<div id="content">
<asp:ListView ID="lvPlaces" runat="server">
<LayoutTemplate>
<ul id="ulPlaces" class="pageitem" runat="server">
<asp:PlaceHolder ID="itemPlaceholder" runat="server" />
<li class="menu">
<asp:LinkButton ID="lbNotListed" runat="server" CssClass="name"
OnClick="lbNotListed_Click">
Place not listed
<span class="arrow"></span>
</asp:LinkButton>
</li>
</ul>
</LayoutTemplate>
<ItemTemplate>
<li class="menu">
<asp:LinkButton ID="lbImHere" runat="server" CssClass="name"
OnClick="lbImHere_Click">
<%#DisplayName(Eval("Name")) %>
<%# Convert.ToString(Convert.ToInt32(Eval("Distance"))) + " meters" %>
<asp:HiddenField ID="hdPlaceId" runat="server" Value='<%#Eval("PlaceId") %>' />
<span class="arrow"></span>
</asp:LinkButton></li></ItemTemplate></asp:ListView>
</div>
</asp:Panel>
<asp:Panel ID="pnlImHereNow" runat="server" Visible="false">
<div id="topbar">
<div id="title">
MapSplore</div><div id="leftbutton">
<asp:LinkButton runat="server" Text="Places"
ID="lbImHereNowReturn" OnClick="lbImHereNowReturn_Click" /></div></div>
<div id="rightbutton">
<asp:LinkButton runat="server" Text="Beginning"
ID="lbBackToBeginning" OnClick="lbBackToBeginning_Click" />
</div>
<div id="content">
<ul class="pageitem">
<asp:HiddenField ID="hdPlaceId" runat="server" />
<asp:HiddenField ID="hdPlaceLat" runat="server" />
<asp:HiddenField ID="hdPlaceLon" runat="server" />
<asp:HiddenField ID="hdPlaceTitle" runat="server" />
<asp:Button ID="btnImHereNow" runat="server"
Text="I'm here" OnClick="btnImHereNow_Click" />
<asp:Label ID="lblPlaceTitle" runat="server" /><br />
<asp:TextBox ID="txtWhatsHappening" runat="server" TextMode="MultiLine" Rows="2" style="width:300px" /><br />
<div id="divImHere" style="width:300px; height:300px"></div>
</div>
</ul>
</asp:Panel>
<asp:Panel runat="server" ID="pnlIveBeenHere" Visible="false">
<div id="topbar">
<div id="title">
Where I've been</div><div id="leftbutton">
<asp:LinkButton ID="lbIveBeenHereBack" runat="server" Text="Back" OnClick="lbIveBeenHereBack_Click" /></div></div>
<div id="content">
<asp:ListView ID="lvWhereIveBeen" runat="server">
<LayoutTemplate>
<ul id="ulWhereIveBeen" class="pageitem" runat="server">
<asp:PlaceHolder ID="itemPlaceholder" runat="server" />
</ul>
</LayoutTemplate>
<ItemTemplate>
<li class="menu" runat="server">
<asp:LinkButton ID="lbPlaceIveBeen" runat="server" OnClick="lbPlaceIveBeen_Click" CssClass="name">
<asp:Label ID="lblPlace" runat="server" Text='<%#Eval("PlaceName") %>' /> at
<asp:Label ID="lblTime" runat="server" Text='<%#Eval("ATTime") %>' CssClass="content" />
<asp:HiddenField ID="hdATID" runat="server" Value='<%#Eval("ATID") %>' />
<span class="arrow"></span>
</asp:LinkButton>
</li>
</ItemTemplate>
</asp:ListView>
</div>
</asp:Panel>
<asp:Panel runat="server" ID="pnlPlaceIveBeen" Visible="false">
<div id="topbar">
<div id="title">
I've been here
</div>
<div id="leftbutton">
<asp:LinkButton ID="lbPlaceIveBeenBack" runat="server" Text="Back" OnClick="lbPlaceIveBeenBack_Click" />
</div>
<div id="rightbutton">
<asp:LinkButton ID="lbPlaceIveBeenBeginning" runat="server" Text="Beginning" OnClick="lbPlaceIveBeenBeginning_Click" />
</div>
</div>
<div id="content">
<ul class="pageitem">
<li>
<asp:HiddenField ID="hdPlaceIveBeenPlaceId" runat="server" />
<asp:HiddenField ID="hdPlaceIveBeenLatitude" runat="server" />
<asp:HiddenField ID="hdPlaceIveBeenLongitude" runat="server" />
<asp:Label ID="lblPlaceIveBeenName" runat="server" /><br />
<asp:Label ID="lblPlaceIveBeenAddress" runat="server" /><br />
<asp:Label ID="lblPlaceIveBeenCity" runat="server" />,
<asp:Label ID="lblPlaceIveBeenState" runat="server" />
<asp:Label ID="lblPlaceIveBeenZipCode" runat="server" /><br />
<asp:Label ID="lblPlaceIveBeenCountry" runat="server" /><br />
<div id="divPlaceIveBeen" style="width:300px; height:300px"></div>
</li>
</ul>
</div>
</asp:Panel>
<asp:Panel ID="pnlAddPlace" runat="server" Visible="false">
<div id="topbar">
<div id="title">MapSplore</div><div id="leftbutton">
<asp:LinkButton ID="lbAddPlaceReturn" runat="server" Text="Back" OnClick="lbAddPlaceReturn_Click" /></div><div id="rightnav">
</div>
</div>
<div id="content">
<ul class="pageitem">
<li id="liPlaceAddMessage" runat="server" visible="false">
<asp:Label ID="PlaceAddMessage" runat="server" />
</li>
<li class="bigfield">
<asp:TextBox ID="txtPlaceName" runat="server" placeholder="Name of Establishment" />
</li>
<li class="bigfield">
<asp:TextBox ID="txtAddress1" runat="server" placeholder="Address 1" />
</li>
<li class="bigfield">
<asp:TextBox ID="txtCity" runat="server" placeholder="City" />
</li>
<li class="select">
<asp:DropDownList ID="ddlProvince" runat="server" placeholder="Select State" />
<span class="arrow"></span>
</li>
<li class="bigfield">
<asp:TextBox ID="txtZipCode" runat="server" placeholder="Zip Code" />
</li>
<li class="select">
<asp:DropDownList ID="ddlCountry" runat="server"
onselectedindexchanged="ddlCountry_SelectedIndexChanged" />
<span class="arrow"></span>
</li>
<li class="bigfield">
<asp:TextBox ID="txtPhoneNumber" runat="server" placeholder="Phone Number" />
</li>
<li class="checkbox">
<span class="name">You Here Now:</span> <asp:CheckBox ID="cbYouHereNow" runat="server" Checked="true" />
</li>
<li class="button">
<asp:Button ID="btnAdd" runat="server" Text="Add Place"
onclick="btnAdd_Click" />
</li>
</ul>
</div>
</asp:Panel>
<asp:Panel ID="pnlImHere" runat="server" Visible="false">
<asp:TextBox ID="txtImHere" runat="server"
TextMode="MultiLine" Rows="3" Columns="40" /><br />
<asp:DropDownList ID="ddlPlace" runat="server" /><br />
<asp:Button ID="btnHere" runat="server" Text="Tell Everyone I'm Here"
onclick="btnHere_Click" /><br />
</asp:Panel>
</div>
</ContentTemplate>
</asp:UpdatePanel>
</asp:Content>
Code Behind .cs file:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using LocationDataModel;
public partial class AT_iPhone_Default : ViewStatePage
{
private iPhoneDevice ipd;
protected void Page_Load(object sender, EventArgs e)
{
LocationDataEntities lde = new LocationDataEntities();
if (!Page.IsPostBack)
{
var Countries = from c in lde.Countries select c;
foreach (Country co in Countries)
{
ddlCountry.Items.Add(new ListItem(co.Name, co.CountryId.ToString()));
}
ddlCountry_SelectedIndexChanged(ddlCountry, null);
if (AppleIPhone.IsIPad())
ipd = iPhoneDevice.iPad;
if (AppleIPhone.IsIPhone())
ipd = iPhoneDevice.iPhone;
if (AppleIPhone.IsIPodTouch())
ipd = iPhoneDevice.iPodTouch;
}
}
protected void btnPlaces_Click(object sender, EventArgs e)
{
}
protected void btnAdd_Click(object sender, EventArgs e)
{
bool blImHere = cbYouHereNow.Checked;
string Place = txtPlaceName.Text,
Address1 = txtAddress1.Text,
City = txtCity.Text,
ZipCode = txtZipCode.Text,
PhoneNumber = txtPhoneNumber.Text,
ProvinceId = ddlProvince.SelectedItem.Value,
CountryId = ddlCountry.SelectedItem.Value;
int iProvinceId, iCountryId;
double dLatitude, dLongitude;
DataAccess da = new DataAccess();
if ((!String.IsNullOrEmpty(ProvinceId)) &&
(!String.IsNullOrEmpty(CountryId)))
{
iProvinceId = Convert.ToInt32(ProvinceId);
iCountryId = Convert.ToInt32(CountryId);
if (blImHere)
{
dLatitude = Convert.ToDouble(Latitude.Value);
dLongitude = Convert.ToDouble(Longitude.Value);
da.StorePlace(Place, Address1, String.Empty, City,
iProvinceId, ZipCode, iCountryId, PhoneNumber,
dLatitude, dLongitude);
}
else
{
da.StorePlace(Place, Address1, String.Empty, City,
iProvinceId, ZipCode, iCountryId, PhoneNumber);
}
liPlaceAddMessage.Visible = true;
PlaceAddMessage.Text = "Awesome, your place has been added. Add Another!";
txtPlaceName.Text = String.Empty;
txtAddress1.Text = String.Empty;
txtCity.Text = String.Empty;
ddlProvince.SelectedIndex = -1;
txtZipCode.Text = String.Empty;
txtPhoneNumber.Text = String.Empty;
}
else
{
liPlaceAddMessage.Visible = true;
PlaceAddMessage.Text = "Please select a State and a Country.";
}
}
protected void ddlCountry_SelectedIndexChanged(object sender, EventArgs e)
{
string CountryId = ddlCountry.SelectedItem.Value;
if (!String.IsNullOrEmpty(CountryId))
{
int iCountryId = Convert.ToInt32(CountryId);
LocationDataModel.LocationDataEntities lde = new LocationDataModel.LocationDataEntities();
var prov = from p in lde.Provinces where p.CountryId == iCountryId
orderby p.ProvinceName select p;
ddlProvince.Items.Add(String.Empty);
foreach (Province pr in prov)
{
ddlProvince.Items.Add(new ListItem(pr.ProvinceName, pr.ProvinceId.ToString()));
}
}
else
{
ddlProvince.Items.Clear();
}
}
protected void btnImHere_Click(object sender, EventArgs e)
{
int i = 0;
DataAccess da = new DataAccess();
double Lat = Convert.ToDouble(Latitude.Value),
Lon = Convert.ToDouble(Longitude.Value);
List<Place> lp = da.NearByLocations(Lat, Lon);
foreach (Place p in lp)
{
ListItem li = new ListItem(p.Name, p.PlaceId.ToString());
if (i == 0)
{
li.Selected = true;
}
ddlPlace.Items.Add(li);
i++;
}
pnlAddPlace.Visible = false;
pnlImHere.Visible = true;
}
protected void lbImHere_Click(object sender, EventArgs e)
{
string UserName = Membership.GetUser().UserName;
ListViewItem lvi = (ListViewItem)(((LinkButton)sender).Parent);
HiddenField hd = (HiddenField)lvi.FindControl("hdPlaceId");
long PlaceId = Convert.ToInt64(hd.Value);
double dLatitude = Convert.ToDouble(Latitude.Value);
double dLongitude = Convert.ToDouble(Longitude.Value);
DataAccess da = new DataAccess();
Place pl = da.GetPlace(PlaceId);
pnlImHereNow.Visible = true;
pnlPlaces.Visible = false;
hdPlaceId.Value = PlaceId.ToString();
hdPlaceLat.Value = pl.Latitude.ToString();
hdPlaceLon.Value = pl.Longitude.ToString();
hdPlaceTitle.Value = pl.Name;
lblPlaceTitle.Text = pl.Name;
}
protected void btnHere_Click(object sender, EventArgs e)
{
string UserName = Membership.GetUser().UserName;
string WhatsH = txtImHere.Text;
long PlaceId = Convert.ToInt64(ddlPlace.SelectedValue);
double dLatitude = Convert.ToDouble(Latitude.Value);
double dLongitude = Convert.ToDouble(Longitude.Value);
DataAccess da = new DataAccess();
da.StoreUserAT(UserName, PlaceId, WhatsH,
dLatitude, dLongitude);
}
protected void btnLocalCoupons_Click(object sender, EventArgs e)
{
double dLatitude = Convert.ToDouble(Latitude.Value);
double dLongitude = Convert.ToDouble(Longitude.Value);
DataAccess da = new DataAccess();
}
protected void lbBusiness_Click(object sender, EventArgs e)
{
string UserName = Membership.GetUser().UserName;
GridViewRow gvr = (GridViewRow)(((LinkButton)sender).Parent.Parent);
HiddenField hd = (HiddenField)gvr.FindControl("hdPlaceId");
string sPlaceId = hd.Value;
Int64 PlaceId;
if (!String.IsNullOrEmpty(sPlaceId))
{
PlaceId = Convert.ToInt64(sPlaceId);
}
}
protected void lbLocalDeals_Click(object sender, EventArgs e)
{
double dLatitude = Convert.ToDouble(Latitude.Value);
double dLongitude = Convert.ToDouble(Longitude.Value);
DataAccess da = new DataAccess();
pnlCoupons.Visible = true;
pnlStart.Visible = false;
List<GeoPromotion> lgp = da.NearByDeals(dLatitude, dLongitude);
lvCoupons.DataSource = lgp;
lvCoupons.DataBind();
}
protected void lbLocalPlaces_Click(object sender, EventArgs e)
{
DataAccess da = new DataAccess();
double Lat = Convert.ToDouble(Latitude.Value);
double Lon = Convert.ToDouble(Longitude.Value);
List<LocationDataModel.Place> places = da.NearByLocations(Lat, Lon);
lvPlaces.DataSource = places;
lvPlaces.SelectedIndex = -1;
lvPlaces.DataBind();
pnlPlaces.Visible = true;
pnlStart.Visible = false;
}
protected void ReturnFromPlaces_Click(object sender, EventArgs e)
{
pnlPlaces.Visible = false;
pnlStart.Visible = true;
}
protected void ReturnFromDeals_Click(object sender, EventArgs e)
{
pnlCoupons.Visible = false;
pnlStart.Visible = true;
}
protected void btnImHereNow_Click(object sender, EventArgs e)
{
long PlaceId = Convert.ToInt32(hdPlaceId.Value);
string UserName = Membership.GetUser().UserName;
string WhatsHappening = txtWhatsHappening.Text;
double UserLat = Convert.ToDouble(Latitude.Value);
double UserLon = Convert.ToDouble(Longitude.Value);
DataAccess da = new DataAccess();
da.StoreUserAT(UserName, PlaceId, WhatsHappening,
UserLat, UserLon);
}
protected void lbImHereNowReturn_Click(object sender, EventArgs e)
{
pnlImHereNow.Visible = false;
pnlPlaces.Visible = true;
}
protected void lbBackToBeginning_Click(object sender, EventArgs e)
{
pnlStart.Visible = true;
pnlImHereNow.Visible = false;
}
protected void lbWhereIveBeen_Click(object sender, EventArgs e)
{
string UserName = Membership.GetUser().UserName;
pnlStart.Visible = false;
pnlIveBeenHere.Visible = true;
DataAccess da = new DataAccess();
lvWhereIveBeen.DataSource = da.UserATs(UserName, 0, 15);
lvWhereIveBeen.DataBind();
}
protected void lbIveBeenHereBack_Click(object sender, EventArgs e)
{
pnlIveBeenHere.Visible = false;
pnlStart.Visible = true;
}
protected void lbPlaceIveBeen_Click(object sender, EventArgs e)
{
LinkButton lb = (LinkButton)sender;
ListViewItem lvi = (ListViewItem)lb.Parent.Parent;
HiddenField hdATID = (HiddenField)lvi.FindControl("hdATID");
Int64 ATID = Convert.ToInt64(hdATID.Value);
DataAccess da = new DataAccess();
pnlIveBeenHere.Visible = false;
pnlPlaceIveBeen.Visible = true;
var plac = da.GetPlaceViaATID(ATID);
hdPlaceIveBeenPlaceId.Value = plac.PlaceId.ToString();
hdPlaceIveBeenLatitude.Value = plac.Latitude.ToString();
hdPlaceIveBeenLongitude.Value = plac.Longitude.ToString();
lblPlaceIveBeenName.Text = plac.Name;
lblPlaceIveBeenAddress.Text = plac.Address1;
lblPlaceIveBeenCity.Text = plac.City;
lblPlaceIveBeenState.Text = plac.Province.ProvinceName;
lblPlaceIveBeenZipCode.Text = plac.ZipCode;
lblPlaceIveBeenCountry.Text = plac.Country.Name;
}
protected void lbNotListed_Click(object sender, EventArgs e)
{
SetupAddPoint();
pnlPlaces.Visible = false;
}
protected void lbAddAPlace_Click(object sender, EventArgs e)
{
SetupAddPoint();
}
private void SetupAddPoint()
{
double lat = Convert.ToDouble(Latitude.Value);
double lon = Convert.ToDouble(Longitude.Value);
DataAccess da = new DataAccess();
var zip = da.WhereAmIAt(lat, lon);
if (zip.Count > 0)
{
var z0 = zip[0];
txtCity.Text = z0.City;
txtZipCode.Text = z0.ZipCode;
ddlProvince.ClearSelection();
if (z0.ProvinceId.HasValue == true)
{
foreach (ListItem li in ddlProvince.Items)
{
if (li.Value == z0.ProvinceId.Value.ToString())
{
li.Selected = true;
break;
}
}
}
}
pnlAddPlace.Visible = true;
pnlStart.Visible = false;
}
protected void lbAddPlaceReturn_Click(object sender, EventArgs e)
{
pnlAddPlace.Visible = false;
pnlStart.Visible = true;
liPlaceAddMessage.Visible = false;
PlaceAddMessage.Text = String.Empty;
}
protected void lbPlaceIveBeenBack_Click(object sender, EventArgs e)
{
pnlIveBeenHere.Visible = true;
pnlPlaceIveBeen.Visible = false;
}
protected void lbPlaceIveBeenBeginning_Click(object sender, EventArgs e)
{
pnlPlaceIveBeen.Visible = false;
pnlStart.Visible = true;
}
protected string DisplayName(object val)
{
string strVal = Convert.ToString(val);
if (AppleIPhone.IsIPad())
{
ipd = iPhoneDevice.iPad;
}
if (AppleIPhone.IsIPhone())
{
ipd = iPhoneDevice.iPhone;
}
if (AppleIPhone.IsIPodTouch())
{
ipd = iPhoneDevice.iPodTouch;
}
return (iPhoneHelper.DisplayContentOnMenu(strVal, ipd));
}
}
iPhoneHelper.cs file:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
public enum iPhoneDevice
{
iPhone, iPodTouch, iPad
}
/// <summary>
/// Summary description for iPhoneHelper
/// </summary>
///
public class iPhoneHelper
{
public iPhoneHelper()
{
//
// TODO: Add constructor logic here
//
}
// This code is stupid in retrospect. Use css to solve this problem
public static string DisplayContentOnMenu(string val, iPhoneDevice ipd)
{
string Return = val;
string Elipsis = "...";
int iPadMaxLength = 30;
int iPhoneMaxLength = 15;
if (ipd == iPhoneDevice.iPad)
{
if (Return.Length > iPadMaxLength)
{
Return = Return.Substring(0, iPadMaxLength - Elipsis.Length) + Elipsis;
}
}
else
{
if (Return.Length > iPhoneMaxLength)
{
Return = Return.Substring(0, iPhoneMaxLength - Elipsis.Length) + Elipsis;
}
}
return (Return);
}
}
Source code for the ViewStatePage:
using System;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
/// <summary>
/// Summary description for BasePage
/// </summary>
#region Base class for a page.
public class ViewStatePage : System.Web.UI.Page
{
PageStatePersisterToDatabase myPageStatePersister;
public ViewStatePage()
: base()
{
myPageStatePersister = new PageStatePersisterToDatabase(this);
}
protected override PageStatePersister PageStatePersister
{
get
{
return myPageStatePersister;
}
}
}
#endregion
#region This class will override the page persistence to store page state in a database.
public class PageStatePersisterToDatabase : PageStatePersister
{
private string ViewStateKeyField = "__VIEWSTATE_KEY";
private string _exNoConnectionStringFound = "No Database Configuration information is in the web.config.";
public PageStatePersisterToDatabase(Page page)
: base(page)
{
}
public override void Load()
{
// Get the cache key from the web form data
System.Int64 key = Convert.ToInt64(Page.Request.Params[ViewStateKeyField]);
Pair state = this.LoadState(key);
// Abort if cache object is not of type Pair
if (state == null)
throw new ApplicationException("Missing valid " + ViewStateKeyField);
// Set view state and control state
ViewState = state.First;
ControlState = state.Second;
}
public override void Save()
{
// No processing needed if no states available
if (ViewState == null && ControlState != null)
return;
System.Int64 key;
IStateFormatter formatter = this.StateFormatter;
Pair statePair = new Pair(ViewState, ControlState);
// Serialize the statePair object to a string.
string serializedState = formatter.Serialize(statePair);
// Save the ViewState and get a unique identifier back.
key = SaveState(serializedState);
// Register hidden field to store cache key in
// Page.ClientScript does not work properly with Atlas.
//Page.ClientScript.RegisterHiddenField(ViewStateKeyField, key.ToString());
ScriptManager.RegisterHiddenField(this.Page, ViewStateKeyField, key.ToString());
}
private System.Int64 SaveState(string PageState)
{
System.Int64 i64Key = 0;
string strConn = String.Empty,
strProvider = String.Empty;
string strSql = "insert into tblPageState ( SerializedState ) values ( '" + SqlEscape(PageState) + "');select scope_identity();";
SqlConnection sqlCn;
SqlCommand sqlCm;
try
{
GetDBConnectionString(ref strConn, ref strProvider);
sqlCn = new SqlConnection(strConn);
sqlCm = new SqlCommand(strSql, sqlCn);
sqlCn.Open();
i64Key = Convert.ToInt64(sqlCm.ExecuteScalar());
if (sqlCn.State != ConnectionState.Closed)
{
sqlCn.Close();
}
sqlCn.Dispose();
sqlCm.Dispose();
}
finally
{
sqlCn = null;
sqlCm = null;
}
return i64Key;
}
private Pair LoadState(System.Int64 iKey)
{
string strConn = String.Empty,
strProvider = String.Empty,
SerializedState = String.Empty,
strMinutesInPast = GetMinutesInPastToDelete();
Pair PageState;
string strSql = "select SerializedState from tblPageState where tblPageStateID=" + iKey.ToString() + ";" +
"delete from tblPageState where DateUpdated<DateAdd(mi, " + strMinutesInPast + ", getdate());";
SqlConnection sqlCn;
SqlCommand sqlCm;
try
{
GetDBConnectionString(ref strConn, ref strProvider);
sqlCn = new SqlConnection(strConn);
sqlCm = new SqlCommand(strSql, sqlCn);
sqlCn.Open();
SerializedState = Convert.ToString(sqlCm.ExecuteScalar());
IStateFormatter formatter = this.StateFormatter;
if ((null == SerializedState) ||
(String.Empty == SerializedState))
{
throw (new ApplicationException("No ViewState records were returned."));
}
// Deserilize returns the Pair object that is serialized in
// the Save method.
PageState = (Pair)formatter.Deserialize(SerializedState);
if (sqlCn.State != ConnectionState.Closed)
{
sqlCn.Close();
}
sqlCn.Dispose();
sqlCm.Dispose();
}
finally
{
sqlCn = null;
sqlCm = null;
}
return PageState;
}
private string SqlEscape(string Val)
{
string ReturnVal = String.Empty;
if (null != Val)
{
ReturnVal = Val.Replace("'", "''");
}
return (ReturnVal);
}
private void GetDBConnectionString(ref string ConnectionStringValue, ref string ProviderNameValue)
{
if (System.Configuration.ConfigurationManager.ConnectionStrings.Count > 0)
{
ConnectionStringValue = System.Configuration.ConfigurationManager.ConnectionStrings["ApplicationServices"].ConnectionString;
ProviderNameValue = System.Configuration.ConfigurationManager.ConnectionStrings["ApplicationServices"].ProviderName;
}
else
{
throw new ConfigurationErrorsException(_exNoConnectionStringFound);
}
}
private string GetMinutesInPastToDelete()
{
string strReturn = "-60";
if (null != System.Configuration.ConfigurationManager.AppSettings["MinutesInPastToDeletePageState"])
{
strReturn = System.Configuration.ConfigurationManager.AppSettings["MinutesInPastToDeletePageState"].ToString();
}
return (strReturn);
}
}
#endregion
AppleiPhone.cs file:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
/// <summary>
/// Summary description for AppleIPhone
/// </summary>
public class AppleIPhone
{
public AppleIPhone()
{
//
// TODO: Add constructor logic here
//
}
static public bool IsIPhoneOS()
{
return (IsIPad() || IsIPhone() || IsIPodTouch());
}
static public bool IsIPhone()
{
return IsTest("iPhone");
}
static public bool IsIPodTouch()
{
return IsTest("iPod");
}
static public bool IsIPad()
{
return IsTest("iPad");
}
static private bool IsTest(string Agent)
{
bool bl = false;
string ua = HttpContext.Current.Request.UserAgent.ToLower();
try
{
bl = ua.Contains(Agent.ToLower());
}
catch { }
return (bl);
}
}
Master page .cs:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
public partial class MasterPages_iPhoneMaster : System.Web.UI.MasterPage
{
protected void Page_Load(object sender, EventArgs e)
{
HtmlHead head = Page.Header;
HtmlMeta meta = new HtmlMeta();
if (AppleIPhone.IsIPad() == true)
{
meta.Content = "width=400,user-scalable=no";
head.Controls.Add(meta);
}
else
{
meta.Content = "width=device-width, user-scalable=no";
meta.Attributes.Add("name", "viewport");
}
meta.Attributes.Add("name", "viewport");
head.Controls.Add(meta);
HtmlLink cssLink = new HtmlLink();
HtmlGenericControl script = new HtmlGenericControl("script");
script.Attributes.Add("type", "text/javascript");
script.Attributes.Add("src", ResolveUrl("~/Scripts/iWebKit/javascript/functions.js"));
head.Controls.Add(script);
cssLink.Attributes.Add("rel", "stylesheet");
cssLink.Attributes.Add("href", ResolveUrl("~/Scripts/iWebKit/css/style.css") );
cssLink.Attributes.Add("type", "text/css");
head.Controls.Add(cssLink);
HtmlGenericControl jsLink = new HtmlGenericControl("script");
//jsLink.Attributes.Add("type", "text/javascript");
//jsLink.Attributes.Add("src", ResolveUrl("~/Scripts/jquery-1.4.1.min.js") );
//head.Controls.Add(jsLink);
HtmlLink appleIcon = new HtmlLink();
appleIcon.Attributes.Add("rel", "apple-touch-icon");
appleIcon.Attributes.Add("href", ResolveUrl("~/apple-touch-icon.png"));
HtmlMeta appleMobileWebAppStatusBarStyle = new HtmlMeta();
appleMobileWebAppStatusBarStyle.Attributes.Add("name", "apple-mobile-web-app-status-bar-style");
appleMobileWebAppStatusBarStyle.Attributes.Add("content", "black");
head.Controls.Add(appleMobileWebAppStatusBarStyle);
}
internal string FindPath(string Location)
{
string Url = Server.MapPath(Location);
return (Url);
}
}
© ASP.net Weblogs or respective owner