converting DLL to ascx?

nakins

New member
Joined
Aug 21, 2008
Messages
4
Programming Experience
1-3
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

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
 
ascx is the extension for user controls, Add a "Web User Control" to your project, then use the visual designer to design it like any other Asp.net page. Add code-behind when you have the UI ready. That means most of your code in RenderContents is redundant.
 
I believe you might have missed the point of my post. I already know how to add an ascx. My question is how to know what parts of this code are particular to Visual Studio and of those how or what to I use to make the code work as an ascx.
 
Oh, you didn't write this code, you just copied it and wonders what it does? The essence is consuming a webservice, see for example Creating and Consuming a Web Service, Part 2 . In short regarding your posted code a variable is declared to hold the service reference and a new instance is created:
VB.NET:
Dim mForecast As New net.webservicex.www.WeatherForecast
then you can interact with the service methods:
VB.NET:
Dim Wx As net.webservicex.www.WeatherForecasts = mForecast.GetWeatherByZipCode(zip)
LabelPlace.Text = Wx.PlaceName
The namespace (net.webservicex.www) could be different when you add the web reference.
 
Back
Top