Contents tagged with MICROSOFT

  • Simplified ASP.NET AJAX Custom Control Development With VS Item Template

    Hello,

    At work today I had a friend (Blair) have issues with trying to get a browser alert to popup on the page during an ASYNC post back. I told him I would make a custom control in 30 secounds or less that would do this and make life much easier. Overkill I know for such a simple task but it was more of a demo to show the ease/speed of creating a control using my custom item template.

  • Book Review: Linq Quickly by N Satheesh Kumar

    This books title does not lie, it is LINQ and it is quickly. This book is an excellent introduction into the world of LINQ and also a great reference once you get your feet wet.

    The start of the book gives you a brief introduction into what LINQ is and also an introduction into the new features in C# 3.0. It covers things like anonyms types, object initializers, collection initializers, partial methods, implicitly typed local variables, extension methods, lambda expressions,  query expressions and also a small intro to expression trees.

    Then it covers LINQ to Objects, which gives you a good idea on how to use LINQ over your in memory objects, including arrays, collections, string and even text files. It shows you just how easy it is to do things that in the past would have been a lot harder to complete.

    Next up is LINQ to XML, this is a great introduction to this subject, it covers a great deal of information in regards to all the new LINQ to XML classes, how to create/update/delete XML documents using these new classes and methods. You are also shown the power you now have when you are querying XML and how easy it just is.

    The LINQ to SQL chapter is also an excellent reference if you would like to get your feet wet but would not like to be overwhelmed with information. There are plenty of good examples on how to begin using LINQ to SQL and the code samples provided make this very easy. It covers everything from Data Context, Attributes, Relationships, querying, data manipulation, using stored procedures and also all the other common query operators that you would use. There is also a chapter on LINQ to Datasets but as I never use datasets this was something I briefly skimmed over and did not digest.

    Finally the book provides a good chapter on Standard Query Operators, this is an excellent post reference once you are already in the world of LINQ, it is good to able to flick to be back, find what you need and see the function prototypes and even a code sample on how to use it. I still sometimes will flick to this part of the book to quickly reference things when I forget :P.

    All in all this was an excellent book and I would highly recommend it to anyone who would like to start out with LINQ but would like to get a quick introduction and be on their way to actually using it. This book provides just that and more


    Book Information:

  • A Templated ASP.NET RSS Feed Reader Control

    Update 20070330:

    By popular demand (well a couple people) I have included a sample project with everything needed to get started using this control.

    Basically just a sample web project, click here to get the zipped archive. You might notice things are done a little different, I include the System.ServiceModel.Syndication namespace on my page so that I can case the Container.DataItem to a SyndicationItem and access its properties that way, stops using Eval, but each to their own.


    Hope that makes it easier for some, will be including full source in future too :).

    Project Files:
    TemplatedRSSFeedReader.zip

    Thanks
    Stefan


    Update 20070319:

    Today I attended the Heros Happen event in Perth, and I learnt something very very cool, you see how I wrote my own classes and code to load the syndicated feed in, then used LINQ to XML to load that into my classes. During Dave Glovers presentation on WCF he had a sample in there that was building an RSS feed, and I could see he was using some classes to do so. What I then noticed is these classes are new in .NET 3.5 and will make creation of this control 100 times easier plug give us full support of the RSS 2.0 spec and allow us to access all feed proprties :) :) and will mean *removing* code and making things much simpler.

    Firstly we need to add a reference to the System.ServiceModel.Web assembley, this is where the magic is. Then add a using System.ServiceModel.Syndication; to the RSSReader.cs class, we then have access to these helper classes which allow us to easily read and create RSS 2.0 and ATOM 1.0 feeds, as I am only supporting RSS I will only use the RSS formatter. To read our feed we need to do the below:

    Firslty read the feed into an XMLReader:

    XmlReader reader = XmlReader.Create("http://weblogs.asp.net/stefansedich/rss.aspx");

    Then create an instance of the RSS20FeedFormatter and then make this read the data from the xmlreader:

    Rss20FeedFormatter feedFormatter = new Rss20FeedFormatter();
    feedFormatter.ReadFrom(reader);


    We now can get access to the feed by using feedFormatter.Feed, this gives us an instance of a SyndicationFeed class which gives us access to all items and properties of our feed. The only difference now is that as some properties are a little different accessing them is different.So instead of feed.Title you use feed.Title.Text instead. The good thing with this is, 1. We have removed the need to do this manually and 2. We have all properties that belong to an RSS feed.

    The updated RSSReader class is below with the code changes. You can now remove the Feed and FeedItem classes we created earlier as they are needed no more. It just goes to show we learn something new every day.....


    Code:

  • UserControl OutputCache and VaryByParam not working with postback!!

    Don't know if anyone has come across this before. But if you have a usercontrol that uses VaryByParam and you have it on a page and do a postback you will get the wrong cache item. Use the below test bed to see what I mean, this is following to my post for help on the asp.net forums, I then found this post which showed me it is an issue, following this I think I came up with a solution. That is to add a hidden field to my page with the name MenuID and populate that. Now on postback it seems to work fine, as VaryByParam="MenuID" seems to read the form var MenuID and gets the correct value hence fixing caching.
     

  • Fully Accessible And SEO Friendly Ajax Paging Using DataPager

    Hey All,

    Working on my current project I implemented paging using a listview and a datapager. I then decided it would be much nicer to use AJAX for my paging so wrapped all this up in an updatepanel. Next step was the issue when you would goto a page, select a product and hit the browser back button you would get the first page not the page you were last on. To fix this I simply implemented the ASP.NET AJAX Futures History control which allowed me to save my current page and restore this at a later time.

    Perfect I thought until I started thinking about SEO, now my product catalogue was dead to a search engine as it would only see the first page and not be able to do any further paging. To fix this I went about creating a SEO friendly linkbutton control (I have blogged about this a while back but this is the first time I used it in real life). Basically what the SEO Friendly linkbutton does is render a normal navigateURL and the postback as an onclick. This way with Javascript turned on you get a postback but without you have a normal URL, in my case I am passing the page # in my url like so: http://site.com/catalogue/page-XX/Whatever.aspx, I am using URL Rewriter.NET for my URL rewriting so making a nice URL for this was as simple as adding a new rule into my web.config.

    Firstly here is my custom SEOLinkButton control (Its in VB.NET as is my current project, I have a C# version too but will just post the VB.NET version unless requested):

    Public Class SEOLinkButton
        Inherits LinkButton

    #Region "Properties"

        Public Property NavigateURL() As String
            Get
                Return If(ViewState("NavigateURL") Is Nothing, "", ViewState("NavigateURL").ToString())
            End Get
            Set(ByVal value As String)
                ViewState("NavigateURL") = value
            End Set
        End Property

    #End Region

        Protected Overrides Sub AddAttributesToRender(ByVal writer As System.Web.UI.HtmlTextWriter)

            If (Me.Page IsNot Nothing) Then
                Me.Page.VerifyRenderingInServerForm(Me)
            End If

            Me.EnsureID()
            writer.AddAttribute(HtmlTextWriterAttribute.Id, Me.ClientID)

            If (Not String.IsNullOrEmpty(Me.CssClass)) Then
                writer.AddAttribute(HtmlTextWriterAttribute.Class, Me.CssClass)
            End If

            If (Not Me.Enabled) Then
                writer.AddAttribute(HtmlTextWriterAttribute.Disabled, "disabled")
            End If

            If (Not String.IsNullOrEmpty(Me.NavigateURL) AndAlso Me.Enabled) Then
                ' Set the href to be our navigateUrl.
                writer.AddAttribute(HtmlTextWriterAttribute.Href, Me.ResolveUrl(Me.NavigateURL))
            End If

            If (Me.Enabled) Then

                Dim customScript As String = Me.OnClientClick

                If (customScript.Length > 0 AndAlso Not customScript.EndsWith(";")) Then
                    customScript = customScript + ";"
                End If

                Dim opts As PostBackOptions = Me.GetPostBackOptions()
                Dim evt As String = Nothing

                If (opts IsNot Nothing) Then
                    evt = Me.Page.ClientScript.GetPostBackEventReference(opts)
                End If

                ' The onclick now becomes our postback, and the appended custom script.           
                writer.AddAttribute(HtmlTextWriterAttribute.Onclick, String.Format("{0}; {1} return false;", evt, customScript))

            End If

        End Sub

    End Class