Friday, July 9, 2010

General tips

General tips
Insert webpart on NewForm, DispForm or EditForm:
Add “toolpaneview=2″ behind the URL in this format:
…NewForm.aspx?toolpaneview=2
…EditForm.aspx?ID=12&toolpaneview=2

This sets the page in edit-mode and you can add webparts as if it was any other page.

Have you broken your page by adding a faulty script?
Add “?contents=1″ behind the URL and you can delete any webpart from your page.

Find List Guid or view Guid:
In the list view – right click and view source. Then search for “ctx.Listname”.
The Guid looks like this:
{DEF643C7-CF80-4DF8-866F-C16DC017DFC0}.

The view Guid is found by searching for “ctx.view” and looks similar to the list Guid above.

Find FieldInternalName:
Open NewForm, EditForm or DispForm. Right click and view source. Then search for your field’s DisplayName and you will find the fields FieldInternalName like this:

1

The fields InternamName never changes and is therefore the safest method of identifying a field.

Wednesday, June 30, 2010

Avoid errors in Javascript

Many times we see some annoying javascript errors on websites.

I am of the opinion that the javascript errors should not be shown to users. Best way is to write correct javascript, but to be on safer side we can write try...catch in javascript to supress the error.

try
{
//Run some code here
}
catch(err)
{
//Just ignore...don't do anything

//Or try to handle the error
}
Alternatively we can handle the onerror event and supress all Javascript errors occuring on the page.

script type="text/javascript">

window.onerror=function(){
//Don't do anything, just supress the error
}
script>
I hope this helps.

Javascript to display time on Web page

JavaScript sample to continuously display the current time on the web page. Continuously means that the textbox value will be updated with the current time every second.

script type="text/javascript">

function ShowTime() {

var dt = new Date();

document.getElementById("<%= TextBox1.ClientID %>").value = dt.toLocaleTimeString();

window.setTimeout("ShowTime()", 1000);

}

script>



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



script type="text/javascript">

// a startup script to put everything in motion

window.setTimeout("ShowTime()", 1000);

script>


If you are using normal web form then it can also be called on Body onload event. If you are using MasterPage then it can be called within ContentTemplate at the end after all the controls have been rendered.

Javascript to add bookmark option on your website

If it is required to add a facility on the webpage that users can click a button on the website and it will open up bookmark option. Following is the code that will perform this task.










-And if you want it work in IE and firefox as well:

function addToBookMarks()
{

if(document.all)
window.external.AddFavorite(location.href,document.title);

else if(window.sidebar)window.sidebar.addPanel (document.title,location.href,'');
}