Friday, November 27, 2009

ASP.NET Validation Controls - Conditionally validating

Using the server side Page.Validate() and Client Side Page_ClientValidate() , we can validate a group of controls conditionally. For eg. i just need to validate some fields only when a checkbox is checked. Both Page.Validate() and Page_ClientValidate() allows to pass a validation group , So we group a set of controls with a validation group and calls this validation functions in both client & server side only on the required validation condition

Server Side



protected void btnSubmit_Click(object sender, EventArgs e)
{
if(cbValidate.Checked)
{
//validate
Validate("vgSubmit");
}
if (!IsValid) return;
lblMsg.Text = "Passed validation";
}



ClientSide


function CheckValidation() {
var cbValidate = document.getElementById('<%=cbValidate.ClientID %>');
var flag = true;
if (cbValidate.checked) {
if (!Page_ClientValidate("vgSubmit"))
flag = false;
}
else {
Page_ClientValidate("vgDummy")
}
return flag;

}



Download Code

Wednesday, September 23, 2009

URL Rewriting with ASP.NET 3.5 using System.Web.Routing.UrlRoutingModule

In asp.net 2.0 we used Context.RewritePath() or other URL rewrite modules. With asp.net 3.5 its easy to do. I did it for my blog for better SEO.

      • Add reference to system.Web.Routing
        Add System.Web.Routing.UrlRoutingModule http module to web.config
        Implement an IRouteHandler
        Registering routes in global.asax

I am going to rewrite blogs/Posts/{BlogPostID}/{*BlogPostTitle}

I implemented a generic IRouteHandler , it will copy url parameters( eg: BlogPostID,BlogPostTitle ) to http context item collection, so i can URL rewrite any page , without modifying IRouteHandler implementation.



using System;
using System.Web;
using System.Web.Routing;
using System.Web.Compilation;
using System.Web.UI;
public class SiteRouteHandler : IRouteHandler
{
//30 june 2009 Priyan R

public IHttpHandler GetHttpHandler(RequestContext requestContext)
{
Page page;
page = BuildManager.CreateInstanceFromVirtualPath(PageVirtualPath, typeof(Page)) as Page;
foreach (var item in requestContext.RouteData.Values)
{
HttpContext.Current.Items["qparam." + item.Key] = item.Value;
}
return page;
}
public string PageVirtualPath { get; set; }
}

In global.asax added


       

routes.Add(
"BlogPost",
new Route("Blogs/Posts/{BlogPostID}/{*BlogPostTitle}",
new SiteRouteHandler() { PageVirtualPath = "~/Blogs/Details.aspx" })
);


So in Details.aspx I can read the parameters



Context.Items["qparam.BlogPostID"].ToString()
Context.Items["qparam.BlogPostTitle"].ToString()

Check the code.


Download Code

Wednesday, August 12, 2009

A simple Rss Feed Parser using LINQ To XML

Download Code

A simple class that can be used to parse rss feed, Check the code attached it will display my twitter updates by parsing the rss feed using the class.


using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Xml.Linq;
namespace RssFeed
{

//12-aug-2009 Priyan R
public class RssParser
{
public RssParser()
{
Items = new List<RssItem>();
}
public RssParser(string url):this()
{
URL = url;
}
#region Methods
public void Parse()
{
XDocument doc = XDocument.Load(URL, LoadOptions.None);
Items = (from t in doc.Descendants("item")
select new RssItem()
{
Title = t.Element("title").Value,
Description = t.Element("description").Value,
Link = t.Element("link").Value

}
).ToList();

}
#endregion
#region properties
public string URL { get; set; }
public List<RssItem> Items { get; set; }
#endregion
}
#region RSS Item Class
public class RssItem
{
public string Title{ get; set; }
public string Description { get; set; }
public string Link { get; set; }
}
#endregion
}

Friday, June 12, 2009

Finding Total Records / Total Count returned By Object Datasource SelectCountMethod

Often SelectCountMethod may be int or long so check the type in object datasource Selected event and store the value in ViewState or a variable so it can be used in the page

protected void odSource_Selected(object sender, ObjectDataSourceStatusEventArgs e)
{
if (e.ReturnValue.GetType() == typeof(Int32))
{
ViewState["TotalCount"] = e.ReturnValue;
}
}

Sunday, May 10, 2009

I Moved To New VPS Hosting

I moved my websites to a new VPS hosting.For the last 6 months I was managing my own server. here in my place its not trusty ISP problems/Power/Cable , so I am leaving it.
For the last two weeks I was experimenting running windows in virtual machine inside mosso-rackspace cloud using QEMU, Mosso is great but currently they provide only linux , surely they will come with windows,

Now I am using VPS by http://www.infinitelyvirtual.com/.They are not well known, but I trust them. Their plans are cheap and good, 19.99$ 512mb 10gb space 500 GB monthly data transfer. They are using vmware based virtualization.

Saturday, May 9, 2009

Story : Installing WIndows On Mosso RackSpace Cloud With QEMU

Rack space cloud is a very good service with low price. Good customer support, i always got chat session with customer support very soon. They currently provide Linux cloud server only. I am waiting for windows cloud server.

I installed windows 2003 in QEMU , i was not able to Get the KQEMU accelaration layer working. So performance was really poor,


Installation story (Installing 32 bit windows 2003)....


First i created an ubuntu 8.04.2 LTS (hardy) cloud server.

1. Update package list

apt-get update
apt-get upgrade

2. Install QEMU

apt-get install qemu

2. Install X Server

aptitude install xorg

3. Redirect X Display to our system

This was the one main problem i faced. I 'X' forwarded the display to windows pc running XMing. It was really slow.. It took abt 50-100MB data transfer to show the initial windows loading screen.. Took long time to display the screen.. So it is impossible to install windows in this way.

I found a solution X11 VNC

4. Install X11 VNC

apt-get install x11vnc

apt-get install xvfb

Create password file


x11vnc -storepasswd

5. Start X11 VNC create option (will automatically create a display)

x11vnc -usepw -create


6. Connect to X11 VNC From windows using TightVNC


7. Download windows 2003 ISO ( i used windows 2003 trial)


8. Create QEMU hdd image file


qemu-img create win2003.img 20G


9.Install windows

Run qemu with win 2003 iso as cdrom

qemu -m 256 -boot d -cdrom win2003.iso -hda win2003.img -localtime



Problems faced


32 bit windows 2003 installation stuck at installing devices screen , but on restarting once it worked fine.


NetWorking


I was not able setup TAP network working. I used user mode networking with port forwarding option

I run this to forward 3389 port for remote desktop , and 80 for IIS

qemu -hda win2003.img -m 256 -localtime -net nic,model=rtl8139 -net user -redir tcp:3389::3389 -redir tcp:80::80



Installing 64 bit Windows 2003


The qemu version we get with apt-get will not work with win 2003 64
I got stuck "Starting windows" while installing
http://qemu-forum.ipi.fi/viewtopic.php?f=9&t=4906


I installed QEMU 0.90 from source , we need gcc 3.x for doing that,ubuntu comes with gcc 4.x so install gcc 3.4

sudo apt-get install gcc-3.4 g++-3.4

export CC=gcc-3.4

Download qemu 0.9.0 source , then compile & install


wget http://tx-us.lunar-linux.org/lunar/cache/qemu-0.9.0.tar.gz

tar xzf qemu-0.9.0.tar.gz

cd qemu-0.9.0

./configure
make
make install

I got an error while comiling qemu

Looking for gcc 3.x ./configure: 372: Syntax error: Bad fd number

To fix this edit the ./configure file and change the first line from "#!/bin/sh" to "#!/bin/bash".


Now i was able to install windows 2003 64

qemu-system-x86_64 -m 256 -boot d -cdrom win2003.iso -hda win2003.img -localtime


Installing KQEMU Acceleration Layer

I tried but was not able to get it work

I tried both apt-get kqemu and compiled qemu 0.9.0 and kqemu-1.3 from source,

1st i tried to install with kqemu it got stuck while "starting windows"

Next i tried with -no-kqemu option now 2003 64 installed successfully
After installation i tried with acceleration, initial loading screen came after it screen gone blank.

I installed using

wget http://www.nongnu.org/qemu/kqemu-1.3.0pre11.tar.gz

tar xzf kqemu-1.3.0pre11.tar.gz
cd kqemu-1.3

./configure
make
make install

modprobe kqemu




Installation Experience



Windows 2003 32 bit got stuck while "installing devices" , after restarting it worked fine.

Windows 2003 64 bit installed with not problem

It took 4-5 hours to complete the installation !!!!

Really need great patient to do.. Some steps will take long time with not progress moving.. But may not be stuck.. need waiting...


Windows Experience



Its usable but really slow.. I ran some asp.net sites on that including my home page, its a little bit slow.. but not so bad.
To install .net framework 3.5 it took 1hr!!

Wednesday, April 8, 2009

Me And Sumesh

I saw sumesh for the second time this monday ( i think after 2 years) . We are often in contact(phone,chat) with each other with programming related for the past 4-5 years.. He is the only programmer i know in Alappuzha, We both started with electronics, programming we both came from a VB6 background , We both are self taught programmers with good experience in Programming/System administration.