Tuesday, June 9, 2009

Copy code as HTML on Visual Studio, easy way to copy code on blogspot

I just notice that blogspot has limited capability on doing blog for programming code. Copy and paste from word also won't work properly as blogspot will not tolerate some whitespace character generated by Word. Luckily, on the net I found something that can address this issue.

Guy Burstein, provide a link to Microsoft Visual Studio AddIns that will allow us to copy any code that we highlight within Visual Studio and paste it automatically as HTML code. This will be helpfull lot on writing a code blog on blogspot, eventough we have to use manual HTML code still and it's exclusively to Visual Studio :)

So, here are steps to doing this:

1. Obtain AddIns here

2. Extract it to your Visual Studio AddIns folder, it's should be under MyDocuments\Visual Studio 2008\Addins, if you couldn't find AddIns folder, then create new one

3. Open Visual Studio, click on Tools -> AddIn Manager


4. AddIn manager dialog will be opened, select to activate "Copy as HTML" AddIn


5. Now, you can select any code and when do right click, "Copy as HTML" will be shown

Below is the result :)


  195 Public ReadOnly Property Email() As String


  196             Get


  197                 Dim daUser As New UserTableAdapter


  198                 Dim ret As String = ""


  199                 Dim users() As String = {}


  200                 Dim i As Integer = 0


  201 


  202                 For Each row As SecurityApprovalRow In Me.Rows


  203                     If Array.IndexOf(users, row.UserId) < 0 Then


  204                         Array.Resize(users, i + 1)


  205                         users(i) = row.User.Email


  206                         i += 1


  207                     End If


  208                 Next


  209                 Return IIf(users.Length = 0, "", String.Join(";", users))


  210             End Get


  211         End Property





Nice isn't ? Try it, code with smile :)

Saturday, May 30, 2009

Flip your windows screen permanently

This one maybe not related with programming, but it may a funny trick that we could use to punk someone with. Okay, from Windows operating system (I use Windows XP SP2)  on working mode, means it's not locked state, pres Ctrl+Alt+[Any arrow], and your screen direction will follow arrow you are pressed, for instance to flip your screen 180 degree, press Ctrl+Alt+[down arrow] :)

 



So, say, you have someone that you crush on him/her, you can silently flip his/her screen, and then when he/she notice that something wrong with his/her monitor and get stuck on how to make it like it used to be, you may appear as hero who save the day ... code with smile.

Thursday, May 28, 2009

How to do check and uncheck all record on ASP.Net GridView using JavaScript and ASP.Net


When we accessing common webmail site like Gmail, Yahoo!mail or Hotmail, we will see a feature that allow us to select all message listed with a single click, either on a link says "Check All" or a checkbox that will trigger same action somewhere on its header, for the same if want to uncheck them. To do such feature above on ASP.Net Application, I would say impossible with just using standard control, ASP.Net GridView for instance, you would need to get enhanced control or else programmatically coding it in your application :)

On every application that was built by our team, we try to as much as possible not to do it on server side, as it will increase data round trip which as we all know will reduce application performance. So, combination of ASP.Net, Ajax and JavaScript come as a solution.

First we need to have to add TemplateField on GridView to show CheckBox, in our approach we also use a CheckBox on this column header to trigger Check/UnCheck operation by bind a JavaScript function on this CheckBox's OnChange event.

Next, we will need to write down JavaScript function that will be used to perform Check/UnCheck operation, our code is as follow:

var checkMes = new Array();

function addCheckMe(checkMe_CtlId)

{

checkMes.length++;

checkMes[checkMes.length-1]=checkMe_CtlId;

}

function doCheckUncheckAll()
{
var tmpCheckMe;
var checkAll=$get("CheckAll");

for(i=0;i { tmpCheckMe=$get(checkMes[i]); tmpCheckMe.checked=checkAll.checked; }
}


You may see, we have one array to store list of checkbox, a function to fill that array and another function to trigger Check/Uncheck operation.

Okay with done with defining checkbox and JavaScript thing, next we need to call addCheckMe function within our ASP.Net code as GridView will define checkbox dynamically, so ... we add a function to handle GridView's RowDataBound event

protected void grd_RowDataBound(object sender, GridViewRowEventArgs e)
{

string addCheckMeScript = "";
CheckBox CheckMe = (CheckBox)e.Row.FindControl("CheckMe");

if (CheckMe != null)
{
ScriptManager.RegisterStartupScript(this.Page, this.Page.GetType(), String.Format("OnGrdCheckMeRegister_{0}", e.Row.RowIndex), String.Format("addCheckMe('{0}');", CheckMe.UniqueID.Replace("$","_")), true);
}
}

And voilla we get as we have expected ... code with smile :)

Tuesday, May 26, 2009

How to make an ASP.Net application offline within second

Sometime it neccesary to make an application offline, it may possible due to code upgrade process or essential data upload. Before ASP.Net framework 2.0 era, normally we will place a code on global.asa or global.asax to set a application variable to indicate offline status to true, then programatically read this status and redirect to specific page to inform if an application is offline.

There is an approach to make it simple for a developer to make its application offline on ASP.Net 2.0, by using app_offline.htm. Simply just design a page using standard HTML editor and named it as app_offline.htm then place this file on root of application that you would offline it.

Validate text to match specific date pattern on JavaScript

My team got requirement to standarize date appearance to dd-MMM-yyyy, thanks to ASP.Net Ajax who has CalendarExtender tools so we may manipulate the way date format returned.

Unfortunately, we also allow user to free type value of date on any datetime control we have provided, which causing us a problem on how to restrict it to expected format. CustomValidator come in handy as its has capability to validate thru JavaScript, so next problem is how to build a javascript code to validate its format to be dd-MMM-yyyy, as we need a handy script and fight with deadline, so below code is written:

function isDate(sDate) {
var datePat = /^(\d{1,2})(\/|-)(\w{3})(\/|-)(\d{4})$/;
var matchArray = sDate.match(datePat); // is the format ok?

if(matchArray==null) return false;

return true;
}


Above code only validated date written format as per expected pattern, it won't check validity of data value entered, for instance if you type: 45-Apr-2009, script will return true, so that problem stil a homework for our team :)

Monday, May 25, 2009

Hello World

Hi, thanks for visiting this weblog, uhm ... like its name, the purpose of having this blog is to share my experience on software & web development :)

Okay, have nice reading (I mean when there would be enough post to be read) and looking forward for your next visit :)