Archive for the 'Code snippets' Category

Get the IP Address and Referer of a visitor

Here is the code snippet to obtain the IP Address and Referer of a visitor to your website Dim IPAddress=Request.ServerVariables(“REMOTE_ADDR”) Dim Referer=Request.ServerVariables(“HTTP_REFERER”) Check a demo here Check my IP

Replace “\n” with “<BR\>”

There are lot of times we accept user input in a textarea or a multiline textbox and save it to a database. But while displaying the data from the database, you will find that the line breaks are not displayed properly, infact there will be no line breaks. We will need to convert all the [...]

Efficient Search Engine Friendly Gridview Paging control

The Gridview Control in ASP.NET has built in paging support. But here are a few disadvantages of using the built in paging It fetches all the records from the database even when we need only 10 records per page. This is quite inefficient when your tables have thousands of records The paging controls uses javascript [...]

How to access a control in the masterpage ?

Suppose you have a label control name lblMessage in your masterpage and you want to access the label control from one of your pages, you can do so using the following code Ctype( Page.Master.FindControl(“lblMessage”),Label).Text = “Hello”

Insert flash file correcty in ASP.NET masterpage

Many people have problems inserting flash files in masterpages . The following code is the correct way of inserting flash file in a mastepage . It will work in all situations <object classid=”clsid:D27CDB6E-AE6D-11cf-96B8-444553540000″ codebase= “http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=7,0,19,0″ width=”663″ height=”105″ title=”Serious Reading….”> <param name=”movie” value=’<%= Request.ApplicationPath + “main_anim.swf” %>’ <param name=”quality” value=”high” /> <embed src=’<%= Request.ApplicationPath + “main_anim.swf” [...]

Format dates in a Gridview column

To format a gridview column to display dates properly , two properties needs to be changed, the DateFormatString property and the HTML encode property. After setting the DateFormatString the HTMLEncode property should be set to false. For Example : <asp:BoundField DataField=”RegistrationDate” DataFormatString=”{0:MM/dd/yyyy}” HeaderText=”Date” HtmlEncode=”False” SortExpression=”RegistrationDate” />

Enable or disable the delete link in the GridView according to the role of the logged in user

In some scenarios you may only want users of a particular role to delete records from the GridView. So you need a mechanism to disable/hide the delete link in the gridview based on roles. The code below will enable the delete link only for “Admin” roles and disable it for the rest. Protected Sub gridView1_RowDataBound(ByVal [...]

How do I reference a server control in javascript?

<%=ServerControlID.ClientID%> Yes that’s all you need . For Example suppose you have a asp.net textbox server control whose ID is “Textbox1″ . In your javascript code to get the value of the control you will use something like this var myValue= document.getElementById(‘<%=TextBox1.ClientID%>’).value;