Thursday, July 30, 2009

ASP.Net RadioButtonList and JQuery

This is a short one, but I had to search around quite a bit for an answer. The internet is peppered with answers which were probably right for the version of jQuery they referred to. This sample uses jQuery 1.3.2.

Question
:
How to access the value of the ASP.Net RadioButtonList in jQuery ?

Answer:

$('#radioButtonListClientId').find('input[checked]').val();


Remember "radioButtonListClientId" referred above is the ClientID and not the ID of the server control.

Happy Coding!

ravi

Thursday, July 16, 2009

Generic XML Serialization / De-Searialization

Generic Serialize and Deserialize Methods:

public static XmlDocument Serialize<T>(T xmlObject)
{
var xmlDocument = new XmlDocument();
var xml = new StringBuilder();
var serializer = new XmlSerializer(typeof(T));
var writer = new StringWriter(xml);
serializer.Serialize(writer, xmlObject);
xmlDocument.LoadXml(xml.ToString());
return xmlDocument;
}

public static T Deserialize<T>(XmlDocument xmlDocument)
{
T result;
XmlSerializer serializer = new XmlSerializer(typeof(T));

using (StringReader stringReader = new StringReader(xmlDocument.InnerXml))
{
using (XmlTextReader xmlReader = new XmlTextReader(stringReader))
{
result = (T) serializer.Deserialize(xmlReader);
xmlReader.Close();
}
stringReader.Close();
}

return result;
}


Sample Usage:

//Serialize
XmlDocument xmlPerson = Serialize(person);

//Deserialize
Person anotherPerson = Deserialize<Person>(xmlPerson);

Setting Up Print view when using a Master Page

Lets say you are using Master Pages in you ASP.Net Web Site or Application and want to set up a print view for your users. Your master page has top navigation / side navigation etc which you do not want to appear in the print view. A simple solution is to pass a print query string in the URL and change your master page to a one suitable for print view. You will need to do this in the Page_PreInit event.

Code Sample:
void Page_PreInit(Object sender, EventArgs e)
{
if (Request.QueryString["print"] == 1)
{
MasterPageFile = "~/MasterPage/Print.Master";
}
}

[ BTW, the above code syntax highlighting is by http://alexgorbatchev.com/wiki/SyntaxHighlighter. Absolutely love this! Thank you, Alex. ]

Happy Coding!

Ravi







Reblog this post [with Zemanta]

Friday, July 3, 2009

Using JQuery UI Dialog with ASP.Net and AJAX Update Panel

One of the first things you will notice when you try and use JQuery UI Dialog in ASP.Net page is it does not perform post backs from within the dialog window. This is because in a ASP.Net page there is only one form element and Jquery appends the dialog "div" outside of the form element. There is a easy way to fix this. And once its done then its simple matter of combining some ASP.Net AJAX Update Panels and you are ready to go.


Lets look at a simple but non-trivial example of combining JQuery UI Dialog, Asp.Net and AJAX Update Panel. You can download the source of the sample here. Our sample application will display a list of names. To edit a name, you click on a name.



This will pop up a JQuery UI Dialog with the current name. You can edit the name and click save. This will save and automatically close the dialog.



For adding a new Name, click on the "Add New Person" button. And this will bring up the new person dialog and allow you to save. Adding or Editing names will automatically update the list with the changes.



I am not going to go through explaining each line of the source. Its much easier to read the source code for yourself. I will just point out the main highlights. The first thing you will notices is when setting up the JQuery dialog, we are adding a open event to attach the dialog div to the form element. This will insure the post backs to happen.


Secondly, We set up update panels for each of the dialogues. It is important to note that the Update Panels are nested inside the dialog div. And also the GridView to display the list is also within an Update Panel. All of our update panels, UpdateMode is set as "Conditional" and ChildrenAsTriggers as "true". So they only update when some control within them posts back and we can also trigger the update by their Update method from the server side.

OK, now the code behind. To keep thing simple this example uses a Dictionary to store the names in the viewstate. Most of the code here is self explanatory. However I will point out the use of "ScriptManager.RegisterClientScriptBlock" to close the dialog. RegisterClientScriptBlock helps in adding javascript to be executed on asynchronous post back.

That's pretty much it. Give me a yell if you have any questions or some suggestions.

Happy Coding

Ravi

Download Sample Code

Thursday, July 2, 2009

Preparing your team for their very first Agile Scrum

If not all members of your new team have previously worked using the Agile Scrum framework, it would help to lay some groundwork. To start with, I would suggest taking your team though the concise 90 minute Agile scrum presentation by Mountain Goat Software. It is fully re-distributable and reusable. This would be a excellent starting point for anybody brand new to Agile/Scrum.

Secondly I would recommend your team to read this book "Scrum and XP from the Trenches" by Henrik Kniberg. This is around 120 pages and nice easy read over the weekend. The ebook version is free to download. (Thank you, Henrik Kniberg).

These two together will help your team to get a good grounding of the Agile Scrum framework. But don't expect the first couple of sprints to be great! That will only come with experience.

Happy Sprinting

Ravi