Sunday, 28 October 2012

JavaScript: Copy to Clipboard

Sometimes it is required to provide the functionality of Copying value to Clipboard. This small piece of javascript lets up do exactly the same thing.

window.clipboardData.setData("Text", <TheTextToBeCopied>);

Saturday, 6 October 2012

Applying ToolTip to each list item in an Check Box List

I have myself faced this problem, so I am sharing this very useful code with everyone. 

When we are using Check Box List in ASP.NET then it is sometimes required to display a tooltip on each item in the list. There is no attribute as tooltip of the list items but we can achieve this by using the "title" attribute of the items while traversing through the list.

The following C# code does the same thing:

foreach (ListItem lstItem in cbxlstProducts.Items)
{         
    lstItem.Attributes["title"] = "<YourToolTipHere>";     
}

Tuesday, 25 September 2012

List all the instances of a text in the complete database

It is sometimes required to search the complete database for the instance of a particular word/text.
The following SQL Query does this work very easily and lists down the use of the searched text any where throughout the database. 

Note: I have used this in ORACLE Database. Never tried it on any other.

Select * from user_source where text like '%<YourSearchText>%'


Sunday, 23 September 2012

Return Items as XML from a Database Procedure


XML is a great way to manage large data fast and smoothly. We can use this technique in Databases also, to get resultset from the procedure in the form of XML. This XML can be used in our application to get the required data.
The data returned in the XML is in the format of tags which makes data management easier.

Sample of XML Formatting:
<ROOT_TAG>
   <TAG1>
      <DATA1>your_data_here</DATA1>
      <DATA2>your_data_here</DATA2>
   </TAG1>
   <TAG2>
      <DATA1>your_data_here</DATA1>
      <DATA2>your_data_here</DATA2>
   </TAG2>
</ROOT_TAG>

Procedure to return data as XML based on a passed user ID:
CREATE PROCEDURE SP_GetItemsAsXML
@userID NUMBER
AS  
BEGIN
           SELECT
                                    1 AS Tag,
                                    NULL AS Parent,
                                    NULL AS 'ITEMS!1!',
                                    NULL AS 'ITEM!2!ITEMS_NAME!ELEMENT',
                                    NULL AS 'ITEM!2!ITEMS_SELECTED!ELEMENT'
                         UNION ALL
                         SELECT
                                    2 AS Tag,
                                    1 AS Parent,
                                    NULL,
                                    ITEMS_NAME,
                                    ITEMS_SELECTED
                         FROM TABLE_NAME
                         WHERE ITEMS_USERID=@userID
                         FOR XML EXPLICIT
END;

Thursday, 20 September 2012

Showing a JavaScript alert message from C# code

It is required many times to show a javascript popup or alert message from C# code. This can be achieved by registering the Script of alert message. This can be achieved in the following two ways.

1. If you are using a ScriptManager on the page then use the following code:-
   ScriptManager.RegisterStartupScript(
   this, 
   GetType(),
   "<alertTitle>",
   "alert('<yourMessage>');",
   true);

2. If you are not using the ScriptManager then you can do so using the following code:-   Page.ClientScript.RegisterStartupScript(
   this.GetType(),
   "<alertTitle>",
   "alert('<yourMessage>');",
   true);


WHERE:

1. GetType() is used to get the type of the web page;
2. <alertTitle> is the title of the alert box;
3. <yourMessage> is the alert message that you want to be visible to the user;
4. true is the return type.

Code to replace all commas in a string using JavaScript



JavaScript does not replaces all commas in a given string using the Replace keyword. This may lead to various issues if the developer does not keeps this in mind.

So, to replace all commas in a string with a "<newchar>" one can use the following code segment

mystring.replace(/,/g, "<newchar>");
Example:
var mystring = "this,is,a,test";
mystring.replace(/,/g,"#");

In the above example I have replaced all commas in the string mystring with # sign.


Wednesday, 19 September 2012

Applying a Stationary Background Image to your Web Page

Many a times it is required to apply a stationary/constant background image to the web page that we are creating. 
Stationary background image means that as the user scrolls down the page, the background image will remain stationary whereas the rest of the webpage content will scroll. This gives a nice User Interface experience.
Applying stationary background to a webpage can be achieved by using any of the following two codes:-

1. Using HTML:-
<body background ="yourimage.gif" bgproperties="fixed">

2. Using CSS:-

<STYLE TYPE="text/css">
BODY { background-image: url(yourimage.jpg) }
BODY { background-repeat: no-repeat }
BODY { background-position: center }
</STYLE>