Friday, December 12, 2008

Proper use of StringBuilder

'Declare the StringBuilder variable
Private body As New System.Text.StringBuilder("")
'Append the string into the StringBuilder
body.Append("Hello world!!
")
body.Append("Hi!!
")
'Retrieve the value of the StringBuilder
body.toString

OUTPUT:
Hello world!!
Hi!!

Cool right? StringBuilder is much recommended than using the usual concatination symbols like + and &.

Method in .NET that uses javascript to display an alert box before redirecting to a new page

Public Sub RedirectBox(ByVal page As System.Web.UI.Page, ByVal message As String, ByVal URL As String)
Dim Script As String = "
"

page.ClientScript.RegisterStartupScript(Me.GetType, "RedirectBox", Script)
End Sub

This method is very useful when programming in .NET and you want to display an alert message in your page before the page would redirect to another page. You could call this method for example to display the success message alert after a successful transaction before going back to the home page. Usually if we just use window.location after displaying alert, the tendency is that the alert box is not displayed and the page will immediately be redirected to the new URL. Using ClientScript.RegisterStartupScript, the alert will first display and the page will only redirect after clicking the OK button of the alert box.

Method to display an alert box in .NET using javascript

Public Sub MessageBox(ByVal page As System.Web.UI.Page, ByVal message As String)
Dim Script As String = ""
page.ClientScript.RegisterStartupScript(Me.GetType, "MessageBox", Script)
End Sub

This method is very useful when programming in .NET and you want to display an alert message in your page. You could call this method for example to display the success message alert after a successful transaction.

Saturday, December 6, 2008

Javascript to disable all controls upon Button click

function DisableButton()
{
document.forms[0].submit();
window.setTimeout("disableButton()", 0);
}

function disableButton()
{
for(i = 0; i <= document.forms[0].elements.length; i++) {
elm = document.forms[0].elements[i];
elm.disabled=true;
}
}

This is to prevent clicking the button more than once. This javascript code has already been tested to be working. All controls are disabled including the button itself after it is clicked. Remember to set the Submit behavior of the button to false so that it will not cause a postback when clicked.

Javascript to check all datagrid checkboxes

function CheckAllDataGridCheckBoxes(aspCheckBoxID, checkVal)
{
for(i = 0; i <= document.forms[0].elements.length; i++)
{
elm = document.forms[0].elements[i]
if (elm.type == 'checkbox')
{
elm.checked = checkVal;
}
}
}
This code snippet has been tested to work on a datagrid with checkboxes named as aspCheckBoxID. The input parameter of checkVal is the value of the checkbox which is either true or false (true if you want to check all check box, and false if otherwise).