Wednesday, March 17, 2010

Highlighting Search Keywords in a HTML DIV Control Using Javascript

In many of the popular search engines (most of all ones), when you perform a search the resulting page displays the word(s) you searched on are highlighted different colors to make them easy to pick out.
This article will examine how to provide such functionality in HTML DIV control with the use of a simple javascript function.

Lets Start…….. :-)

Here is the Javascript function will do that.

objDIV = Div which contains the Text Contents
textToHightLight = Text (Searched Word) which should be Highlighted

function innerHighlight(objDIV, textToHightLight)
{
var skip = 0;
if (objDIV.nodeType == 3)
{
var pos = objDIV.data.toUpperCase().indexOf(textToHightLight.toUpperCase());
if (pos >= 0)
{
var spannode = document.createElement(’span’);
spannode.className = ‘highlight’;
var middlebit = objDIV.splitText(pos);
var endbit = middlebit.splitText(textToHightLight.length);
var middleclone = middlebit.cloneNode(true);
spannode.appendChild(middleclone);
middlebit.parentNode.replaceChild(spannode, middlebit);
skip = 1;
}
}
else if (objDIV.nodeType == 1 && objDIV.childNodes && !/(script|style)/i.test(objDIV.tagName))
{
for (var i = 0; i < objDIV.childNodes.length; ++i)
{
i += innerHighlight(objDIV.childNodes[i], textToHightLight);
}
}
return skip;
}

In the code note that i have set the className for the SPAN Element to 'highlight'. This HTML will apply the style of a particular class to the word we wish to highlight. Feel free to use whatever CSS markup you'd like to in order to have the search word "highlighted." The below CSS will give the highlighted word a yellow background.

.highlight {text-decoration: none;color:black;background:yellow;}

Happy codding!!!!!!!!!! :-)

Friday, January 8, 2010

Resize Bitmap Image with Aspect Ratio

Just Copy this function into your code and use it to resize the bitmap image with corresponding aspect Ratio.

private Bitmap ResizeImage(Bitmap image, int width, int height)
{
   Bitmap OriginalBmp = image;

   int OriginalWidth = OriginalBmp.Width;
   int OriginalHeight = OriginalBmp.Height;
   int OriginalX = 0;
   int OriginalY = 0;
   int NewX = 0;
   int NewY = 0;

   float Percent = 0;
   float WidthPercent = 0;
   float HeightPercent = 0;

   WidthPercent = ((float)width / (float)OriginalWidth);
   HeightPercent = ((float)height / (float)OriginalHeight);
   if (HeightPercent < WidthPercent)
   {
      Percent = HeightPercent;
      NewX = System.Convert.ToInt32((width -       (OriginalWidth * Percent)) / 2);
   }
   else
   {
      Percent = WidthPercent;
      NewY = System.Convert.ToInt32((height -       (OriginalHeight * Percent)) / 2);
   }

   int DestWidth = (int)(OriginalWidth * Percent);
   int DestHeight = (int)(OriginalHeight * Percent);

   Bitmap NewImage = new Bitmap(width, height,    System.Drawing.Imaging.PixelFormat.Format24bppRgb);
   NewImage.SetResolution(OriginalBmp.HorizontalResolution,    OriginalBmp.VerticalResolution);

   Graphics Gr = Graphics.FromImage(NewImage);
   Gr.Clear(Color.White);
   Gr.InterpolationMode =    System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;

   Gr.DrawImage(OriginalBmp, new Rectangle(NewX, NewY, DestWidth,    DestHeight), new Rectangle(OriginalX, OriginalY, OriginalWidth,    OriginalHeight), GraphicsUnit.Pixel);
   Gr.Dispose();
   OriginalBmp.Dispose();
   MemoryStream Ms = new MemoryStream();
   return NewImage;
}

List Object's Properties in Javascript

Here is the Code to List all the Properties of an Object in JavaScript

for (var propertyName in Object)
{
document.writeln(propertyName);
document.write('<BR>')
}

Note:- Object will be replaced by your objectName :-)

Search Text in Database's Objects

Here is the SQL Stored Procedure which will list all the database's Objects(SP, Views, Functions etc...) in which the Searched Text exist.

CREATE PROCEDURE [dbo].[SearchTextInDatabase]
@SearchText VARCHAR(200)
AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;
DECLARE @SQL VARCHAR(MAX)

SET @SQL = 'SELECT Name FROM SysObjects WHERE ID IN
(SELECT ID FROM SysComments WHERE Text Like ''%' + @SearchText + '%'')
ORDER By Name'

EXEC(@SQL)
END

/*
Here is the Example How to use it..... :-)

EXEC [SearchTextInDatabase] 'SearchText'

*/

Populate Cascading Dropdown using Javascript

Here is javascript code will be useful to Populate Cascading Dropdown Manually

function pageLoad()
{
var cde = $find('test_cascade');
cde.set_contextKey("not raymond");
var parent = $get(cde.get_ParentControlID());
var index = parent.selectedIndex;
parent.selectedIndex = -1;
cde._onParentChange(null, false);
parent.selectedIndex = index;
cde._onParentChange(null, false);
}


Reference : http://forums.asp.net/t/1155780.aspx