ASP.NET - How to embed weather forecast in website

Asked By rohan phavade on 29-Jun-12 12:32 PM
How to embed weather forecast in website
Super Man replied to rohan phavade on 29-Jun-12 01:21 PM
Better and Simple way is that : You can make use of ready made widget available.

Here you can get instruction and readymade script to show in your website.

http://leedstestsite.moonfruit.com/#/weather-forecast-widget/4545140937
kalpana aparnathi replied to rohan phavade on 29-Jun-12 02:17 PM
hi,

In the aspx:


<asp:GridView
    ID="GridView1"
    runat="server"
    AutoGenerateColumns="false"
    ShowHeader="false">
    <Columns>
      <asp:BoundField DataField="Title" />
      <asp:BoundField DataField="Description" />
    </Columns>
</asp:GridView>


In the code-behind Page_Load event:

XmlTextReader reader = new XmlTextReader("http://feeds.bbc.co.uk/weather/feeds/rss/5day/world/2243.xml");
DataSet ds = new DataSet();
ds.ReadXml(reader);
GridView1.DataSource = ds.Tables[3];
GridView1.DataBind();

Ravi Maurya replied to rohan phavade on 30-Jun-12 02:30 AM
Refer these link you will get good ready to use sample or tutorials with step by step instructions

http://blogs.msdn.com/b/mikhailarkhipov/archive/2006/06/12/628540.aspx 

http://www.freevbcode.com/ShowCode.asp?ID=8300 

http://www.codeproject.com/Articles/112238/Creating-ASP-NET-Weather-User-Control 


Hope this might help you
Lalit M replied to rohan phavade on 30-Jun-12 02:36 AM
Read this step & code sample
=================

1. Put .cs files in your App_Code folder.
2. Add WeatherControl.ascx to the Web site.
3. Add <%@ Register TagPrefix="My" TagName="Weather" Src="WeatherControl.ascx" %> on top of the page markup
4. Add <My:Weather Runat="server" Latitude="47.72" Longtitude="-122.02" cssClassCold="Cold" cssClassNormal="Normal" cssClassWarm="Warm" Width="70%"/>
 where you want control to appear. Adjust attributes as needed and provide your own CSS file with Cold, Warm and Normal classes.

.ASPX code
------------------
<%@ Page Language="C#" %>

<%@ Register TagPrefix="My" TagName="Weather" Src="WeatherControl.ascx" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<script runat="server">

</script>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
      <div>
        <My:Weather Runat="server" Latitude="47.72" Longtitude="-122.02" cssClassCold="Cold"
          cssClassNormal="Normal" cssClassWarm="Warm" Width="70%" />
      </div>
    </form>
</body>
</html>

.CS Code sample
-------------
<%@ Control Language="C#" %>
<%@ import Namespace="System.Xml" %>
<%@ import Namespace="Weather" %>

<script runat="server">

    private Decimal _latitude = new Decimal(47.72);
    private Decimal _longtitude = new Decimal(-122.02);

    private string _cssClassWarm = "Warm";
    private string _cssClassCold = "Cold";
    private string _cssClassNormal = "Normal";
    
    private Unit _width = Unit.Parse("70%");
    
    public string Longtitude
    { set { _longtitude = Decimal.Parse(value); } }

    public string Latitude
    {  set { _latitude = Decimal.Parse(value); } }

    public string cssClassCold
    { set { _cssClassCold = value; } }

    public string cssClassWarm
    { set { _cssClassWarm = value; } }

    public string cssClassNormal
    { set { _cssClassNormal = value; } }

    public string Width
    { set { _width = Unit.Parse(value); } }
    
    void Page_Load(Object sender, EventArgs e)
    {
      if (!Page.IsPostBack)
      {
      
        ndfdXML weatherFetcher = new ndfdXML();
        weatherParametersType weatherParams = new weatherParametersType();
        string xmlWeather;
        
        WeatherTable.Visible = false;
        WeatherTable.Width = _width;
        
        try
        {
          DayWeatherData[] arrDayWeather;
          DateTime dtStart = new DateTime(DateTime.Now.Year, 1, 1, 0, 0, 0);
          
          // http://www.nws.noaa.gov/forecasts/xml/
          xmlWeather = weatherFetcher.NDFDgen(_latitude, _longtitude, "glance", dtStart, dtStart.AddYears(1), weatherParams);
            
          arrDayWeather = WeatherXMLParser.ParseWeatherXML(xmlWeather);
          if(arrDayWeather != null)
          {
            TableRow tr = new TableRow(); // titles
            foreach (DayWeatherData dt in arrDayWeather)
            {
              TableCell tc = new TableCell();
              tc.Text = dt.DateTime.ToLongDateString();
              tr.Cells.Add(tc);
            }

            tr.CssClass = _cssClassNormal;
            tr.HorizontalAlign = HorizontalAlign.Center;
            tr.VerticalAlign = VerticalAlign.Top;
            WeatherTable.Rows.Add(tr);

            tr = new TableRow(); // clouds
            foreach(DayWeatherData dt in arrDayWeather)
            {
              TableCell tc = new TableCell();
              tc.Text = "<img src='" + dt.CloudIconURL + "'/>";
              tr.Cells.Add(tc);
            }

            tr.CssClass = _cssClassNormal;
            tr.HorizontalAlign = HorizontalAlign.Center;
            tr.VerticalAlign = VerticalAlign.Top;
            WeatherTable.Rows.Add(tr);

            tr = new TableRow(); // highs
            foreach (DayWeatherData dt in arrDayWeather)
            {
              TableCell tc = new TableCell();
              tc.Text = dt.HighTempC + "&#176;C (" + dt.HighTempF + "&#176;F)";
              tc.Wrap = false;
              tr.Cells.Add(tc);
            }

            tr.CssClass = _cssClassWarm;
            tr.HorizontalAlign = HorizontalAlign.Center;
            tr.VerticalAlign = VerticalAlign.Top;
            WeatherTable.Rows.Add(tr);

            tr = new TableRow(); // lows
            foreach (DayWeatherData dt in arrDayWeather)
            {
              TableCell tc = new TableCell();
              tc.Text = dt.LowTempC + "&#176;C (" + dt.LowTempF + "&#176;F)" ;
              tc.Wrap = false;
              tr.Cells.Add(tc);
            }

            tr.CssClass = _cssClassCold;
            tr.HorizontalAlign = HorizontalAlign.Center;
            tr.VerticalAlign = VerticalAlign.Top;
            WeatherTable.Rows.Add(tr);

            WeatherTable.Visible = true;
          }
        }
        catch(Exception)
        {        
          WeatherTable.Visible = false;
        }
      }
    }
    
</script>

<asp:Table runat="server" id="WeatherTable" />