LINQ To XML Tutorials That Make Sense

Sometime back, I had posted some LINQ to XML Tutorials on dotnetcurry.com demonstrating some common operations with 'How Do I’ kind of LINQTOXML examples. Check them out over here:

Some Common Operations using LINQ To XML - Part I

Some Common Operations using LINQ To XML - Part II

Some Common Operations using LINQ To XML - Part III

You can also check my other articles on LINQ over here LINQ Articles

Hope you like them all!

Collapse all Open Nodes Of an ASP.NET TreeView when Another Node is expanded

Users have a very common requirement around the ASP.NET TreeView - that is to keep only one TreeNode expanded at a given point of time. So if a TreeNode in the TreeView is expanded, collapse the other open TreeNodes. Here's how to do so:


 <asp:TreeView ID="TreeView1" runat="server"


        ontreenodeexpanded="TreeView1_TreeNodeExpanded">


        <Nodes>


            <asp:TreeNode Text="Node1" Value="Node1">


                <asp:TreeNode Text="Node1.1" Value="Node1.1"/>


                <asp:TreeNode Text="Node1.2" Value="Node1.2"/>


                <asp:TreeNode Text="Node1.3" Value="Node1.3"/>


            </asp:TreeNode>


            <asp:TreeNode Text="Node2" Value="Node2">


                <asp:TreeNode Text="Node1.1" Value="Node2.1"/>


                <asp:TreeNode Text="Node1.2" Value="Node2.2"/>


            </asp:TreeNode>


            <asp:TreeNode Text="Node3" Value="Node3">


                <asp:TreeNode Text="Node3.1" Value="Node3.1"/>


                <asp:TreeNode Text="Node3.2" Value="Node3.2"/>


            </asp:TreeNode>


        </Nodes>


</asp:TreeView>




C#


protected void TreeView1_TreeNodeExpanded(object sender, TreeNodeEventArgs e)


{


    // Loop through all nodes


    foreach (TreeNode treenode in TreeView1.Nodes)


    {


        // If node is expanded


        if (treenode != e.Node)


        {


            // Collapse all other nodes


            treenode.Collapse();    


        }


    }


}




VB.NET


    Protected Sub TreeView1_TreeNodeExpanded(ByVal sender As Object, ByVal e As TreeNodeEventArgs)


        ' Loop through all nodes


        For Each treenode As TreeNode In TreeView1.Nodes


            ' If node is expanded


            If treenode IsNot e.Node Then


                ' Collapse all other nodes


                treenode.Collapse()


            End If


        Next treenode


    End Sub


Windows 7 Beta will Auto Shut Down every 2 hours from July 1st 2009

It's official now. The Bi-hourly shutdowns for the Windows 7 Beta will begin July 1st, 2009 and will expire on August 1st 2009. This post comes as a saver, since a few days ago, all Windows 7 Beta users got an email miscommunicating that the bi-hourly shutdowns will begin June 1st 2009, instead of July 1st, 2009.

So if any one of you are still using Windows 7 Beta, you have around 34 days from now to download and install the Windows 7 RC, saving you from going through the pains of seeing your PC shutting down every 2 hours. Ahemm..sound familiar eh!

Changing the Background Color of an ASP.NET AJAX CalendarExtender

By default, an ASP.NET AJAX CalendarExtender looks similar to the following:



However if you want to change the header/background color of an ASP.NET AJAX CalendarExtender, then here's how to do it using CSS.


<head runat="server">


    <title></title>


    <style type="text/css">


        .cal .ajax__calendar_header


        {


              background-color: Silver;


        }


        .cal .ajax__calendar_container


        {


                background-color: Gray;


        }        


    </style>


</head>


<body>


    <form id="form1" runat="server">


    <div>


        <asp:ScriptManager ID="ScriptManager1" runat="server">


        </asp:ScriptManager>


        <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>


        <cc1:CalendarExtender ID="CalendarExtender1" runat="server" CssClass="cal"


        TargetControlID="TextBox1" >


        </cc1:CalendarExtender>


    </div>


    </form>


</body>


</html>




After applying the CSS, the CalendarExtender will look similar to the following:

Bind an ASP.NET DropDownList to a Dictionary

I have seen a lot of folks keeping data in a Dictionary<,>. However when it comes to binding this Dictionary with a control, they feel lost!

Here's a simple way that demonstrates how to bind a Dictionary to an ASP.NET DropDownList control


        <asp:DropDownList ID="ddlRelatives" runat="server">


        </asp:DropDownList>




C#


    Dictionary<int, string> dicRel = new Dictionary<int, string>();


    dicRel.Add(1, "Father");


    dicRel.Add(2, "Mother");


    dicRel.Add(3, "Brother");


    dicRel.Add(4, "Sister");


    dicRel.Add(5, "Others");


    ddlRelatives.DataSource = dicRel;


    ddlRelatives.DataTextField = "Value";


    ddlRelatives.DataValueField = "Key";


    ddlRelatives.DataBind();




VB.NET


    Dim dicRel As New Dictionary(Of Integer, String)()


    dicRel.Add(1, "Father")


    dicRel.Add(2, "Mother")


    dicRel.Add(3, "Brother")


    dicRel.Add(4, "Sister")


    dicRel.Add(5, "Others")


    ddlRelatives.DataSource = dicRel


    ddlRelatives.DataTextField = "Value"


    ddlRelatives.DataValueField = "Key"


    ddlRelatives.DataBind()


Tech.Ed India 2009 - What a blast I had!

Another episode of TechEd was successfully executed at Hyderabad, India. I know this post comes a little late since the event got over on the 16th and I am 9 days late in posting this. However I HAD to share, my experience with you guys, especially the one where I met Stephen Walther and exchanged ideas and feedback with him!



Here are a few highlights of the event:

Keynote by Steve Ballmer - The star attraction of Tech.Ed India 2009, the keynote by Steve Ballmer, was a rock on, where he spoke about the current market situations and how to beat the recession heat!



Rock solid Technical Sessions - including the one from Stephen Walther. For those who do not know much about Stephen Walther, he is the author of the very famous ASP.NET book ASP.NET 3.5 Unleashed. This is one book you MUST have in your collection if you are an ASP.NET developer and want to get a deep understanding of the technology. This is one of the best books i have had.

Stephen also has an upcoming book (much awaited) on ASP.NET MVC ASP.NET MVC Framework Unleashed which I highly recommend you must have in your collection.



UPDATE: Stephen's Tech.Ed Sessions and code can be downloaded over here http://stephenwalther.com/blog/archive/2009/05/27/back-from-tech-ed-india.aspx

Keynote by Resul Pookutty - Academy Award Winner-Film Sound Designer (Slumdog Millionaire)



Social Networking - I also got a chance to chit-chat with some great bloggers like Geetesh Bajaj, Pinal Dave and Niraj Bhatt



Food and Entertainment - Food was amazing all the three days coupled with entertainment including live performances by a rock band called Agnee.



To see other photos on the event, check out these captured by Abhishek Baxi.

Overall it was an amazing event with good food, sessions and social networking. I had a lot of experiences to take back home. Oh Boy! What a blast I had!

What's next? Tech.Ed 2010?

Replicating the 'IN' operator in LINQ

We often use the 'IN' Operator to specify multiple values in the WHERE clause. What if you have to do something similar in LINQ. Here's a simple example that demonstrates this. The example searches out the desired pincodes in a list of Booth Addresses.

C#


var pinCodes = new[] { 411021, 411029, 411044 };


var Booths = new[] {


    new { BoothName = "Booth1", PinCode = 411011 },


    new { BoothName = "Booth2", PinCode = 411021},


    new { BoothName = "Booth3", PinCode = 411029 },


    new { BoothName = "Booth4", PinCode = 411044 },


    new { BoothName = "Booth5", PinCode = 411056 },


    new { BoothName = "Booth6", PinCode = 411023 },


    new { BoothName = "Booth7", PinCode = 411024 }


};


 


var whereAmI = from booth in Booths


              join pins in pinCodes


              on booth.PinCode equals pins


              select booth;




VB.NET


        Dim pinCodes = New Integer() {411021, 411029, 411044}


        Dim Booths = New Object() _


        {New With {Key .BoothName = "Booth1", Key .PinCode = 411011}, _


        New With {Key .BoothName = "Booth2", Key .PinCode = 411021}, _


        New With {Key .BoothName = "Booth3", Key .PinCode = 411029}, _


        New With {Key .BoothName = "Booth4", Key .PinCode = 411044}, _


        New With {Key .BoothName = "Booth5", Key .PinCode = 411056}, _


        New With {Key .BoothName = "Booth6", Key .PinCode = 411023}, _


        New With {Key .BoothName = "Booth7", Key .PinCode = 411024}}


 


        Dim whereAmI = _


         From booth In Booths _


         Join pins In pinCodes On booth.PinCode Equals pins _


         Select booth




OUTPUT

Copy Text From One ASP.NET TextBox To Another

Here's a simple way of copying Text From One ASP.NET Text Box to Another as the user types, using JavaScript

Add the following TextBox to your page


<asp:TextBox ID="txtOne" runat="server" onkeyup="OneTextToOther();"></asp:TextBox>


<asp:TextBox ID="txtTwo" runat="server"></asp:TextBox>




Now add the following JavaScript in the <head> section of your page


<head>


    <title></title>


    <script type="text/javascript">


        function OneTextToOther() {


            var first = document.getElementById('<%= txtOne.ClientID %>').value;


            document.getElementById('<%= txtTwo.ClientID %>').value = first;


        } 


    </script>


</head>