Archives / 2008 / November
  • How to Read the HTML of a Web Page Programmatically

    We might need to read the contents of some page (local or remote) by code. This is quite simple in .net.

    using System.Net;
    using System.IO;

    WebRequest req = WebRequest.Create("");
    WebResponse res = req.GetResponse();
    StreamReader sr = new StreamReader(res.GetResponseStream());
    string html = sr.ReadToEnd();

    The string html will then hold the html contents of We can also use relative uris in the same website:

    WebRequest req = WebRequest.Create(new Uri("somepage.aspx", UriKind.Relative));

    Hope that helps.


  • ACT: CollapsiblePanelExtender - How to Collapse / Expand Programmatically

    The CollapsiblePanelExtender is a cool way to convert simple panels to collapsible ones. It usually has a target panel, a control which can make it collapse, another (or the same) control to make it expand etc. Clicking the said controls will trigger the collapse/open behaviour.

    We may need to sometimes control this behaviour from code - both client side javascript and server side code behind. Suppose we have a CollapsiblePanelExtender with the Id 'cpe1'. We wish to manipulate the state via code.

    Client Side

    Client side manipulation is pretty simple and can be found in the tutorials section in - we just need to call these methods:

    To open: $find('cpe1')._doOpen();
    To close: $find('cpe1')._doClose();

    The $find tracks down the behaviour object and calls the _doOpen or _doClose methods.

    Server Side

    This is surprisingly slightly trickier. You'd think that just calling

    ScriptManager.RegisterClientScriptBlock(this, typeof(Page), DateTime.Now.ToString(), "$find('cpe1')._doOpen();", true);

    would work. It doesn't. Any attempt to call $find('cpe1') results in a null. This is likely because of the fact that behaviours are added at the end and our script gets registered before the behaviour is available. The actual way to do it is actually quite simple (once you know how):

    To open: cpe1.Collapsed = true; cpe1.ClientState = "true";
    To close: cpe1.Collapsed = false; cpe1.ClientState = "false";

    Setting just the Collapsed property is not enough, you need to set both. It's actually a lot less messy than registering javascript, but it is very annoying until you know it.

    Hope that helps.


  • LINQ to SQL dbml - More than just for accessing a db

    Hi guys...I've been gone nearly a month now. I've finished the last of my classes of my last year of my BSc degree (final exams are in January) and we were having all sorts of grad parties. Add to that finishing off a freelance project and my new role as one of the moderators of, blogging had to take a back seat. Anyway, I'm back :)

    I recently formatted my dev machine with a fresh install of everything. I always keep my documents and all projects in a drive other than my OS drive, so I didn't think much of it. To my horror, I found that a pet project that I had worked on a couple of months ago had its db in the default instance of SQL Server. I had backed up scripts for each db other than that one. I'd worked quite a bit on that project before putting it on hold. And there I was - having lost the database. The db was relatively complex and it would've been a major pain trying to recreate the db. I could start afresh and try to make it work with the site code I had - but I really really didn't want to. Looking through my source files, I found that amazing dbml file. And I was overjoyed. Why? Pretty simple.

    Linq to SQL dbml files can be used to create the database, as well as access it. I fired up VS, added a page, made it the default and added two simple lines of code in Page_Load:

    StudioMorphDBDataContext dc = new StudioMorphDBDataContext();

    I made sure that there was correct connection info in the web.config and ran the page. And behold - I had a database with all the tables and stuff. Of course, things like stored procs and functions won't get recovered, but at least it gave me the schema (with all the relations) back. It saved me from having to design and create the tables from scratch.

    It's a miracle :D