The XBox 360 Widget that I use on this site was fairly straight forward to build thanks to mads and the team for making such an awesome framework for widgets. This post will run you through how to create such a widget.

It is fairly easy to create a widget as all you need to have is an edit.ascx to handle the edit fields and a widget.ascx to handle the display of the actual widget.

Edit: This article + code is horribly out of date. There is a better version of this widget located on biztron.net

~/widgets/XboxGamerCard/edit.ascx:

<%@ Control Language="C#" AutoEventWireup="true" CodeFile="edit.ascx.cs" Inherits="widgets_XboxGamerCard_edit" %>
<fieldset>

	<legend>Add your Gamer Tag ID</legend>
	<label for="<%=txtGamerTag.ClientID %>">Gamer Tag ID</label>
	<asp:RequiredFieldValidator runat="Server" ControlToValidate="txtGamerTag" ErrorMessage="Please enter a gamer tag" ValidationGroup="add" /><br />
	<asp:TextBox runat="server" ID="txtGamerTag" /><br /><br />

	<asp:Button runat="server" ID="btnAdd" Text="Add gamer tag" ValidationGroup="add" />
</fieldset>

~/widgets/XboxGamerCard/edit.ascx.cs:

#region Using

using System;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Xml;

#endregion

public partial class widgets_XboxGamerCard_edit : WidgetEditBase
{

    /// 
    /// Handles the Load event of the Page control.
    /// 
    /// The source of the event.
    /// The  instance containing the event data.
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            XmlNode node = Xml.SelectSingleNode("GamerTag");
            if (node != null)
            {
                txtGamerTag.Text = node.InnerText;
            }
        }
    }

    /// 
    /// Saves this the basic widget settings such as the Title.
    /// 
    public override void Save()
    {
        if (Xml.ChildNodes.Count == 0)
        {
            XmlNode node = Xml.CreateElement("GamerTag");
            Xml.AppendChild(node);
        }

        Xml.SelectSingleNode("GamerTag").InnerText = txtGamerTag.Text;

        SaveXml();
        Cache.Remove(txtGamerTag.Text + "_XboxGamerCard");
    }
}

~/widgets/XboxGamerCard/widget.ascx:

<%@ Control Language="C#" AutoEventWireup="true" CodeFile="widget.ascx.cs" Inherits="widgets_XboxGamerCard_widget" %>
<asp:Table ID="gamerTag" runat="server">
    <asp:TableRow>
        <asp:TableCell RowSpan="2" VerticalAlign="top"><b><asp:HyperLink ID="gamerName" Target="_new" runat="server" /></b><br /><asp:HyperLink ID="mainTag" Target="_new" runat="server" /></asp:TableCell>
        <asp:TableCell><b>Gamer Score: </b><asp:Literal ID="gamerScore" runat="server" /></asp:TableCell>
    </asp:TableRow>
    <asp:TableRow>
        <asp:TableCell>
            <asp:Repeater ID="gameLinks" runat="server">
                <ItemTemplate>
                    <asp:HyperLink ID="GameImage" Target="_new" NavigateUrl='<%# Eval("DetailsURL") %>' ImageUrl='<%# Eval("Image32Url") %>' Text='<%# Eval("Name") + " (" + Eval("Score") + ")" %>' runat="server" />
                </ItemTemplate>
            </asp:Repeater>
        </asp:TableCell>
    </asp:TableRow>
    <asp:TableRow>
        <asp:TableCell ColumnSpan="2"><asp:Label ID="gamerOnline" runat="server" /><br /><asp:Label ID="gamerSeen" runat="server" /><br /><asp:Label ID="gamerLocation" runat="server" /></asp:TableCell>
    </asp:TableRow>
</asp:Table>

~/widgets/XboxGamerCard/widget.ascx.cs:

#region Using

using System;
using System.Data;
using System.Collections;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Xml;
using System.IO;
using System.Web.UI.WebControls.WebParts;
using System.Net;
using System.Collections.Generic;
using System.Web.Caching;
using BlogEngine.Core;

#endregion

public partial class widgets_XboxGamerCard_widget : WidgetBase
{
	/// 
	/// Handles the Load event of the Page control.
	/// 
	/// The source of the event.
	/// The  instance containing the event data.
	protected void Page_Load(object sender, EventArgs e)
	{

        if (!Request.IsSecureConnection && !Page.IsPostBack)
        {
            gamerTag.Visible = true;
            XmlDownload();
        }
        else
            gamerTag.Visible = false;

		base.Name = "XboxGamerCard";
	}

    private void XmlDownload()
    {
        try
        {
            string gamerTagName = "";
            XmlNode node = base.Xml.SelectSingleNode("GamerTag");
            if (node != null)
                gamerTagName = node.InnerText;

            DataSet gamerDataSet = (DataSet)Cache[gamerTagName + "_XboxGamerCard"];
            if (gamerDataSet == null)
            {
                string url = String.Format("http://duncanmackenzie.net/services/GetXboxInfo.aspx?GamerTag={0}", gamerTagName);
                gamerDataSet = new DataSet();
                XmlTextReader xml = new XmlTextReader(url);
                gamerDataSet.ReadXml(xml);
            }

            Cache[gamerTagName + "_XboxGamerCard"] = gamerDataSet;

            DataTable xboxInfo = gamerDataSet.Tables["XboxInfo"];
            DataTable presenceInfo = gamerDataSet.Tables["PresenceInfo"];
            DataTable recentGameInfo = gamerDataSet.Tables["XboxUserGameInfo"];
            DataTable recentGame = gamerDataSet.Tables["Game"];
            DataTable modRecentGame = new DataTable();
            modRecentGame.Columns.Add("DetailsURL");
            modRecentGame.Columns.Add("Image32Url");
            modRecentGame.Columns.Add("Name");
            modRecentGame.Columns.Add("Score");

            for (int x = 0; x < recentGameInfo.Rows.Count; x++)
                modRecentGame.Rows.Add(new object[] { recentGameInfo.Rows[x]["DetailsURL"], recentGame.Rows[x]["Image32Url"], recentGame.Rows[x]["Name"], recentGameInfo.Rows[x]["GamerScore"] });

            gamerName.Text = Convert.ToString(xboxInfo.Rows[0]["Gamertag"]);
            gamerName.NavigateUrl = Convert.ToString(xboxInfo.Rows[0]["ProfileUrl"]);

            mainTag.ImageUrl = Convert.ToString(xboxInfo.Rows[0]["TileUrl"]);
            mainTag.Text = Convert.ToString(xboxInfo.Rows[0]["Gamertag"]);
            mainTag.NavigateUrl = Convert.ToString(xboxInfo.Rows[0]["ProfileUrl"]);

            gamerScore.Text = Convert.ToString(xboxInfo.Rows[0]["GamerScore"]);

            gameLinks.DataSource = modRecentGame.DefaultView;
            gameLinks.DataBind();

            gamerOnline.Text = Convert.ToString(presenceInfo.Rows[0]["StatusText"]);
            gamerSeen.Text = Convert.ToString(presenceInfo.Rows[0]["LastSeen"]);
            gamerLocation.Text = Convert.ToString(presenceInfo.Rows[0]["Info"]);
            gamerLocation.Text += Convert.ToString(presenceInfo.Rows[0]["Info2"]);
        }
        catch (Exception ex)
        {
            gamerTag.Visible = false;
            string foobar = ex.ToString();
        }
    }
}

XboxGamerCard.zip (2.92 kb)

Comments Comments (11) Permalink Permalink     Rss feed for comments Post Comment Feed

E-mail DotNetKicks Digg StumbleUpon Slashdot Twitter Facebook Del.icio.us

Comments (11) -

Nice one on providing the code for this widget. Its prooven a good starting point for my own site where I wanted to aggregate duncanmackenzie getxboxinfo webservice.

I'm planning on going a few steps further and actually making a class with the various bits of data available in!

Thanks!
United Kingdom 6/12/2008 2:11:52 PM #
Alberto
Alberto
I tried your code, but it doesn't work in my project.
These are the errors that gives me Visual Studio 2008 building web site:

'widgets_XboxGamerCard_widget' does not implement inherited abstract member 'WidgetBase.Name.get
'widgets_XboxGamerCard_widget' does not implement inherited abstract member 'WidgetBase.IsEditable.get
'widgets_XboxGamerCard_widget' does not implement inherited abstract member 'WidgetBase.LoadWidget()

I'm using BlogEngine.net 1.4.5
Italy 11/1/2008 8:30:36 AM #
Yeah BlogEngine changed things since the writing of this post and I haven't had a chance to update it. All you have to do is implement those functions.


    public override string Name
    {
        get { return "XboxGamerCard"; }
    }

    public override bool IsEditable
    {
        get { return true; }
    }


and change the Page_Load line to :

    public override void LoadWidget()
    {
        if (!Request.IsSecureConnection && !Page.IsPostBack)
        {
            gamerTag.Visible = true;
            XmlDownload();
        }
        else
            gamerTag.Visible = false;
  }
United States 11/2/2008 7:32:01 PM #
Alberto
Alberto
I editd the file, but now I have 8 new errors:
Error  1  Property or indexer 'WidgetBase.Name' cannot be assigned to -- it is read only    
Error  2  'WidgetBase' does not contain a definition for 'Xml'    
Error  3  'System.Web.UI.WebControls.Xml' does not contain a definition for 'SelectSingleNode'    
Error  4  'System.Web.UI.WebControls.Xml' does not contain a definition for 'ChildNodes'    
Error  5  'System.Web.UI.WebControls.Xml' does not contain a definition for 'CreateElement'    
Error  6  'System.Web.UI.WebControls.Xml' does not contain a definition for 'AppendChild'    
Error  7  'System.Web.UI.WebControls.Xml' does not contain a definition for 'SelectSingleNode'    
Error  8  The name 'SaveXml' does not exist in the current context
Italy 11/5/2008 5:48:47 AM #
What version of .NET Framework do you have on your server?
6/7/2010 10:29:45 AM #
Alberto
Alberto
OK, I have another question: how can you post the source code in that format? I mean the line numbers, the alternate background color, the highlight for the reserved words, ecc.
Do you use an extensn for blogengine or something?

Thank you, Alberto
Italy 11/6/2008 3:50:13 AM #
I use this extension from RTur to do the code highlighting rtur.net/.../...hter-extension-for-BlogEngine.aspx
United States 6/7/2010 10:29:14 AM #
BlogEngine.NET Widgets

ProjectDescriptionAcollectionofwidgetscreatedf...
5/19/2009 4:08:22 PM #
New and Improved BlogEngine.NET Twitter Widget

New and Improved BlogEngine.NET Twitter Widget
1/5/2010 7:23:31 PM #
I went to biztron.net/.../XBOX-GamerCard-Widget.aspx and the newer code is not there. The zip file link goes to a missing page.
United States 8/30/2010 10:28:53 PM #
He must have took it down. I will see what I can do to post the most current version.
United States 8/31/2010 10:48:51 AM #

Pingbacks and trackbacks (2)+

Add comment




  Country flag
biuquote
  • Comment
  • Preview
Loading


About Author

Justin Wendlandt
Justin Wendlandt
I work for Wells Fargo, play a lot of video games, and program in many languages. [ more ] E-mail me Send mail

View Justin Wendlandt's profile on LinkedIn View Justin Wendlandt's Facebook Profile View Justin Wendlandt's Twitter Profile View Justin Wendlandt's Zune Profile View Justin Wendlandt's Xbox Live Profile

Calendar

<<  September 2010  >>
MoTuWeThFrSaSu
303112345
6789101112
13141516171819
20212223242526
27282930123
45678910

View posts in large calendar

Recent Posts

Recent Comments

Comment RSS

Most comments

ericchapman ericchapman
1 comments
gb United Kingdom
Clone High Streaming Clone High Streaming
1 comments
us United States
iphone mad iphone mad
1 comments
gb United Kingdom

Poll

This poll is closed.
What do you use to connect to the internet?
A DLink Exterme Gaming router
 
0.0%
A Linksys / Cisco home router
 
0.0%
None of the above
 
0.0%
A Netgear home router
 
0.0%
I built my own box to do routing
 
100.0%

Total Votes: 1

Stock Quotes

WFC 25.10 +0.44 (+1.78%)
GOOG 463.18 +2.845 (+0.62%)
BBY 33.46 +0.80 (+2.45%)
CML 17.80 +2.79 (+18.59%)
COST 58.59 +0.80 (+1.38%)
MSFT 23.94 +0.04 (+0.17%)

Digsby

Xbox Gamer Card

Jwendl Gamer Score: 11570
Jwendl
Rock Band 2 (310) TMNT 1989 Arcade (35) Assassin's Creed II (340) Marvel Ult. Alliance 2 (390) FINAL FANTASY XIII (245) Borderlands (1340) Kane and Lynch:DeadMen (40) Madden NFL 10 (0) Family Game Night (0) STREET FIGHTER IV (180) South Park (180) Gears of War 2 (400) TEKKEN 6 (0) Culdcept SAGA (95) CSI: Deadly Intent (0) CSI-Hard Evidence (0)
Offline
8/30/2010 1:31:44 AM
Last seen 08/30/10 playing Xbox.com

Xbox Avatar

xbox Avatar

Disclaimer

The opinions expressed herein are my own personal opinions and do not represent my employer's view in anyway.

© Copyright 2010

Creative Commons License