Hello,
I found a VS project that is a weather control which gets an xml file from default (National Weather Service) and parses it to deliver a 3 day forecast.
This comes from ASP.NET 2.0 US Weather Forecast Custom Control and is all in a zip file. There is a completed DLL and aspx and other supporting files to make it work, as is, on a web page.
I would like to use this feed/webservice as it is free and with no strings attached. However, I need to make changes in how the info is laid out and displayed. I also would like to display my own images as opposed to the fetched ones.
I believe I have found the file I need to use in order to create a ascx, but I'm not sure if the code below is all that is required. Also, I'n not sure what to add/remove in order to make it work, such as converting it from a dll to an ascx. I do have some experience with vb, building aspx pages, mostly dealing with database i/o.
I would really appreciate any advice, suggestion, or direction in how to convert this to an ascx.
Thank you
I found a VS project that is a weather control which gets an xml file from default (National Weather Service) and parses it to deliver a 3 day forecast.
This comes from ASP.NET 2.0 US Weather Forecast Custom Control and is all in a zip file. There is a completed DLL and aspx and other supporting files to make it work, as is, on a web page.
I would like to use this feed/webservice as it is free and with no strings attached. However, I need to make changes in how the info is laid out and displayed. I also would like to display my own images as opposed to the fetched ones.
I believe I have found the file I need to use in order to create a ascx, but I'm not sure if the code below is all that is required. Also, I'n not sure what to add/remove in order to make it work, such as converting it from a dll to an ascx. I do have some experience with vb, building aspx pages, mostly dealing with database i/o.
I would really appreciate any advice, suggestion, or direction in how to convert this to an ascx.
Thank you
VB.NET:
Imports System
Imports System.Collections.Generic
Imports System.ComponentModel
Imports System.Text
Imports System.Web
Imports System.Web.UI
Imports System.Web.UI.WebControls
'Imports System.Xml
<ToolboxData("<{0}:Forecast runat=server></{0}:Forecast>")> _
Public Class Forecast
Inherits WebControl
#Region "Declarations"
Private mForecast As net.webservicex.www.WeatherForecast
Private WxDetails() As net.webservicex.www.WeatherData
Private Wx As net.webservicex.www.WeatherForecasts
#End Region
#Region "Methods"
Private Sub Forecast_Init(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Init
If Not String.IsNullOrEmpty(ZipCode) Then
GetWeather(ZipCode)
Else
GetWeather("36201")
End If
End Sub
Public Sub GetWeather(ByVal zip As String)
Try
mForecast = New net.webservicex.www.WeatherForecast
Wx = mForecast.GetWeatherByZipCode(zip)
WxDetails = Wx.Details
Catch
Exit Sub
End Try
End Sub
#End Region
#Region "Properties"
<Category("Weather"), Description("Set Forecast Zip Code"), Browsable(True)> _
Property ZipCode() As String
Get
Dim s As String = CStr(ViewState("ZipCode"))
If s Is Nothing Then
Return String.Empty
Else
Return s
End If
End Get
Set(ByVal Value As String)
ViewState("ZipCode") = Value
End Set
End Property
#End Region
#Region "Rendering"
Protected Overrides Sub RenderContents(ByVal output As HtmlTextWriter)
' the web service actually returns seven days, I am just using the
' first three days to make a 3 day forecast but the additional days
' could be added in a similar manner
Try
' set padding and start the table
output.AddStyleAttribute(HtmlTextWriterStyle.Padding, "3")
output.RenderBeginTag(HtmlTextWriterTag.Table)
' display location information based on zip code
' in first row of the table
output.RenderBeginTag(HtmlTextWriterTag.Tr)
output.RenderBeginTag(HtmlTextWriterTag.Td)
output.Write("<b>Location: </b>" & Wx.PlaceName.ToString() & ", " & _
Wx.StateCode.ToString() & "<br/>")
output.Write("<b>Zip Code: </b>" & ZipCode & "<br/>")
output.Write("<b>Lat/Long: </b>" & Wx.Latitude.ToString() & _
"/" & Wx.Longitude.ToString() & "<br/>")
output.RenderEndTag()
output.RenderEndTag()
' display highs and lows for day 1
output.RenderBeginTag(HtmlTextWriterTag.Tr)
output.RenderBeginTag(HtmlTextWriterTag.Td)
output.Write("<hr/>")
output.Write("<b> Day: </b>" & WxDetails(0).Day.ToString() & "<br/>")
output.Write("<b> High/Low: </b>" & WxDetails(0).MaxTemperatureF.ToString() & _
"/" & WxDetails(0).MinTemperatureF.ToString() & "<br/><br/>")
output.RenderEndTag()
output.RenderEndTag()
' get weather service image and add it to control
output.AddAttribute(HtmlTextWriterAttribute.Align, "center")
output.RenderBeginTag(HtmlTextWriterTag.Tr)
output.RenderBeginTag(HtmlTextWriterTag.Td)
Dim img As New Image()
img.ImageUrl = WxDetails(0).WeatherImage.ToString()
img.BorderStyle = WebControls.BorderStyle.Inset
img.BorderWidth = 2
img.RenderControl(output)
output.RenderEndTag()
output.RenderEndTag()
' display highs and lows for day 2
output.RenderBeginTag(HtmlTextWriterTag.Tr)
output.RenderBeginTag(HtmlTextWriterTag.Td)
output.Write("<hr/>")
output.Write("<b>Day: </b>" & WxDetails(1).Day.ToString() & "<br/>")
output.Write("<b>High/Low: </b>" & WxDetails(1).MaxTemperatureF.ToString() & _
"/" & WxDetails(1).MinTemperatureF.ToString() & "<br/><br/>")
output.RenderEndTag()
output.RenderEndTag()
' get weather service image and add it to control
output.AddAttribute(HtmlTextWriterAttribute.Align, "center")
output.RenderBeginTag(HtmlTextWriterTag.Tr)
output.RenderBeginTag(HtmlTextWriterTag.Td)
Dim img2 As New Image()
img2.ImageUrl = WxDetails(1).WeatherImage.ToString()
img2.BorderStyle = WebControls.BorderStyle.Inset
img2.BorderWidth = 2
img2.RenderControl(output)
output.RenderEndTag()
output.RenderEndTag()
' display highs and lows for day 3
output.RenderBeginTag(HtmlTextWriterTag.Tr)
output.RenderBeginTag(HtmlTextWriterTag.Td)
output.Write("<hr/>")
output.Write("<b>Day: </b>" & WxDetails(2).Day.ToString() & "<br/>")
output.Write("<b>High/Low: </b>" & WxDetails(2).MaxTemperatureF.ToString() & _
"/" & WxDetails(2).MinTemperatureF.ToString() & "<br/><br/>")
output.RenderEndTag()
output.RenderEndTag()
' get weather service image and add it to control
output.AddAttribute(HtmlTextWriterAttribute.Align, "center")
output.RenderBeginTag(HtmlTextWriterTag.Tr)
output.RenderBeginTag(HtmlTextWriterTag.Td)
Dim img3 As New Image()
img3.ImageUrl = WxDetails(2).WeatherImage.ToString()
img3.BorderStyle = WebControls.BorderStyle.Inset
img3.BorderWidth = 2
img3.RenderControl(output)
output.Write("<br/><br/>")
output.RenderEndTag()
output.RenderEndTag()
' close the table
output.RenderEndTag()
Catch
' the control will not render without contacting the web service
' so just display text if the data is unavailable or the web
' service web method has not be evoked
output.Write("Weather Report Control")
End Try
End Sub
#End Region
End Class